cottonformation Styled Parameter

Parameterization of the CloudFormation is awesome. Because the nature of CloudFormation is JSON and YAML, there’s no easy way to implement data visiting and data handling. That’s the reason why CloudFormation invent Parameter / Mapping / Condition / Rule and Intrinsic Function system (like Fn::Join, Fn::Sub).

HOWEVER, with cottonformation, Infrastructure is simply Python Code. Python can do far more better parameterization and data handling over the Parameter and Intrinsic Function system. Here’s the introduction of the cottonformation Styled Parameter system that can completely replace the Cloudformation Parameter / Mapping / Condition / Rule and Intrisic Function system.

# -*- coding: utf-8 -*-

import attr
import cottonformation as ctf
from cottonformation.res import s3


# declare a Parameter object
@attr.s
class Params:
    project_name: str = attr.ib()
    stage: str = attr.ib()

    # a derived value as a prefix for all naming convention and tag
    @property
    def env_name(self):
        return f"{self.project_name}-{self.stage}"


# declare a function that creates Template object from Parameters, Mappings,
# Conditions, Rules ...
def create_template(params: Params) -> ctf.Template:
    tpl = ctf.Template(Description="Demo: ctf styled parameter")

    bucket = s3.Bucket("MyBucket", p_BucketName=f"{params.env_name}-my-bucket")
    tpl.add(bucket)

    tpl.batch_tagging(dict(EnvName=params.env_name))

    return tpl


if __name__ == "__main__":
    # give values to parameters
    param = Params(
        project_name="ctf-styled-param",
        stage="dev"
    )
    tpl = create_template(param)
    print(tpl.to_json())

Template json:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Demo: ctf styled parameter",
    "Metadata": {
        "cottonformation": {
            "version": "0.0.3"
        }
    },
    "Resources": {
        "MyBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "ctf-styled-param-dev-my-bucket",
                "Tags": [
                    {
                        "Key": "EnvName",
                        "Value": "ctf-styled-param-dev"
                    }
                ]
            }
        }
    }
}