Learn to use AWS CDK for creating and managing AWS resources, including S3 bucket infrastructure, through installation, project initialization, deployment, and cleanup.
In this guide, you’ll learn how to use the AWS Cloud Development Kit (CDK) to create and manage AWS resources. We’ll cover installing prerequisites, initializing a CDK project, exploring generated files, and then deploying, verifying, and cleaning up a simple S3 bucket infrastructure.
Before you begin with AWS CDK, ensure that Node.js is installed on your machine. Node.js comes with NPM (Node Package Manager), which is required to install the AWS CDK CLI tool.After installing Node.js, run these commands to install and verify the AWS CDK CLI:
Copy
Ask AI
aws sts get-caller-identity
Copy
Ask AI
npm install -g aws-cdk
Copy
Ask AI
cdk --version
Ensure Node.js is installed correctly by referring to the Node.js documentation. The site usually detects your operating system and provides the appropriate download options.
For this demo, we’ll modify the CDK stack to create an S3 bucket. First, update the import statements to include AWS S3 (and optionally AWS KMS if you need encryption):
Copy
Ask AI
from constructs import Constructfrom aws_cdk import ( Duration, Stack, aws_s3 as s3, aws_iam as iam, aws_sqs as sqs, aws_sns as sns, aws_sns_subscriptions as subs,)# Additional imports (e.g., for encryption) can be added as required.
Then update the stack definition to include the S3 bucket:
Remember that bucket names must be globally unique. The CDK will append extra characters to ensure uniqueness.
For advanced configurations such as encryption using a KMS key, you might use:
Copy
Ask AI
bucket = s3.Bucket(self, "MyEncryptedBucket", encryption=s3.BucketEncryption.KMS)# To verify, you can access the encryption key:# assert(bucket.encryption_key instanceof kms.Key)
In this demo, however, we use the basic bucket configuration.
To generate the CloudFormation templates from your CDK app, run:
Copy
Ask AI
cdk synth
This command synthesizes templates that detail the AWS resources like the S3 bucket and any related policies. A sample snippet of the generated output might be:
Before deploying, configure your AWS CLI credentials since the AWS CDK utilizes them for resource deployment. Run:
Copy
Ask AI
aws configure
You will be prompted to enter your AWS Access Key ID, AWS Secret Access Key, default region, and output format. For example:
Copy
Ask AI
AWS Access Key ID [********************SQ5B]: AKIA4IAWSJ5UJ5XRRTMAIAWS Secret Access Key [********************aRv]: <your-secret-key>Default region name [us-east-1]:Default output format [json]:
It is best practice to create a dedicated IAM user with limited permissions for CDK deployments in production. Avoid using full administrative privileges.
The following screenshots illustrate the process of creating an IAM user and generating access keys:
Before deploying your changes, run a diff to compare your local template with what is currently deployed:
Copy
Ask AI
cdk diff
The output will indicate which resources are set to be created, modified, or deleted. Since the S3 bucket hasn’t been deployed yet, the diff output should reflect that a new S3 bucket will be created.
Certain environments require bootstrapping before deployment. If you encounter an error such as:
Copy
Ask AI
Error: CdkStack: SSM parameter /cdk-bootstrap/hnb659fds/version not found. Has the environment been bootstrapped? Please run 'cdk bootstrap'
Run the bootstrap command:
Copy
Ask AI
cdk bootstrap
After successful bootstrapping, deploy your stack with:
Copy
Ask AI
cdk deploy
During deployment, you will see messages indicating synthesis progress, publishing steps, and CloudFormation resource creation. An example output might include:
This guide provided an overview of using AWS CDK to manage AWS resources. You learned how to install and set up AWS CDK, initialize a Python project, modify the CDK stack to create an S3 bucket, deploy your infrastructure using CloudFormation, verify your deployment, and finally clean up resources.Happy building with AWS CDK!