In part 2 of the AWS CDK & Python series, which you can find here, you learned how to create a new CDK app, browse the files in the new app, and run the app. In part 3, which is the final part of this series, you’re going to learn how to create and automate a real-world AWS S3 bucket using the AWS CDK.
To follow along with this blog post, you should have the following:
- An AWS account. If you don’t have one, you can create a free tier account here.
- Read the first and second blog post on in the AWS CDK & Python series, which you can find here and here.
Opening the CDK Project
In Part 2, you created a new CDK project using Python. You can use the same CDK project as you didn’t modify it at all and it was left as the original template.
Open up the CDK project in VS Code, or whichever code editor you’re using. If you deleted the project or want to start fresh, you can run the following command:
cdk init app --language python
Next, open up the project directory and the
Notice under the
__init__ method there’s a command saying the code you define gets added there. That means that any CDK code that you’re writing to utilize S3, or any other AWS service, goes here. Realistically and in many cases, this is the only section you need to edit to deploy the CDK project.
Writing the CDK S3 Code
Now that you have the CDK project open, it’s time to start writing the S3 code for the project.
The first thing that you’re going to need to do is import from the
aws_cdk library, the
aws_s3 construct library. The construct library contains all of the different ways to interact with the S3 service from the AWS CDK.
aws_s3 library with the following
from aws_cdk import aws_s3
Next, you’ll need to use the
aws_s3 library to start working with buckets for S3. To do that, you’ll utilize the
Bucket class in the
aws_s3 library. The
Bucket class will take two arguments:
- The name of the new S3 bucket you’re creating
For example, here’s a variable that has a variable name of
bucket and the value is the
aws_s3 library utilizing the
Bucket class. The bucket name argument is passed in, which defines that the bucket name will be s3_lambda_monitoring.
bucket = aws_s3.Bucket(self, "s3_lambda_monitoring")
bucket variable is defined, you’ll want to ensure that you see the output of the bucket on the terminal to ensure the bucket was created successfully. To do so, you can use the
[stack.py](<http://stack.py>) file should look like the below:
from aws_cdk import core as cdk # For consistency with other languages, `cdk` is the preferred import name for # the CDK's core module. The following line also imports it as `core` for use # with examples from the CDK Developer's Guide, which are in the process of # being updated to use `cdk`. You may delete this import if you don't need it. from aws_cdk import core from aws_cdk import aws_s3 class Part3Stack(cdk.Stack): def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) bucket = aws_s3.Bucket(self, "s3_lambda_monitoring") print(bucket)
Deploying the CDK App
Once the CDK app is written and the AWS S3 bucket is ready to be created, it’s time to deploy the application.
In the directory where the CDK project is, run the following:
You should see an output similar to the screen below.
You have now successfully deployed a new S3 bucket to AWS using the CDK!
As mentioned in part 1 and part 2 of the AWS CDK & Python series, the CDK is an extremely powerful way of automating workloads in the cloud. If you don’t want to use an Infrastructure-as-Code platform, or if you want to combine both IaC and general-purpose programming, the AWS CDK is a great implementation.
In this 3-part series you learned:
- What the AWS CDK is
- Why it’s important
- Why it’s changing the way we automate today
- How to create your first CDK project
- How to work with AWS services using the CDK
I hope you enjoyed the series!