Here is my CDKTF (TypeScript) code approach to enable a CloudWatch alarm that triggers based on CPU/Memory utilization for an ECS-Fargate service. Currently, I have hard-coded the cluster and service names in the code. However, I am looking for a way to dynamically retrieve and update the service name while the code is running. I have explored the "Data Sources" option but haven't found a solution yet. I would appreciate any guidance on this matter.
import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
//import { cloudwatchMetricAlarm, snsTopic, snsTopicSubscription, iamRole, iamRolePolicy } from './.gen/providers/aws';
import { AwsProvider } from './.gen/providers/aws/provider';
import { SnsTopic } from './.gen/providers/aws/sns-topic';
import { SnsTopicSubscription } from './.gen/providers/aws/sns-topic-subscription';
import { IamRole } from './.gen/providers/aws/iam-role';
import { IamPolicy } from './.gen/providers/aws/iam-policy';
import { CloudwatchMetricAlarm } from './.gen/providers/aws/cloudwatch-metric-alarm';
import { dataAwsEcsService } from './.gen/providers/aws';
class MyStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new AwsProvider(this, 'Aws', {
region: 'us-east-1',
});
const snsTopic = new SnsTopic(this, 'SnsTopic', {
name: 'ecs-cpu-notification',
});
new SnsTopicSubscription(this, 'SnsTopicSubscription', {
topicArn: snsTopic.arn,
//topic: snsTopic.id,
protocol: 'email',
endpoint: 'XXXXXemailaddress',
});
new IamRole(this, 'IamRole', {
name: 'cloudwatch-sns-access',
assumeRolePolicy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: {
Service: 'cloudwatch.amazonaws.com',
},
},
],
}),
});
//const servicename = new dataAwsEcsService(this, "servicename", {
} );
new IamPolicy(this, 'IamRolePolicy', {
name: 'publish',
//role: iamRole.id,
policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Action: 'sns:Publish',
Effect: 'Allow',
Resource: snsTopic.arn,
},
],
}),
});
// CloudWatch alarm for CPU utilization
new CloudwatchMetricAlarm(this, 'CloudwatchMetricAlarm', {
alarmName: 'ecs-cpu-utilization',
comparisonOperator: 'GreaterThanOrEqualToThreshold',
evaluationPeriods: 2,
metricName: 'CPUUtilization',
namespace: 'AWS/ECS',
period: 120,
statistic: 'Average',
threshold: 50,
alarmActions: [snsTopic.arn],
dimensions: {
ClusterName: 'XXXX-ClusterName',
ServiceName: 'XXXX-ServiceName',
},
});
new CloudwatchMetricAlarm(this, 'MemoryCloudwatchMetricAlarm', {
alarmName: 'ecs-memory-utilization',
comparisonOperator: 'GreaterThanOrEqualToThreshold',
evaluationPeriods: 2,
metricName: 'MemoryUtilization',
namespace: `AWS/ECS`,
period: 120,
statistic: 'Average',
threshold: 40,
alarmActions: [snsTopic.arn],
dimensions: {
ClusterName: 'XXXX-ClusterName',
ServiceName: 'XXXX-ServiceName',
},
})
}
}
const app = new App();
new MyStack(app, 'aws');
app.synth();
I have tried the Terraform output / Python approach with boto3 but all these method seems far fetch. According to Terraform documentation we should be able to use Data Source. Am expecting if someone can guide me to data source with cdktf.