Containers are humanity's greatest invention after the transistor, and anyone who disagrees is wrong. They make life easier, which even complicates. When you realize, you're already finding an excuse to use it where you shouldn't.
Exaggerations aside, I think that is not the case here. I intend to show in this brief tutorial how to use them to create a local environment ready to develop your apps and stacks based on AWS CDK - Cloud Development Kit. We are not going to create the resources on AWS. That is not the goal. We will prepare the environment to start your development.
Recently collaborating on data lakes implementation projects in the AWS cloud, I have been using the CDK as an IaC tool to automate the creation of resources and environment, whether in roles, jobs glue, or lake formation configurations creation. I can't imagine a life doing it manually on the console.
Has anyone ever worked or works like this?
You could indeed install AWS CDK and its prerequisites directly on your PC's operating system. However, I believe that the method suggested in this tutorial is faster. Also, you could use this method to facilitate the version upgrade or even to create and maintain several simultaneous CDK environments, in different versions, without one version interfering or impacting the other.
Believe me, almost every day, AWS releases a new version.
Finally, part of this method could be adapted and used in a CI / CD pipeline.
It's a big deal ;)
All the steps described in this tutorial were performed on Ubuntu 20.04, but this is not a prerequisite. Adapting a few steps and installing the software packages in the required version, you can use another S.O. of your preference.
Without further ado, let's get this environment ready to "Hello World"!
Before performing step 1, you will need Git, Docker, VSCode, and AWS-CLI installed. If you don't have these applications yet, I suggest following the official links below. The idea is simple.
With Git, we will download the source code for our AWS-CDK Docker container image.
With Docker, we will create the image and start the container.
With VSCode, we will connect to the container to develop our applications.
With AWS-CLI, you can set up your AWS account and get the environment ready to deploy your application in the cloud. Remember that for that, of course, you will need an AWS account.
On the operating system, open the terminal and run the following command to download the source code
sudo git clone https://github.com/contino/docker-aws-cdk.git
Go to the
docker-aws-cdk folder and open the Dockerfile file for editing.
cd docker-aws-cdk && sudo gedit Dockerfile
In the first lines, find the section containing the variable
ENV AWS_CDK_VERSION and change it to the desired version number. As I write this tutorial, the current version is
The file line should look like this:
Save and clode the file.
You can edit and customize this Dockerfile in several other ways, as needed. Take a look at the project repository for more information.
To create the image of the container run:
sudo docker build -t contino/aws-cdk:1.91.0 .
Now it's time to start our service. Run the command:
sudo docker run -itd -v ~/.aws:/root/.aws -v /home/projeto:/home/projeto --name aws_cdk contino/aws-cdk:1.90.0 bash
-v ~/.aws:/root/.aws : maps the AWS credentials from the local machine to the container. For this step, you must configure your credentials after installing the AWS-CLI. Although it is not essential for this tutorial since we will not perform any deployment, I recommend following it to leave your environment ready.
-v /home/projeto:/home/projeto : maps the local project directory to a project directory internal to the container. Here you can change the name of the local folder as needed. Pay attention to the path names to locate the project on your local machine and in the container.
To check if the container is running and that the CDK is installed in the desired version, execute:
sudo docker exec aws_cdk cdk --version
Open VSCode and install the python extensions and remote-containers:
With the extensions installed, we will connect the VSCode to our container.
Click the green icon in the bottom left corner of the screen.
A menu will appear in the center of the screen, next to the title bar.
Select the Attach to Running Container option and click on the container name.
A new VSCode session connected to the aws_cdk container will open.
In the top menu, click on Terminal and New Terminal.
Navigate to the project folder.
Start a new Python CDK project.
cdk init app --language python
Run the command below to check the created project structure.
You can also browse your container or project folders through the VSCode interface. From the main menu, click File and Open Folder.
Type the path, example
/home and click OK. The folder hierarchy should be displayed in the left pane.
You now have the structure of an AWS-CDK Python project. If this is your first contact, I recommend following the official Developer Guide to understand the concepts and build your first stack.
Disconnect the VSCode from the container by clicking on the green icon in the lower left corner and on the Close Remote Connection option.
At the terminal of the operational system, end the execution of the container with the command:
sudo docker stop aws_cdk
See you next time!