First step to AWS CDK
This is my first step of using AWS CDK in macOS. I am using Pyenv tool to create python enviroment as explained in the Python my workflow.
Here the simple example created using AWS CDK.
Followed AWS CDK Python workshop.
- Prepare the development env
- Initialse the project
- Synthesize a Cloudformation template
- Bootstrapping and deployment
- Create a real application
Prepare the development env
First, you have to verify that, you have installed the AWS CDK.
- in the macOS, install AWS CLI using Homebrew
- install NodeJS
- install AWS CDK using NodeJS
If all the above is good, now install the python enviroment to use with the AWS SDK:
- install Python version 3
- create virtual enviroment
p3
using pyenv tool - You can use any directory to creat your scripts, I use Visual Studio Code as my editor
- In case, I can use the following command to set the global version of the python to version 3:
pyenv global 3.8.0
Initialse the project
Create a directory as you want, in my case
mkdir helloaws
Use cdk init
to create the project
cdk init sample-app --language python
As given in the output of the above command, you have to activate the Virtualenv
source .env/bin/activate
Now install the requirments:
pip install -r requirements.txt
The default application entry point is app.py which impor the helloaws_stack.py. According to the helloaws_stack.py, following will be created default:
- sqs queue
- sns topic
Synthesize a Cloudformation template
To list the availble stacks
cdk ls
In my case I get the result helloaws
. To synthesize:
cdk synth
Here the ouput
Resources:
HelloawsQueue99542750:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 300
Metadata:
aws:cdk:path: helloaws/HelloawsQueue/Resource
HelloawsQueuePolicy3ED87862:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Statement:
- Action: sqs:SendMessage
Condition:
ArnEquals:
aws:SourceArn:
Ref: HelloawsTopic419BCE5E
Effect: Allow
Principal:
Service: sns.amazonaws.com
Resource:
Fn::GetAtt:
- HelloawsQueueN95NNN50
- Arn
Version: "2012-10-17"
Queues:
- Ref: HelloawsQueueN95NNN50
Metadata:
aws:cdk:path: helloaws/HelloawsQueue/Policy/Resource
HelloawsQueuehelloawsHelloawsTopic05A44NNA13C4E1C5:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
TopicArn:
Ref: HelloawsTopic419BCE5E
Endpoint:
Fn::GetAtt:
- HelloawsQueue9N95NNN50
- Arn
Metadata:
aws:cdk:path: helloaws/HelloawsQueue/helloawsHelloawsTopic05A4499A/Resource
HelloawsTopic418BCE5E:
Type: AWS::SNS::Topic
Metadata:
aws:cdk:path: helloaws/HelloawsTopic/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: ...
Bootstrapping and deployment
First titme you have have to install the bootstrap stack which is necessary for the toolkit.
cdk bootstrap
Now time to deply as follows:
cdk deploy helloaws
Now you are ready to create your own application
Create a real application
You have to clean up the sample-app SNS and the SQS form the helloaws_stack.py. It is better to use the following command to see the impact when you deploy new stack compared to existing:
cdk diff
if you are starisfied, run the following command to update the stack:
cdk deploy
I have followed the AWS CDK Python workshop form this point onwards.
To delete the stack
cdk destroy