backend-and-orchestration-t.../code/kubernetes/python-cdk/python/VPC_example
2024-11-17 17:03:20 -08:00
..
imgs merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
vpc_example merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
app.py merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
cdk.json merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
README.md merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
requirements.txt merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00
setup.py merge files from the blockchain infra repo (#59) 2024-11-17 17:03:20 -08:00

Setting up a VPC with CDK in Python

AWS CDK is a very neat way to write infrastructure as code, enabling you to create and provision AWS infrastructure deployments predictably and repeatedly.

You choose your favorite language to code what resources (stacks) you want, and CDK synthetizes them to CloudFormation and helps you to deploy them to AWS.

In this example we see how to setup a VPC using CDK in Python.

Install AWS CDK

Follow this instructions.

Create a virtual environment and install dependencies:

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

Note: If you are starting from a blank project with cdk init app --language=python instead, you will we need to manually install resources, such as pip install aws_cdk.aws_ec2.

Define You VPC

Define how you want your VPC in the file vpc_example/vpc_example_stack.py:

class VpcExampleStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        vpc = aws_ec2.Vpc(self, "MiaVPCTest", cidr="10.0.0.0/16", max_azs=3)

Create synthesized CloudFormation template

cdk synth

You can check what changes this introduces into your AWS account:

cdk diff --profile <AWS PROFILE>

Deploy to AWS

If everything looks right, deploy:

cdk deploy --profile <AWS PROFILE>

To check all the stacks in the app:

cdk ls

Clean up

To destroy/remove all the newly created resources, run:

cdk destroy --profile <AWS PROFILE>