Skip to content

ahmadalibagheri/cdktf-typescript-aws-ec2

Repository files navigation

Documentation

  • Explore the Terraform for Terraform CLI >= v1.0+
  • Explore the Nodejs for npm CLI >= v14+
  • Explore the Yarn for Yarn CLI >= v1.21 (optional - NPM will work as an alternative)
  • Explore the CDK for cdktf CLI

Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.

$ export AWS_ACCESS_KEY_ID=AAAAAA
$ export AWS_SECRET_ACCESS_KEY=AAAAA

typescript-aws-ec2

A CDK for Terraform application in TypeScript for EC2 configuraiton.

Usage

Install project dependencies

yarn install

Generate CDK for Terraform constructs for Terraform provides and modules used in the project.

cdktf get

You can now edit the main.ts file if you want to modify any code.

vim main.ts
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider, ec2 , vpc} from "./.gen/providers/aws";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new AwsProvider(this, "aws", {
      region: "us-east-1",
    });

    const instancesg = new vpc.SecurityGroup(this, "instancesg", {
      name: "CDKtf-TypeScript-Demo-sg",
      description: "Allows traffic to the instance.",
      ingress: [
        {
          protocol: "tcp",
          fromPort: 80,
          toPort: 80,
          cidrBlocks: ["0.0.0.0/0"],
        },
        {
          protocol: "tcp",
          fromPort: 22,
          toPort: 22,
          cidrBlocks: ["0.0.0.0/0"],
        },
        {
          protocol: "tcp",
          fromPort: 443,
          toPort: 443,
          cidrBlocks: ["0.0.0.0/0"],
        },
      ],
      egress: [
        {
          protocol: "-1",
          fromPort: 0,
          toPort: 0,
          cidrBlocks: ["0.0.0.0/0"],
        },
      ],
      tags: {
        Name: this.node.tryGetContext("Name"),
        Team: this.node.tryGetContext("Team"),
        Company: this.node.tryGetContext("Company"),
      },
    });

    new ec2.Instance(this, "compute", {
      ami: "ami-03d315ad33b9d49c4", //Ubuntu Server 20.04 LTS (HVM)
      instanceType: "t2.micro",
      keyName: "DevOps",     //You should create Keyname manually before running code
      vpcSecurityGroupIds: [instancesg.id],
      tags: {
        Name: this.node.tryGetContext("Name"),
        Team: this.node.tryGetContext("Team"),
        Company: this.node.tryGetContext("Company"),
      },
    });

  }
}

const app = new App({ context: { Name: "CDKtf-TypeScript-Demo" , Team: "DevOps", Company: "Your company"} });
new MyStack(app, "typescript-aws");
app.synth();

Compile the TypeScript application

tsc

At this step you can run code with two different way:

The first way:

Generate Terraform configuration

cdktf synth

The above command will create a folder called cdktf.out that contains all Terraform JSON configuration that was generated.

Run Terraform commands

cd cdktf.out
terraform init
terraform plan
terraform apply

The second way:

Run cdktf commands

cdktf deploy

About

AWS ec2 and security configuration with typescript and cdktf

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published