AWS went GA with the Cloud Development Kit (CDK) today. The TLDR is that this a way to really build your infrastructure as code (as opposed to calling your template “code”).
It’s an interesting project, but as you start to read through the docs, you’ll start to notice that while it delivers on the promise to let you define your infrastructure as code, it still requires CLI commands… which makes it difficult to truly run it as code.
I found, however, that with a little digging, it becomes some what trivial to model out a way you could script a templated infrastructure.
To keep it simple, I’ll be implementing the basic
Hello World example in the documentation, but on my own terms, and extrapolate from there.
Set up some basics
Like I said, I’m going to skip the CLI, so I’ll setup my
virtualenv and install a few things through
pip and the console.
python3 -m venv .env pip install aws_cdk.core pip install aws_cdk.aws_s3 mkdir output mkdir hello_cdk
Hello World steps
If I were to go through the CLI
init process, I would have a project scaffolded with an
from aws_cdk import core from hello_cdk.hello_cdk_stack import HelloCdkStack app = core.App() HelloCdkStack(app, "hello-cdk") app.synth()
You would also get a file in
./hello_cdk/hello_cdk_stack.py, and after following the instructions in
Hello World it would look like the below.
from aws_cdk import ( aws_s3 as s3, core ) class HelloCdkStack(core.Stack): def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) s3.Bucket(self, 'MyFirstBucket', versioned=True, encryption=s3.BucketEncryption.KMS_MANAGED )
This all seems straightforward enough. I generate an
App class, generate a stack class with that
App object, and include some instructions on how to build out an encrypted S3 bucket.
Trying something that won’t work
Thus far I have only replicated the exact same code output of the CLI. Now I want to try to replicate the output of the synth command.
python app.py and… nothing outputs.
Trying something that probably works better
To short circuit some troubleshooting and debugging, I’ll update
app.py a bit and execute it again.
import os import json from aws_cdk import core from hello_cdk.hello_cdk_stack import HelloCdkStack class AppFactory(): location = os.path.dirname(os.path.abspath(__file__)) def __init__(self): self.app = core.App( outdir=self.location + '/output/' ) return def make_standard_stack(self, stack_name): HelloCdkStack(self.app, stack_name) def generate_stack(self): self.generated = self.app.synth() def create_stack(self): for stack in self.generated.stacks: ...boto3 create_stack operation using
stack.template... if __name__ == "__main__": app_factory = AppFactory() app_factory.make_standard_stack("hello-cdk") app_factory.generate_stack() app_factory.create_stack()
And boom. I get an output in the form of a JSON CloudFormation template located at
I’ve implemented a factory class with some basic operations. I’ve set the
outdir location in the
App object. And finally I’ve made the naming convention flexible and created a generation step.
I could, of course, also implement a
deploy step here using
stack.template attribute, and the create_stack call – allowing me to keep fully automate the build without any CLI commands. Using a similar format, I could just as easily deploy this as a Lambda function – no keyboard required!