AWS Cloud Development Kit
If you’ve just landed here, we’re doing a “Become a Cloud Architect” Unicorn Workshop by building a Unicorn Pursuit Web App step by step, and you’re more then welcome to join!
About AWS CDK
CDK is a Cloud Development Kit. It allows us to programatically define our infrastructure, and luckily - Python is one of the options. AWS CDK will allow us to write our infrastructure in Python, and it will manage CloudFormation Template creation and stack deployment for us.
Your Development envionment is ready, if you followed the tutorial on how to set it up… if you didn’t just click here, do it and come back… I’ll wait
Before you jump into the code you should consider doing a very short (2-3 hours) official AWS CDK Workshop. It will help you understand the basics, and this is perhaps among the most important topics of the Workshop.
Constructs are the basic building block of CDK apps. They represent abstract “cloud components” which can be composed together into higher level abstractions via scopes. Scopes can include constructs, which in turn can include other constructs, etc. Constructors must always have:
- Scope, which is normaly ‘self’
- Id, as a local identity
- Kwargs, a set of optional initialization arguments, specific to each construct. For example, for Lambda, it will be runtime, code and handler. Details: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-readme.html
The AWS CDK is shipped with an extensive library of constructs called the AWS Construct Library. The construct library is divided into modules, one for each AWS service. For example, if you want to define an AWS Lambda function, we will need to use the AWS Lambda construct library. Bookmark this, you’ll need it.
Code
Let me point the important files:
- app.py: Entrypoint to our infrastructure. See Apps on the CDK Documentation for more information
- iac/iac_stack.py: Our stack. See Stacks on the CDK Documentation for more information
- setup.py: Setup configurations of the project
Let’s assume you did the official CDK workshop, and you now understand that AWS CDK is a programmatic way to manage your CloudFormation Stacks.
You need to be in your IAC folder, and initialize the CDK with Python:
cdk init app --language python
This will create files and folders. Go to VS Code and open your IAC folder. Find the app.py file. This is where you define:
- CloudFormation Stack configuration.
- TAGs
- Region
You can use multiple Apps within the same app.py, each one in different region if you want, but for now - we will use Ireland Region, and 3 TAGs: project, bu (business unit) and environment.
#!/usr/bin/env python3
from aws_cdk import core
# Import Tag library, so that you can have all resources within the Stack Tagged
from aws_cdk.core import Tag
from iac.iac_stack import IacStack
app = core.App()
# Next line is where you define CloudFormation Stack Name and the Region
IacStack(app, "UnicornIaC", env={'region': 'eu-west-1'})
# Define the TAGs all resources will have. Example, key: project, value: unicorn.
Tag.add(app,"project", "unicorn")
Tag.add(app,"bu", "cloud")
Tag.add(app,"environment", "prod")
app.synth()
After initializing the project, activate the project’s virtual environment. This allows the project’s dependencies to be installed locally in the project folder, instead of globally. Do this whenever you’re working with CDK.
source .env/bin/activate
Once you’re done, just do deactivate
and you’re back in your environment.
deactivate
Only AFTER you’ve done this, install APPs dependencies:
pip install -r requirements.txt
Ok, now install all the modules for AWS services you wish to use, and update the requirements.txt file. For example, to add DynamoDB, install the DynamoDB CDK module, and update the requirements.txt:
pip install aws-cdk.aws-dynamodb
pip freeze > requirements.txt
pip freeze captures the current versions of all modules installed in your virtual environment. You can edit requirements.txt to allow upgrades; simply replace the == preceding a version number with ~= to allow upgrades to a higher compatible version, or remove the version requirement entirely to specify the latest available version of the module.
Deep Dive
Very important, when stuff is not supported in CDK, you can still do a mix of things:
- Write a custom resource.
- Using AWS CloudFormation Constructs (the level 1 Cfn* constructs), follow this link
- Use escape hatches like addPropertyOverride.
- Cast as Cfn object like bucket.node.defaultChild as s3.CfnBucket
Just to be extra clear, everything you can do in CFT can be done in CDK. Check out the details.
Ok, let’s see our code. When we first create a project, and check out the content of our IAC folder, we’ll see:
(.env) ➜ iac git:(dev) ✗ ls -lsa
total 56
0 drwxr-xr-x 11 mjovanovic staff 352 Apr 29 16:18 .
0 drwxr-xr-x 24 mjovanovic staff 768 Apr 29 15:50 ..
0 drwxr-xr-x 6 mjovanovic staff 192 Apr 29 16:03 .env
8 -rw-r--r-- 1 mjovanovic staff 118 Apr 29 16:03 .gitignore
8 -rw-r--r-- 1 mjovanovic staff 1652 Apr 29 16:03 README.md
8 -rw-r--r-- 1 mjovanovic staff 538 Apr 29 16:30 app.py
8 -rw-r--r-- 1 mjovanovic staff 144 Apr 29 16:03 cdk.json
0 drwxr-xr-x 5 mjovanovic staff 160 Apr 29 16:17 iac
8 -rw-r--r-- 1 mjovanovic staff 867 Apr 29 16:20 requirements.txt
8 -rw-r--r-- 1 mjovanovic staff 1016 Apr 29 16:03 setup.py
8 -rw-r--r-- 1 mjovanovic staff 434 Apr 29 16:03 source.bat
(.env) ➜ iac git:(dev) ✗
(.env) ➜ iac git:(dev) ✗ ls -ls
total 8
0 -rw-r--r-- 1 mjovanovic staff 0 Apr 29 16:03 __init__.py
0 drwxr-xr-x 7 mjovanovic staff 224 Apr 29 16:17 iac.egg-info
8 -rw-r--r-- 1 mjovanovic staff 230 Apr 29 16:03 iac_stack.py
(.env) ➜ iac git:(dev) ✗
Apart from the app.py python file we’ve seen before, which is where our global configuration is, the core of our insfrastructure is in .iac/iac folder, and it’s called iac_stack.py. In VS Code, check out this file, it should look like the code below. Can you recognize where our Scope, Construct and Kwargs are?
from aws_cdk import core
class IacStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# The code that defines your stack goes here
We will start from here, and as we move forward, we will be adding more and more AWS Resources required for the Unicorn.
Issues
Could not build wheels for aws-cdk.core, since package 'wheel' is not installed.
Solution
(.env) ➜ iac git:(dev) ✗ pip install wheel
Collecting wheel
Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: wheel
Successfully installed wheel-0.34.2
(.env) ➜ iac git:(dev) ✗ pip install --upgrade aws-cdk.core
Requirement already up-to-date: aws-cdk.core in ./.env/lib/python3.7/site-packages (1.36.1)
Requirement already satisfied, skipping upgrade: aws-cdk.cx-api==1.36.1 in ./.env/lib/python3.7/site-packages (from aws-cdk.core) (1.36.1)
Requirement already satisfied, skipping upgrade: publication>=0.0.3 in ./.env/lib/python3.7/site-packages (from aws-cdk.core) (0.0.3)
Requirement already satisfied, skipping upgrade: jsii<2.0.0,>=1.4.1 in ./.env/lib/python3.7/site-packages (from aws-cdk.core) (1.4.1)
Requirement already satisfied, skipping upgrade: constructs<4.0.0,>=3.0.2 in ./.env/lib/python3.7/site-packages (from aws-cdk.core) (3.0.2)
Requirement already satisfied, skipping upgrade: aws-cdk.cloud-assembly-schema==1.36.1 in ./.env/lib/python3.7/site-packages (from aws-cdk.core) (1.36.1)
Requirement already satisfied, skipping upgrade: typing-extensions~=3.7.4 in ./.env/lib/python3.7/site-packages (from jsii<2.0.0,>=1.4.1->aws-cdk.core) (3.7.4.2)
Requirement already satisfied, skipping upgrade: cattrs~=1.0.0 in ./.env/lib/python3.7/site-packages (from jsii<2.0.0,>=1.4.1->aws-cdk.core) (1.0.0)
Requirement already satisfied, skipping upgrade: attrs~=19.3.0 in ./.env/lib/python3.7/site-packages (from jsii<2.0.0,>=1.4.1->aws-cdk.core) (19.3.0)
Requirement already satisfied, skipping upgrade: python-dateutil in ./.env/lib/python3.7/site-packages (from jsii<2.0.0,>=1.4.1->aws-cdk.core) (2.8.1)
Requirement already satisfied, skipping upgrade: six>=1.5 in ./.env/lib/python3.7/site-packages (from python-dateutil->jsii<2.0.0,>=1.4.1->aws-cdk.core) (1.14.0)
(.env) ➜ iac git:(dev) ✗
Where to find more info
Important links:
- CDK - CloufFormation Layer - CloudFormation Custom Resources - AWS CDK Custom Resources
- Filling CDK Gaps
Feedback
Was this page helpful?
Awesome! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.