Creating resources in AWS has always been a challenge for the growing enterprises or for the enterprises which are using AWS resources or services in large scale. There are indeed different approaches, as below:
2. Using AWS CLI (Call API)
3. Declarative (using Cloudformation)
4. DOM (Using a different language to create the CF templates and deploying the same, example: Python troposphere)
In last few years DOM was heavily used, but that comes with different set of challenges. As an example using the same if someone wants to create a VPC might end up writing hundred lines of code.
Setup CDK in local using Python:
Here we have used Windows machine, on the top of that we have installed virtual linux box on the top of it.
Install below softwares and set them up in your system:
- Virtual Linux
- Visual Studio
Setup the language:
This applies the python template theme on the project.
cdk init app –language python
On running the above command below things happened in the work space:
- A blank project in CDK python is created.
- cdk.json gets created which tells the CDK how to execute the app.
- The initialization process creates a virtual environment.
- The AWS packages can be downloaded in the requirements file. Using command: $ pip install -r requirements.txt
Some Useful CDK commands:
## Useful commands
- * `cdk ls` list all stacks in the app or compiles
- * `cdk synth` emits the synthesized CloudFormation template
- * `cdk deploy` deploy this stack to your default AWS account/region
- * `cdk diff` compare deployed stack with current state
- * `cdk docs` open CDK documentation
Enable the virtual environment with the below command:
Install the aws-cdk libraries as follows:
Create a S3 bucket in the python file.
After synthesizing the python file the template.json file gets created:
For first time, if you are running the project then use the below command: $cdk bootstrap
Once the above command is run, below changes are visible in the S3 and Cloudformation services:
A bucket gets created:
Then deploy using the below command:
The Bucket gets created:
You can check the same from the stack:
Plan and visualize deployment:
$ Cdk diff – keyword to check the differences between previous and what we have changed.
We can use Cdk token, which is cfn intrinsic function (part of aws-cdk.core library).
Now using Cdk token we can save the state of the aws resource and that can be used in the different project as well.
In cloud formation stack:
To destroy the resources use below command: