DEV Community

Cover image for Cloud native apps with AWS CDK, Python and VSCode Dev Containers
Tom Harvey
Tom Harvey

Posted on

Cloud native apps with AWS CDK, Python and VSCode Dev Containers

When starting a new project I reach for Python and AWS.

To help me build cloud-native Python apps, I've been using AWS's CDK. This "Cloud Development Kit" allows me to create storage or compute using Python and have my infrastructure live alongside my domain logic.

And I write all of this in VSCode, using their "Developer Containers" features to keep things standardised and 'simple'.

When I use a Dev Container, I don't need to worry about having python installed on my laptop. I don't need to fuss around with virtualenvs. My laptop can be left with the base OS and no funny business with Brew or Pyenv or Conda or whatever else I need to experiment with.

I just need my laptop to have:

  1. Docker Desktop (or an equivalent)
  2. VSCode

And that's it.

I can now write apps in whatever Python version I want. And you'll see how easy it would be to extend to TypeScript, Go, Rust or whatever.

So, install Docker Desktop (or an equivalent) and VSCode and then these few simple steps will get you building a cloud native app in no time.

What are "Dev Containers"?

These are machines running inside of your "host" machine. The "host" machine is your laptop (or desktop).

Self contained

Dev containers are self-contained environments. They contain Python and all of the other dependancies that you need to get the work done.

One doesn't conflict with another. So, if one project needs Python 3.7 and another needs 3.12 - no big deal. For all that you're using your one laptop these Dev Containers might as well be separate machines.

Your dev container will exist on your laptop, and your desktop in an identical way. Or on your colleague's laptop in an identical way. Or on a big powerful AWS instance in an identical way.

So, team mates can have identical environments. And, as a solo-coder you can rough out the easy stuff on your laptop and then easily run that dev container on an AWS instance with 96 CPUs 768GB of RAM, 8 GPUS and a 100Gbps network connection (if you have the $18.30/hour to spend on such hardware).

The point is, your developer environment is the same regardless of the underlying hardware. It's repeatable and reliable. It doensdoesn't rely on you remembering to brew install foo and if there is a version conflict you need to perform some magical steps.

Finally, these dev containers can be scrapped easily when you're done with some experiment; without leaving a trail of old versions of Python on your 'host machine'.

The template project

I have a template project at https://github.com/tomharvey/python-aws-cdk-devcontainer

This is really just one file!

Inside a directory called .devcontainer i have a devcontainer.json file.

It very very heavily commented, but as an overview, this file tells VSCode that you want a certain flavour of developer environment.

  1. Python 3.12 on Debian Bullseye - yes, even if you're on a mac or on windows.
  2. With some additional tools installed:
    1. AWS CLI so we can interact with AWS
    2. AWS CDK so we can build infrastructure
    3. AWS SSO Util so we can login to AWS using SSO (if we need)
  3. It's setup so we share the AWS Config folder on our machine with this new developer environment. That way our AWS Auth can be shared between the host machine and the dev environment.

Usage

1. Get my template

Go to https://github.com/tomharvey/python-aws-cdk-devcontainer and select the "Use this template" option to "create a new repository".

Image description

2. Create a new Project from the tempalte.

You can now setup your new project as a new GitHub repository.

  1. Give it a name. "My Awesome new App" or "Python CDK Playground"
  2. Keep it private or make it public. Your decision.

Image description

3. Wait for it ....

Image description

4. Copy that Github URL

You now have a new repo in your own github account. Time to open that in VSCode. So, copy the repo URL using SSH or HTTPS - however you like to interact with GitHub.

Image description

5. Open VSCode

Now, go into VSCode and ensure you have installed the Dev Containers extension. See here to install that extension

6. Use Dev Containers Extension

And bring up the "Command palette" from the "View" menu, or by ⌘-shift-P (ctrl-shift-p on Windows) and within that "Command palette" type "dev con clone" so it offers you the option you want:

Dev Containers: Clone Repository in Named Container Volume...

Click on that.

Image description

7. Clone the github repo

Then paste in the GitHub repo URL you copied in step 3 and hit the enter button.

Image description

And then select the main branch (which is likely the only option available to you)

Image description

Finally enter a new "volume name" - this is what Docker will name the volume which stores all your code - use the same name as the git hub repo.

Image description

8. Use your new code environment

After a few minutes (depending on your network connection) you will have a fully running Python environment with all of the AWS and CDK dependancies installed and ready to go.

Setup the name of your project. In the template I've named it "My awesome developement environment", but you will want to make it your own.

Image description

Open a terminal from the "terminal" menu - or with the ctrl shift ~ keyboard shortcut. And run python --version to see that you do indeed have python running as expected at version 3.12

Image description

Conclusions

You can now run the cdk command to setup a new cdk app and follow along with the AWS CDK Workshop or use the CDK API reference to build a cloud native app.

Your developer environment is entirely separate from your "host machine". It's transportable, and its definition lives alongside your code. Any new developer can pick up your app and use VSCode to immediately get an exact copy of your developer environment.

And, you didn't need to think one time about installing Python, setting up a virtualenv or whether you should use brew install or apt install or whatever when things went wrong.

Finally - while this will get you up and running, the devcontainer.json file is heavily commented so it should be self-explanitory. Please read through it and make it your own. Fork it. Raise a Pull Request to add or change things. And let me know how you prefer your developer environment.

Top comments (0)