Define AWS Infrastructure in Python with AWS Cloud Development Kit
With AWS CDK, we can define an infrastructure using familiar languages (Python, JS, Java, etc) . The code will be compiled into a cloud formation JSON and deployed as a cloud formation stack. Later we can diff the changes made before redeploy.
brew install node npm install -g aws-cdk
cdk init app --language python python3 -m venv .venv source .venv/bin/activate python -m pip install -r requirements.txt pip install aws-cdk.aws-s3 # module to manage s3
from aws_cdk import core from aws_cdk import aws_s3 as s3 class FnInfraStack(core.Stack): def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) s3.bucket(self, "TestingBucketVarokas", versioned=True, )
Preview the cloud formation template with
We can then immediately deploy the changes as CloudFormation stack
cdk --profile varokas deploy
Changes and Diffs
Any changes to the code can be diff-ed against currently deployed stack
There isn't a way to import existing AWS resources to CDK yet
A workaround exist by creating a failed CloudFormation deployment and fix the template.