runway.cfngin.blueprints.base module

CFNgin blueprint base classes.

class runway.cfngin.blueprints.base.CFNParameter[source]

Bases: object

Wrapper around a value to indicate a CloudFormation Parameter.

__init__(name: str, value: Union[bool, float, int, List[Any], str, Any]) None[source]

Instantiate class.

Parameters
  • name – The name of the CloudFormation Parameter.

  • value – The value we’re going to submit as a CloudFormation Parameter.

__repr__() str[source]

Object represented as a string.

property ref: troposphere.Ref

Ref the value of a parameter.

to_parameter_value() Union[List[Any], str][source]

Return the value to be submitted to CloudFormation.

__new__(**kwargs)
runway.cfngin.blueprints.base.build_parameter(name: str, properties: BlueprintVariableTypeDef) Parameter[source]

Build a troposphere Parameter with the given properties.

Parameters
Returns

The created parameter object.

runway.cfngin.blueprints.base.validate_variable_type(var_name: str, var_type: Union[Type[runway.cfngin.blueprints.variables.types.CFNType], runway.cfngin.blueprints.variables.types.TroposphereType[Any], type], value: Any) Any[source]

Ensure the value is the correct variable type.

Parameters
  • var_name – The name of the defined variable on a blueprint.

  • var_type – The type that the value should be.

  • value – The object representing the value provided for the variable

Returns

The appropriate value object. If the original value was of CFNType, the returned value will be wrapped in CFNParameter.

Raises

ValueError – If the value isn’t of var_type and can’t be cast as that type, this is raised.

runway.cfngin.blueprints.base.validate_allowed_values(allowed_values: Optional[List[Any]], value: Any) bool[source]

Support a variable defining which values it allows.

Parameters
  • allowed_values – A list of allowed values from the variable definition.

  • value – The object representing the value provided for the variable.

Returns

Boolean for whether or not the value is valid.

runway.cfngin.blueprints.base.resolve_variable(var_name: str, var_def: BlueprintVariableTypeDef, provided_variable: Optional[Variable], blueprint_name: str) Any[source]

Resolve a provided variable value against the variable definition.

Parameters
  • var_name – The name of the defined variable on a blueprint.

  • var_def – A dictionary representing the defined variables attributes.

  • provided_variable – The variable value provided to the blueprint.

  • blueprint_name – The name of the blueprint that the variable is being applied to.

Returns

The resolved variable value, could be any python object.

Raises
runway.cfngin.blueprints.base.parse_user_data(variables: Dict[str, Any], raw_user_data: str, blueprint_name: str) str[source]

Parse the given user data and renders it as a template.

It supports referencing template variables to create userdata that’s supplemented with information from the stack, as commonly required when creating EC2 userdata files.

Example

Given a raw_user_data string: 'open file ${file}' And a variables dictionary with: {'file': 'test.txt'} parse_user_data would output: open file test.txt

Parameters
  • variables – Variables available to the template.

  • raw_user_data – The user_data to be parsed.

  • blueprint_name – The name of the blueprint.

Returns

The parsed user data, with all the variables values and refs replaced with their resolved values.

Raises
  • InvalidUserdataPlaceholder – Raised when a placeholder name in raw_user_data is not valid. E.g ${100} would raise this.

  • MissingVariable – Raised when a variable is in the raw_user_data that is not given in the blueprint

class runway.cfngin.blueprints.base.Blueprint[source]

Bases: runway.mixins.DelCachedPropMixin

Base implementation for rendering a troposphere template.

VARIABLES

Class variable that defines the values that can be passed from CFNgin to the template. These definition include metadata used to validate the provided value and to propagate the variable to the resulting CloudFormation template.

Type

ClassVar[Dict[str, BlueprintVariableTypeDef]]

context

CFNgin context object.

Type

CfnginContext

description

The description of the CloudFormation template that will be generated by this Blueprint.

Type

Optional[str]

mappings

CloudFormation Mappings to be added to the template during the rendering process.

Type

Optional[Dict[str, Dict[str, Any]]]

name

Name of the Stack that will be created by the Blueprint.

Type

str

template

Troposphere template.

Type

Template

__init__(name: str, context: runway.context.CfnginContext, *, description: Optional[str] = None, mappings: Optional[Dict[str, Dict[str, Any]]] = None, template: Optional[troposphere.Template] = None, **_: Any)[source]

Instantiate class.

Parameters
  • name – A name for the blueprint.

  • context – Context the blueprint is being executed under.

  • description – The description of the CloudFormation template that will be generated by this blueprint.

  • mappings – CloudFormation Mappings to be used in the template during the rendering process.

  • template – Optionally, provide a preexisting Template.

Changed in version 2.0.0: Added template.

Changed in version 2.0.0: Class only takes 2 positional arguments. The rest are now keyword arguments.

property cfn_parameters: Dict[str, Union[List[Any], str]]

Return a dict of variables with type CFNType.

New in version 2.0.0.

Returns

Variables that need to be submitted as CloudFormation Parameters.

create_template() None[source]

Abstract method called to create a template from the blueprint.

property defined_variables: Dict[str, BlueprintVariableTypeDef]

Return a copy of VARIABLES to avoid accidental modification of the ClassVar.

Changed in version 2.0.0: Changed from a method to a property.

property output_definitions: Dict[str, Dict[str, Any]]

Get the output definitions.

New in version 2.0.0.

Returns

Output definitions. Keys are output names, the values are dicts containing key/values for various output properties.

property parameter_definitions: Dict[str, BlueprintVariableTypeDef]

Get the parameter definitions to submit to CloudFormation.

Any variable definition whose type is an instance of CFNType will be returned as a CloudFormation Parameter.

New in version 2.0.0.

Returns

Parameter definitions. Keys are parameter names, the values are dicts containing key/values for various parameter properties.

property parameter_values: Dict[str, Union[List[Any], str]]

Return a dict of variables with type CFNType.

New in version 2.0.0.

Returns

Variables that need to be submitted as CloudFormation Parameters. Will be a dictionary of <parameter name>: <parameter value>.

property rendered: str

Return rendered blueprint.

property required_parameter_definitions: Dict[str, BlueprintVariableTypeDef]

Return all template parameters that do not have a default value.

New in version 2.0.0.

Returns

Dict of required CloudFormation Parameters for the blueprint. Will be a dictionary of <parameter name>: <parameter attributes>.

property requires_change_set: bool

Return true if the underlying template has transforms.

property variables: Dict[str, Any]

Return a Dict of variables available to the Template.

These variables will have been defined within VARIABLES or defined_variables. Any variable value that contains a Lookup will have been resolved.

New in version 2.0.0.

Returns

Variables available to the Template.

Raises

UnresolvedBlueprintVariables – If variables are unresolved.

property version: str

Template version.

add_output(name: str, value: Any) None[source]

Add an output to the template.

Wrapper for self.template.add_output(Output(name, Value=value)).

Parameters
  • name – The name of the output to create.

  • value – The value to put in the output.

get_cfn_parameters() Dict[str, Union[List[Any], str]][source]

Return a dictionary of variables with type CFNType.

Deprecated since version 2.0.0: Replaced by cfn_parameters.

Returns

Variables that need to be submitted as CloudFormation Parameters.

get_output_definitions() Dict[str, Dict[str, Any]][source]

Get the output definitions.

Deprecated since version 2.0.0: Replaced by output_definitions.

Returns

Output definitions. Keys are output names, the values are dicts containing key/values for various output properties.

get_parameter_definitions() Dict[str, BlueprintVariableTypeDef][source]

Get the parameter definitions to submit to CloudFormation.

Any variable definition whose type is an instance of CFNType will be returned as a CloudFormation Parameter.

Deprecated since version 2.0.0: Replaced by parameter_definitions.

Returns

Parameter definitions. Keys are parameter names, the values are dicts containing key/values for various parameter properties.

get_parameter_values() Dict[str, Union[List[Any], str]][source]

Return a dict of variables with type CFNType.

Deprecated since version 2.0.0: Replaced by parameter_values.

Returns

Variables that need to be submitted as CloudFormation Parameters. Will be a dictionary of <parameter name>: <parameter value>.

get_required_parameter_definitions() Dict[str, BlueprintVariableTypeDef][source]

Return all template parameters that do not have a default value.

Deprecated since version 2.0.0: Replaced by required_parameter_definitions.

Returns

Dict of required CloudFormation Parameters for the blueprint. Will be a dictionary of <parameter name>: <parameter attributes>.

get_variables() Dict[str, Any][source]

Return a dictionary of variables available to the template.

These variables will have been defined within VARIABLES or self.defined_variables. Any variable value that contains a lookup will have been resolved.

Deprecated since version 2.0.0: Replaced by variables.

Returns

Variables available to the template.

Raises

UnresolvedBlueprintVariables – If variables are unresolved.

__new__(**kwargs)
import_mappings() None[source]

Import mappings from CFNgin config to the blueprint.

read_user_data(user_data_path: str) str[source]

Read and parse a user_data file.

Parameters

user_data_path – Path to the userdata file.

render_template() Tuple[str, str][source]

Render the Blueprint to a CloudFormation template.

reset_template() None[source]

Reset template.

resolve_variables(provided_variables: List[runway.variables.Variable]) None[source]

Resolve the values of the blueprint variables.

This will resolve the values of the VARIABLES with values from the env file, the config, and any lookups resolved.

Parameters

provided_variables – List of provided variables.

set_template_description(description: str) None[source]

Add a description to the Template.

Parameters

description – A description to be added to the resulting template.

setup_parameters() None[source]

Add any CloudFormation parameters to the template.

to_json(variables: Optional[Dict[str, Any]] = None) str[source]

Render the blueprint and return the template in json form.

Parameters

variables – Dictionary providing/overriding variable values.