Getting Started with AWS CDK Infrastructure as Code
Learn how to write your first AWS CDK app in your laptop and deploy resources in your AWS account right from your laptop. I am using MAC os for this demo. Later on I will use cloud9
to deploy resources in AWS cloud and try to setup CI/CD for my CDK app as well. I will try to setup CI/CD right from my GitHub repository also.
You CDK app has high level class called Stage. Stage can have many Stacks. You can synthesize ( convert code in cloudformation api ) & deploy one or more stacks to your AWS account. This tutorial walks you through creating and deploying a simple AWS CDK app, from initializing the project to deploying the resulting AWS CloudFormation template. The app contains one stack, which contains one resource: an Amazon S3 bucket.
We’ll also show what happens when you make a change and re-deploy, and how to clean up when you’re done.
http://www.rupeshtiwari.com/getting-started-with-aws-cdk/
Step 1: First time environment setup steps
- Open
sudo vim ~/.aws/credentials
and set your access key and security key. - Install
aws-cdk
globallynpm i -g aws-cdk
check versioncdk --version
- Run
aws sts get-caller-identity
to know your account number and runaws configuration get region
to know your region - Run
cdk bootstrap aws://ACCOUNT-NUMBER/REGION
. Bootstrap AWS CDK that will create S3 container to store assets required for deployment. - If this is for production then make sure you create dedicated account to deploy your infrastructure and assign this account role to deploy on another accounts.
- Install AWS toolkit extension in your VSCode that will help you to create, debug and deploy applications on AWS. You can also see all of your AWS CDK projects.
Step 2: Create first AWS CDK project
- Create the app
mkdir hello-cdk cd hello-cdk
cdk init app --language typescript
, it will create cdk template project also initialize git.- Build the app
npm run build
cdk ls
see stacks- Goto
lib/stack.ts
add below line// create new s3 bucket new s3.Bucket(this, 'CdkCreatedBucket', { versioned: true })
Step 3: Synthesize an AWS CloudFormation template
Run cdk synth
. The cdk synth generates a perfectly valid AWS CloudFormation template. You could take it and deploy it using the AWS CloudFormation console or another tool. But the AWS CDK Toolkit can also do that.
Step 4: Deploy the stack
Run cdk deploy
. CDK will deploy the stack using AWS CloudFormation. The AWS CDK synthesizes your stack before each deployment.
Cloudformation stack created at AWS account on my selected region.
Deployment successful from my laptop
S3 created in my AWS account hurray!
Step 5: Modify the app
The AWS CDK can update your deployed resources after you modify your app. Update lib/hello-cdk-stack.ts.
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true
});
Run cdk diff
to check the differences
Run cdk deploy
to deploy changes.
Before deploy S3 policy
After deploy S3 policy changed
Step 6: Destroying the app’s resources
Now that you’re done with the quick tour, destroy your app’s resources to avoid incurring any costs from the bucket you created, as follows.
cdk destroy
Enter y to approve the changes and delete any stack resources.
Destroy was super quick!!
Reference
- https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html
- https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html