What’s up, guys!
In this article, you’ll be learning how to set up AWS CDK with Python. AWS CDK is a tool by Amazon AWS which is used to create resources in the AWS cloud programmatically. This toolkit is an alternative to existing tools like Terraform, CloudFormation, etc. This is especially for programmers who want to put conditional stuff that requires programming logic in order to deploy or create certain resources.
How To Set Up AWS CDK With Python
Setup PIP and Virtualenv
First, Install the required Python dependencies:
$ python -m ensurepip --upgrade
$ python -m pip install --upgrade pip
$ python -m pip install --upgrade virtualenv
Setup NPM
If you have this set up already in your system skip to the next section.
First, we will install NPM as we will be using this to install AWS CDK tool.
# Ubuntu/Debina
$ apt install nodejs
# Mac
$ brew install nodejs
# Redhat based or later version
$ yum install nodejs
Verify NPM
$ npm --version
Output:
$ npm --version
8.5.5
Also, you might want to learn : How to setup Node.js on ubuntu through NVM.
Install AWS-CDK
Now, install AWS CDK using npm:
$ npm install -g aws-cdk
Verify AWS-CDK
Run the version command:
$ cdk --version
Output:
$ cdk --version
2.9.0 (build a69725c)
Example: Create First CDK Project
After the successful installation of CDK, we’re ready to create our first test project to start doing things with it.
$ mkdir example_app
$ cd example_app
$ cdk init app example_app --language python
Output:
Applying project template app for python
# Welcome to your CDK Python project!
...
Enjoy!
Please run 'python3 -m venv .venv'!
Executing Creating virtualenv...
✅ All done!
Inside the project directory, it initializes a virtualenv. (if you haven’t installed virtualenv, install it first)
Or, you can also initialize your own virtualenv. (by running python -m venv .venv
inside the project directory.)
Check the .venv file:
$ ls -al | grep venv
Output:
ls -al | grep venv
drwxr-xr-x 5 zugger zugger 4096 Apr 25 15:36 .venv
Since it is initialized already, the first thing we need to do is activate the virtual environment. To do so run the following command:
$ source .venv/bin/activate
Now that we are inside the python virtual environment and we can install some dependencies.
Add any dependencies or requirements your project may need in the requirements.txt file and install it by running:
$ pip install -r requirements.txt
You can add needed dependencies later on also and run the same command to install.
Verify Python AWS CDK
To verify let’s enter into the python shell:
$ python
And run followings:
>>> import aws_cdk
>>> from aws_cdk import aws_ec2 as ec2
>>> ec2
The output will be:
As shown above, if everything works fine then you have successfully installed and set up AWS CDK for Python.
Initialize AWS CDK Stack
Before starting deploying stack:
- Region: The default region your stack will be deployed on.
- Account: Your AWS account.
Here, CDK tool has pre-created a Python file called app.py which contains these details in the init function to keep things clean.
import os
import aws_cdk as cdk
from example_app.example_app_stack import ExampleAppStack
app = cdk.App()
ExampleAppStack(app, "ExampleAppStack",
# If you don't specify 'env', this stack will be environment-agnostic.
# Account/Region-dependent features and context lookups will not work,
# but a single synthesized template can be deployed anywhere.
# Uncomment the next line to specialize this stack for the AWS Account
# and Region that are implied by the current CLI configuration.
#env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
# Uncomment the next line if you know exactly what Account and Region you
# want to deploy the stack to. */
#env=cdk.Environment(account='123456789012', region='us-east-1'),
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
)
app.synth()
If you see the lines above the account and the region is passed on your Environment function, feel in the attributes with the information you have.
Synthesize and Deploy AWS CDK Stack
A stack is basically a set of AWS resources that you can use and manipulate programmatically using your CDK.
In order to do this you simply need to run:
$ cdk synth ExampleAppStack
To deploy the changes:
$ cdk deploy ExampleAppStack
These commands will basically let you synthesize and then deploy a stack named ExampleAppStack.
That’s it!
Conclusion
In this, you have learned about how to set up AWS CDK with python with an example.
Thank you!
Articles on AWS: