Creating a local environment to develop on AWS CDK using Docker and VSCode

carlosrochacardoso

Carlos A R Cardoso

Posted on March 9, 2021

Creating a local environment to develop on AWS CDK using Docker and VSCode

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.

About AWS CDK

Anchored in the concept of infrastructure as code (IaC), the AWS CDK allows you to create resources in the AWS cloud, using languages ​​such as Python, Java, JavaScript, C #, or TypeScript. If you already use CloudFormation or Terraform, it's worth checking out and adding to your toolbox.

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?

Why containers?

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 ;)

Hands on

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"!

Prerequisites

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.

Git

With Docker, we will create the image and start the container.

Docker

With VSCode, we will connect to the container to develop our applications.

VSCode

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.

AWS-CLI

Creating the image of the Docker container with AWS CDK

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
Enter fullscreen mode Exit fullscreen mode

Go to the docker-aws-cdk folder and open the Dockerfile file for editing.

cd docker-aws-cdk && sudo gedit Dockerfile
Enter fullscreen mode Exit fullscreen mode

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 1.91.0.

The file line should look like this:

ENV AWS_CDK_VERSION=1.91.0
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

where:

-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
Enter fullscreen mode Exit fullscreen mode

Connecting VSCode to our AWS-CDK environment

Open VSCode and install the python extensions and remote-containers:

ms-vscode-remote.remote-containers
ms-python.python
Enter fullscreen mode Exit fullscreen mode

Alt Text

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.

Alt Text

A new VSCode session connected to the aws_cdk container will open.

Starting the project

In the top menu, click on Terminal and New Terminal.
Navigate to the project folder.

cd /home/projeto/
Enter fullscreen mode Exit fullscreen mode

Start a new Python CDK project.

cdk init app --language python
Enter fullscreen mode Exit fullscreen mode

Run the command below to check the created project structure.

ls -la
Enter fullscreen mode Exit fullscreen mode

Alt Text

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.

Alt Text

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.

Wrapping up

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
Enter fullscreen mode Exit fullscreen mode

See you next time!

💖 💪 🙅 🚩
carlosrochacardoso
Carlos A R Cardoso

Posted on March 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related