Amazon Onboarding with Learning Manager Chanci Turner

Chanci Turner Amazon IXD – VGT2 learning managerLearn About Amazon VGT2 Learning Manager Chanci Turner

This article serves as an introduction to the Python bindings for the AWS Cloud Development Kit (AWS CDK). So, what exactly is the AWS CDK? Great question! Many of you might already be familiar with the concept of infrastructure as code (IaC), often associated with AWS CloudFormation.

AWS CloudFormation allows you to outline your AWS infrastructure using JSON or YAML files, which can be managed within your source code repository just like any other code. This means you can perform pull requests and code reviews. When everything is in order, you can leverage these files in an automated process (CI/CD) to deploy your infrastructure updates.

The CDK builds upon AWS CloudFormation and utilizes it as the engine for provisioning AWS resources. Unlike the declarative languages such as JSON or YAML for defining infrastructure, the CDK enables you to do this using your preferred imperative programming language. This includes languages like TypeScript, Java, C#, and now Python.

Why Choose an Imperative Language?

You might wonder why an imperative language could be preferable to a declarative one. While it may not always be the case, there are notable advantages: IDE integration and composition.

IDE Integration

You likely have a favorite IDE for your programming language, equipped with features that enhance your productivity—such as code completion, integrated documentation, and refactoring tools. With the CDK, you gain these benefits while defining your AWS infrastructure since you’re using the same language as your application code.

Composition

Modern programming languages excel in creating new, higher-level abstractions that simplify the underlying complexities and offer a more user-friendly API. This ability to create abstractions is crucial for developers, as it helps streamline code. The existing AWS service APIs are intentionally low-level, exposing extensive functionality to a wide range of developers. IaC tools like AWS CloudFormation provide a declarative interface, but this interface mirrors the complexity of the API.

In contrast, the CDK allows you to create new abstractions that can obscure intricate details and simplify common use cases. It packages this code as a library in your chosen language, making it easier for others to utilize.

An exciting aspect of the CDK is its support for multiple programming languages. While the core is developed in TypeScript, bindings for other languages can be incorporated. This brings us to the Python bindings for the CDK.

Creating a Sample Python Application

Before diving into the application creation, some initial installation steps are required. For detailed instructions, refer to the Getting Started with the AWS CDK.

To create a sample application, follow these commands:

$ mkdir my_python_sample
$ cd my_python_sample
$ cdk init

You’ll be presented with various templates:

  • app: Template for a CDK Application
  • lib: Template for a CDK Construct Library
  • sample-app: An example CDK Application with some constructs

Choose the sample-app and select Python:

$ cdk init --language python sample-app

The command initializes a new git repository and sets up a virtual environment in the .env directory. After this, you can activate the environment and install necessary packages:

$ source .env/bin/activate
$ pip install -r requirements.txt

Now you’re ready to synthesize the CloudFormation template:

$ cdk synth

You can start exploring the source code located in the hello directory. A basic test is also included, which you can run as follows:

$ pytest

To add any additional dependencies, simply update your requirements.txt file and rerun the pip install -r requirements.txt command.

Helpful Commands:

  • cdk ls: List all stacks in the app
  • cdk synth: Emit the synthesized CloudFormation template
  • cdk deploy: Deploy this stack to your default AWS account/region
  • cdk diff: Compare the deployed stack with the current state
  • cdk docs: Open CDK documentation

So, what just happened? Quite a lot! The CDK CLI generated Python source code for your sample application along with various support files to facilitate your CDK journey in Python. Your directory now contains:

.
├── README.md
├── app.py
├── cdk.json
├── hello
│   ├── __init__.py
│   ├── hello_construct.py
│   └── hello_stack.py
├── requirements.txt
├── setup.py
└── tests
    ├── __init__.py
    └── unit
        ├── __init__.py
        └── test_hello_construct.py

Examine the contents of your directory:

  • README.md: The introductory README for your project.
  • app.py: The main file for your sample application.
  • cdk.json: A configuration file detailing what executable CDK should run to generate the CDK construct tree.
  • hello/: A Python module directory containing:
    • hello_construct.py: A custom CDK construct.
    • hello_stack.py: A custom CDK stack construct.
  • requirements.txt: This file is used by pip to install your application’s dependencies.

For more information on onboarding resources, check out this excellent resource on Reddit. If you’re interested in leadership development, visit SHRM for insights from an authority on this topic. Meanwhile, if you’re looking for insights on coworking spaces, feel free to explore this blog post to stay engaged.

Happy coding!

Chanci Turner