Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Last active October 14, 2021 05:58
Show Gist options
  • Save neilkuan/4eebdfdb1d983934fee0c8b088944856 to your computer and use it in GitHub Desktop.
Save neilkuan/4eebdfdb1d983934fee0c8b088944856 to your computer and use it in GitHub Desktop.
ec2-constructs.ts
import * as ec2 from '@aws-cdk/aws-ec2';
import * as sns from '@aws-cdk/aws-sns';
import * as cdk from '@aws-cdk/core';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as cw_actions from '@aws-cdk/aws-cloudwatch-actions';
export interface AlarmInstanceProps {
readonly UserData?: ec2.UserData;
readonly vpc?: ec2.IVpc;
readonly topic?: sns.Topic;
readonly notifyMail?: string[];
}
export class AlarmInstance extends cdk.Construct {
public instance: ec2.Instance;
public topic: sns.Topic;
constructor(scope: cdk.Construct, id: string, props: AlarmInstanceProps = {}) {
super(scope, id)
this.instance = new ec2.Instance(this, 'Instance', {
instanceName: this.node.id,
instanceType: new ec2.InstanceType('t3.micro'),
machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
userData: props.UserData,
vpc: props.vpc ?? new ec2.Vpc(this, 'VPC', { natGateways: 1 })
});
const metrics = new cloudwatch.Metric({
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensionsMap: {
InstanceId: this.instance.instanceId
},
});
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
alarmDescription: 'Alarm for instance',
metric: metrics,
threshold: 50,
evaluationPeriods: 1,
});
this.topic = props.topic ?? new sns.Topic(this, 'Topic');
alarm.addAlarmAction(new cw_actions.SnsAction(this.topic));
if (props.notifyMail){
props.notifyMail.forEach((mail, index) => {
new sns.Subscription(this, `addSubscription${index}`,{
topic: this.topic,
protocol: sns.SubscriptionProtocol.EMAIL,
endpoint: mail,
});
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment