Member-only story
AWS Cloud Development Kit (CDK): Create a VPC with EC2 Instance

Infrastructure as Code (IaC)
When creating your cloud infrastructure, it’s easy to use the AWS Console to quickly provision resources. The problem begins when you need to duplicate those resources in other Regions, when you need to make changes to your environments, or if there is a disaster recovery scenario.
Infrastructure as Code solves this problem by doing exactly what the name implies, it turns your infrastructure into code. You treat the code just like you would any other code with source control and versioning. IaC allows you to automate DevOps best practices by starting a git repository and deploying through a CI/CD pipeline. IaC allows you to scale your infrastructure worldwide and easily update changes.
AWS CloudFormation has been the main IaC service for AWS up to this point, however there are some potential issues. An example would be that as your environment grows, your JSON/YAML CloudFormation files will become larger and harder to maintain. Plus at the end of the day it’s not a programming language and easy to miss syntax or formatting mistakes in your Templates.
What is AWS Cloud Development Kit (CDK)?
AWS CDK was announced in 2019 and is an AWS Infrastructure as Code service that allows developers to use their favorite programming language to define their cloud infrastructure. AWS CDK allows for Integrated Development Environment (IDE) integration, supports multiple programming languages, allows for syntax checks, autocompletion and more. AWS CDK also allows for a higher level of abstraction where as AWS CloudFormation requires low level details, resulting in AWS CDK code being much smaller than an AWS CloudFormation Template. As an example I completed essentially this same project for Level Up In Tech using AWS CloudFormation which ended up being 140 lines of YAML, whereas this AWS CDK project only took 23 lines of code.
AWS Cloud9 Creation
I decided to use AWS Cloud9 for this project but feel free to use your preferred IDE. AWS Cloud9 already has AWS CLI installed so you will need to remember to install if using a different IDE. If you are using a different IDE then you can skip to the…