Spinning up EC2 Instances on AWS Using CDK for Terraform

In this post, you learn how to use CDK for Terraform to spin up Virtual Machines in AWS. We have already done a similar tutorial using AWS CDK, but this one uses CDK for Terraform, which is slightly different. This tutorial might incure costs in your account, so don’t forget to run “cdktf destroy” at the end.

First thing’s first, let’s initialize an empty CDK typescript project:

cdktf init

This brings a couple of interview-like questions which enables you to initialize the project. First, it asks you whether you want your statefile locally or in the cloud. For this demo, we opt for local version, but if you work within a team, or use this production workload, cloud variant could be the choice. Then with arrow keys you can choose the programming language of your choice. You can choose Typescript, Csharp, Java, go and many more.

Slightly different to AWS CDK, we then go to cdktf.json to declare the providers for our project. You add aws provider to the terraformProviders:

{
  "language": "typescript",
  "app": "npm run --silent compile && node main.js",
  "terraformProviders": ["aws@~> 2.0"],
  "terraformModules": [],
  "context": {
    "excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
  }
}

This is an optional step, but go ahead and run “npm run get” which sets up some code for the providers, in our case, azure.

Open main.ts and add the following import statements to before class declaration:

import { Construct } from “constructs”; import { App, TerraformStack } from “cdktf”; import { AwsProvider, Instance, Vpc, Subnet } from “./.gen/providers/aws”;

Then we specifiy our provider:

new AwsProvider(this, “aws”, { region: “eu-central-1”, });

Then we create our VPC and specify our CIDR range:

const vpc = new Vpc(this, 'MyVPC',{
  cidrBlock:"10.0.0.0/16"
});

After our VPC, we create our subnet(s):

const subnet = new Subnet(this,'subnet-a',{
  cidrBlock:"10.0.0.0/28",
  vpcId:vpc.id,
});

As this is a demo, I opted for a small number of IPs (16). A useful website for finding the right IP adress space is https://cidr.xyz/ Moreover, I did not specify any availability zones, which is a required for having a highly available applicaiton, so you can add availabilityZone attribute to the subnet. Bear in mind that this could require modificaions to the rest of the tutorial.

After that I initialize the EC2 Instance using the Amazon Linux AMI:

new Instance(this, "Linux2Instance", {
  ami: "ami-07df274a488ca9195",
  instanceType: "t2.micro",
  subnetId:subnet.id 
  ,
});

You can find the right AMI and instance type from AWS Console. Also, if you import the EC2 construct, you might get some nice intellisence with your code, instead of typing the amis as string.

Now we are ready to verify our cdk stack. First run ‘npm run build’ which compiles typescript to javascript. Then run cdktf synth. This should give a cloud formation template. If it returns an error, go to previous steps and verify your work.

The source code on github: https://github.com/pedramha/ec2-cdktf

This project is maintained by pedramha