CFModel

CFModel

Template that describes AWS infrastructure.

Properties:

  • AWSTemplateFormatVersion
  • Conditions: Conditions that control behaviour of the template.
  • Description: Description for the template.
  • Mappings: A 3 level mapping of keys and associated values.
  • Metadata: Additional information about the template.
  • Outputs: Output values of the template.
  • Parameters: Parameters to the template.
  • Resources: Stack resources and their properties.
  • Rules
  • Transform: For serverless applications, specifies the version of the AWS Serverless Application Model (AWS SAM) to use.

More info at AWS Docs

expand_actions(self)

Returns a model which has expanded all wildcards (*) to get all implied actions for every resource. For example:

  • a model containing s3:* will be expanded to list all the possible S3 actions.
  • a model containing s3:Get* will be expanded to all the Get* actions only.

This method can handle the cases of both Action and NotAction.

Known AWS Actions. These known actions can be updated by executing:

python3 scripts/generate_cloudformation_actions_file.py
Source code in pycfmodel/model/cf_model.py
def expand_actions(self) -> "CFModel":
    """
    Returns a model which has expanded all wildcards (`*`) to get all implied actions for every resource.
    For example:\n
      - a model containing `s3:*` will be expanded to list all the possible S3 actions.
      - a model containing `s3:Get*` will be expanded to all the `Get*` actions only.

    This method can handle the cases of both `Action` and `NotAction`.

    [Known AWS Actions](https://github.com/Skyscanner/pycfmodel/blob/master/pycfmodel/cloudformation_actions.py).
    These known actions can be updated by executing:

    ```
    python3 scripts/generate_cloudformation_actions_file.py
    ```
    """
    dict_value = self.dict()

    resources = dict_value.pop("Resources")
    expanded_resources = {key: expand_actions(value) for key, value in resources.items()}

    return CFModel(**dict_value, Resources=expanded_resources)

resolve(self, extra_params=None)

Resolve all intrinsic functions on the template.

Parameters:

Name Type Description Default
extra_params

Values of parameters passed to the Cloudformation.

None

Returns:

Type Description
CFModel

A new CFModel.

Source code in pycfmodel/model/cf_model.py
def resolve(self, extra_params=None) -> "CFModel":
    """
    Resolve all intrinsic functions on the template.

    Arguments:
        extra_params: Values of parameters passed to the Cloudformation.

    Returns:
        A new CFModel.
    """
    extra_params = {} if extra_params is None else extra_params
    # default parameters
    params = {}
    for key, parameter in self.Parameters.items():
        passed_value = extra_params.pop(key, None)
        ref_value = parameter.get_ref_value(passed_value)
        if ref_value is not None:
            params[key] = ref_value

    extended_parameters = {**self.PSEUDO_PARAMETERS, **params, **extra_params}
    dict_value = self.dict()

    conditions = dict_value.pop("Conditions", {})
    resolved_conditions = {}
    for key, value in conditions.items():
        resolved_conditions.update(
            {key: _extended_bool(resolve(value, extended_parameters, self.Mappings, resolved_conditions))}
        )

    resources = dict_value.pop("Resources")
    resolved_resources = {
        key: resolve(value, extended_parameters, self.Mappings, resolved_conditions)
        for key, value in resources.items()
        if value.get("Condition") is None
        or (value.get("Condition") is not None and resolved_conditions.get(value["Condition"], True))
    }
    return CFModel(**dict_value, Conditions=resolved_conditions, Resources=resolved_resources)

resources_filtered_by_type(self, allowed_types)

Filtered resources based on types.

Parameters:

Name Type Description Default
allowed_types Collection[Union[str, Type[pycfmodel.model.resources.resource.Resource]]]

Collection of desired types.

required

Returns:

Type Description
Dict[str, Dict[str, pycfmodel.model.resources.resource.Resource]]

Dictionary where key is the logical id and value is the resource.

Source code in pycfmodel/model/cf_model.py
def resources_filtered_by_type(
    self, allowed_types: Collection[Union[str, Type[Resource]]]
) -> Dict[str, Dict[str, Resource]]:
    """
    Filtered resources based on types.

    Arguments:
        allowed_types: Collection of desired types.

    Returns:
        Dictionary where key is the logical id and value is the resource.
    """
    result = {}
    allowed_resource_classes = tuple(x for x in allowed_types if isinstance(x, type))
    for resource_name, resource in self.Resources.items():
        if isinstance(resource, allowed_resource_classes) or resource.Type in allowed_types:
            result[resource_name] = resource
    return result

Parameter

CloudFormation Parameter object representation

get_ref_value(self, provided_value=None)

Calculates the parameter value to be used in the template.

  • If NoEcho property is set, it uses a constant value.
  • If it is a list of numbers or a comma delimited list, returns the string version of each element in a list.
  • Returns None if provided_value and Default are None.

Parameters:

Name Type Description Default
provided_value

Value injected in the template

None

Returns:

Type Description
Optional[str]

The computed value.

Source code in pycfmodel/model/parameter.py
def get_ref_value(self, provided_value=None) -> Optional[str]:
    """
    Calculates the parameter value to be used in the template.

    - If `NoEcho` property is set, it uses a constant value.
    - If it is a list of numbers or a comma delimited list, returns the string version of each element in a list.
    - Returns None if `provided_value` and `Default` are `None`.

    Arguments:
        provided_value: Value injected in the template

    Returns:
        The computed value.
    """
    value = provided_value if provided_value is not None else self.Default
    if self.NoEcho:
        if provided_value is not None:
            return self.NO_ECHO_WITH_VALUE
        elif self.Default:
            return self.NO_ECHO_WITH_DEFAULT
        else:
            return self.NO_ECHO_NO_DEFAULT

    elif self.Type in ["List<Number>", "CommaDelimitedList"]:
        return value.split(",")

    return value if value is None else str(value)