Day 27 - 使用 CDK 创建 CloudWatch 也能发送 Alarm 到 LINE 的系统

前几天说了很多建置系统的方法但是对於监控系统没有太多的说明,今天就来一个 CloudWatch 传送 Alarm 到 LINE 的教学吧!先给大家看看今天的成品

https://ithelp.ithome.com.tw/upload/images/20201012/20117701qwE7eaxFmE.jpg

目标

今天的目标是要模拟我们开的 EC2 发生 CPU Utilization 过高触发 CloudWatch Alarm 发送告警讯息给 LINE Notify 整个触发流程是

  1. EC2 CPU Utilization 过高触发 CloudWatch Alarm
  2. CloudWatch Alarm 触发 SNS Action
  3. SNS Action 触发 Lambda
  4. Lambda 发送 HTTP Request 把讯息带给 LINE Notify

所以需要创建的服务会有

  1. EC2
  2. CloudWatch Alarm
  3. SNS Action
  4. Lambda

创建 SNS Topic

我们先创建 SNS

const topic = new sns.Topic(this, "Topic");

https://ithelp.ithome.com.tw/upload/images/20201012/20117701B2upcd4Jko.png

创建 Cloudwatch Metric

在创建 Cloudwatch Alarm 之前需要先把资料抓出来,所以先创建一个 Metric,指定我们要抓的资料是 AWS/EC2 以 CPU Utilization 为目标,并且指定 EC2 Instance Id

const metric = new cloudwatch.Metric({
  namespace: "AWS/EC2",
  metricName: "CPUUtilization",
  dimensions: {
    InstanceId: instance.instanceId,
  },
  period: cdk.Duration.minutes(1),
});

https://ithelp.ithome.com.tw/upload/images/20201012/20117701aPSjLirZVo.png

创建 Cloudwatch Alarm

准备好 Metric 後就可以把 Alarm 创建起来,范例定义 threshold 为 5

平常比较常用的应该是 85 上下取一个比较能判断发生问题的值,不过我们为了方便测试以 5 为我们的阈并设定 period 为 1

const alarm = new cloudwatch.Alarm(this, "Alarm", {
  metric,
  threshold: 5,
  evaluationPeriods: 1,
});

https://ithelp.ithome.com.tw/upload/images/20201012/20117701lFyyaU430W.png

串接 Cloudwatch Alarm 与 SNS

设定完之後把 Alarm 与 SNS 连起来

import * as cw_actions from "@aws-cdk/aws-cloudwatch-actions";

alarm.addAlarmAction(new cw_actions.SnsAction(topic));

创建 Lambda 并取得发送 LINE Notify 方法

创建一个 Lambda 这边我准备好了脚本大家只要把它 Clone 下来就好拉!

Github:https://github.com/clarencetw/sns-alarm.git

$ git clone https://github.com/clarencetw/sns-alarm.git lambda

这边记得要切换 tag 到 v1.0.0 ,不然会与此篇范例教学不一样呦!

$ git checkout v1.0.0

还有一个要注意的是要修改 "your-line-notify-token",此参数是 LINE Notify 专用的 Token 取得方法请参考我之前的文章

const myFunction = new lambda.Function(this, "Lambda", {
  handler: "index.handler",
  runtime: lambda.Runtime.NODEJS_10_X,
  code: lambda.Code.fromAsset("lambda"),
});

https://ithelp.ithome.com.tw/upload/images/20201012/201177019huM8wQUdD.png

SNS 订阅 Lambda

topic 订阅刚刚创建的 Lambda

topic.addSubscription(new subscriptions.LambdaSubscription(myFunction));

https://ithelp.ithome.com.tw/upload/images/20201012/20117701cGgggbMaoq.png

在 SNS 的 Subscriptions 可以看到 Endpoint 为 Lambda

https://ithelp.ithome.com.tw/upload/images/20201012/20117701lTa74wRqKa.png

可以在 CloudWatch 看到 SNS 的 Trigger

整理一下

如此就完成一个可以收到 CloudWatch 通知的系统拉!

这边也跟大家说明一下之前的范例都是每次建立一个 VPC 这样其实有点久,所以这次就使用 fromLookup 让它寻找 Default VPC 可以让整个建置的速度变快

const vpc = ec2.Vpc.fromLookup(this, "VPC", {
  isDefault: true,
});

const securityGroup = new ec2.SecurityGroup(this, "SecurityGroup", {
  vpc,
  allowAllOutbound: true,
});
securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22));

const instance = new ec2.Instance(this, "Instance", {
  vpc,
  instanceType: ec2.InstanceType.of(
    ec2.InstanceClass.T3A,
    ec2.InstanceSize.NANO
  ),
  machineImage: ec2.MachineImage.latestAmazonLinux({
    generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
  }),
  securityGroup,
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },
  keyName: "KeyPair",
});

const topic = new sns.Topic(this, "Topic");

const metric = new cloudwatch.Metric({
  namespace: "AWS/EC2",
  metricName: "CPUUtilization",
  dimensions: {
    InstanceId: instance.instanceId,
  },
  period: cdk.Duration.minutes(1),
});

const alarm = new cloudwatch.Alarm(this, "Alarm", {
  metric,
  threshold: 5,
  evaluationPeriods: 1,
});

alarm.addAlarmAction(new cw_actions.SnsAction(topic));

const myFunction = new lambda.Function(this, "Lambda", {
  handler: "index.handler",
  runtime: lambda.Runtime.NODEJS_10_X,
  code: lambda.Code.fromAsset("lambda"),
});

topic.addSubscription(new subscriptions.LambdaSubscription(myFunction));

今天介绍了一个使用 CloudWatch 发送 LINE Notify 的方法,其实网路上应该有满多文章在教学这段的,不过这次的重点其实在於教大家怎麽使用 CDK 串接 CloudWatch、SNS 与 Lambda!希望对大家有帮助 ~

想要看更多吗?欢迎到我的部落格参观

文章内容主要是网路或是程序开发类型的文章

本文同步刊载於 Clarence 部落格:Day 27 - 使用 CDK 创建 CloudWatch 也能发送 Alarm 到 LINE 的系统


「AWS CDK 完全学习手册:打造云端基础架构程序码 IaC」
本书改编并延伸自第 12 届 iT 邦帮忙铁人赛获得 DevOps 组冠军的《用 CDK 定义 AWS 架构》系列文章,以简单、好读的行文风格详述技术细节,并提供完整的程序码范例与说明,一步一步带领新手从零开始踏上 AWS CDK 技术达人之路。

有兴趣的朋友欢迎至天珑书局选购!

购书连结 https://bit.ly/2ZMAebE

https://ithelp.ithome.com.tw/upload/images/20211103/20117701W7l6fQnn2L.jpg


<<:  [Day 27] 微探讨 Pure pipe 与 Impure pipe

>>:  第27天-CSS-影像(3-1)

【心得】checkbox表单实作-待办清单

上一次介绍了各种表单的使用 那麽实战练习必不可少呀! 今天就演练演练自制待办清单吧 首先来看一下想做...

Day 26 | SQLite资料库(一)

Android系统内建SQLite供开发者使用,通常用於存放使用者或系统相关的资料,如果资料除了本地...

从 JavaScript 角度学 Python(16) - pip

前言 前面章节我们学习了许多 Python 的基础语法,所以接下来我想额外介绍 pip,pip 在实...

Run HEX File 之 Debug 总集篇

今天就是 Debug 总集篇, 前面都是用猴子自己瞎掰设计的 Code Stream 来验证, 这次...

Node.js安装

昨天介绍了Node.js,今天我们就要来实际安装Node.js啦 首先当然是到官网去下载Node.j...