TerraConstructs
Install TerraConstructs
Let's convert MyStack from extending basic TerraformStack to extend a more powerful AWS specific AwsStack.
First, install the terraconstructs.dev package into the workshop.
npm install terraconstructsNow, Replace the TerraformStack from cdktn with TerraConstruct's AwsStack.
This requires us to provide additional metadata such as:
- providerConfig: AWS configuration such as
region. - gridUUID: A Unique Identity that is decoupled from environment name or product/component identity.
- environmentName: A user friendly description of the environment we are deploying into.
import { App } from "cdktn";
import { Construct } from "constructs";
import { AwsStack, AwsStackProps } from "terraconstructs/lib/aws";
class MyStack extends AwsStack {
constructor(scope: Construct, id: string, props: AwsStackProps) {
super(scope, id, props);
// define resources here
}
}
const app = new App();
new MyStack(app, "cdk-workshop", {
environmentName: "dev",
gridUUID: "cdk-workshop-dev", // Ideally this is an autogenated UUID instead
providerConfig: {
region: "us-east-1",
},
});
app.synth();AWS Resources
We are now ready to use following powerful features from the TerraConstructs aws/notify module:
- SQS Queue (
new Queue) - SNS Topic (
new Topic) - Subscribe the queue to receive any messages published to the topic (
topic.addSubscription)
import { App } from "cdktn";
import { Construct } from "constructs";
import { Duration } from "terraconstructs/lib";
import { AwsStack, AwsStackProps } from "terraconstructs/lib/aws";
import { Queue, Topic, subscriptions } from "terraconstructs/lib/aws/notify";
class MyStack extends AwsStack {
constructor(scope: Construct, id: string, props: AwsStackProps) {
super(scope, id, props);
const queue = new Queue(this, "CdkWorkshopQueue", {
visibilityTimeout: Duration.minutes(5),
});
const topic = new Topic(this, "CdkWorkshopTopic");
topic.addSubscription(new subscriptions.SqsSubscription(queue));
}
}
const app = new App();
new MyStack(app, "cdk-workshop", {
environmentName: "dev",
gridUUID: "cdk-workshop-dev",
providerConfig: {
region: "us-east-1",
},
});
app.synth();