Infrastructure
Getting started with Terraform CDK and TypeScript
In this post, I will do a walkthrough about provisioning AWS resources using Terraform CDK.
1. Install Terraform CDK npm module
npm install -g cdktf-cli
2. Initialize your Terraform CDK repository
mkdir thulir-infra
cd thulir-infra
cdktf init --template="typescript" --local
The output will look like this,
% cdktf init --template="typescript" --local Note: By supplying '--local' option you have chosen local storage mode for storing the state of your stack. This means that your Terraform state file will be stored locally on disk in a file 'terraform.tfstate' in the root of your project. We will now set up the project. Please enter the details for your project. If you want to exit, press ^C. Project Name: (default: 'thulir-infra') . . . added 2 packages, and audited 54 packages in 952ms 5 packages are looking for funding run `npm fund` for details found 0 vulnerabilities . . . . npm install @cdktf/provider-null You can also build any module or provider locally. Learn more https://cdk.tf/modules-and-providers ========================================================================================================
3. Install AWS Provider
npm install @cdktf/provider-aws --save
4. Synthesize the application
% cdktf synth
Generated Terraform code for the stacks: thulir-infra
5. Get the Terraform Plan
% cdktf diff
Stack: thulir-infra
Diff: 0 to create, 0 to update, 0 to delete.
%
6. Add some AWS resources
Open the main.tf and add a Kinesis stream.
import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import { AwsProvider, KinesisStream } from "@cdktf/provider-aws";
class MyStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new AwsProvider(this, "aws", {
region: "us-east-1",
});
new KinesisStream(this, "thulir-iot-data-collector", {
name: "thulir-iot-data-collector",
shardCount: 1
})
}
}
const app = new App();
new MyStack(app, 'thulir-infra');
app.synth();
7. Generate the plan, validate and apply
% cdktf diff
Stack: thulir-infra
Resources
+ AWS_KINESIS_STREAM thulir-iot-data-col aws_kinesis_stream.thulir-iot-data-coll
lector ector
Diff: 1 to create, 0 to update, 0 to delete.
%
% cdktf apply
Deploying Stack: thulir-infra
Resources
AWS_KINESIS_STREAM thulir-iot-data-col aws_kinesis_stream.thulir-iot-data-coll
lector ector
Summary: 1 created, 0 updated, 0 destroyed.
%
8. Destroy the resources
% cdktf destroy
Destroying Stack: thulir-infra
Resources
AWS_KINESIS_STREAM thulir-iot-data-col aws_kinesis_stream.thulir-iot-data-coll
lector ector
Summary: 1 destroyed.
%
Continue Reading
-
AWS3 years ago
How to install NodeJS in Amazon Linux 2
-
Infrastructure3 years ago
How to test CPU, Memory and File System Performance using Sysbench
-
AWS3 years ago
How to install .Net 6 in Amazon Linux 2
-
Uncategorized3 years ago
How to install Docker in Amazon Linux 2?
-
Infrastructure3 years ago
How to get Linux OS Information using uname command
-
Infrastructure3 years ago
How to reproduce CVE-2021-44228 (Log4J vulnerability), patch it, and validate the fix
-
Uncategorized3 years ago
Everything, Everywhere, All At Once
-
Linux3 years ago
How to install git in Amazon Linux 2