通过 SNS 和 Lambda 的电子邮件通知 [英] Email notification through SNS and Lambda

查看:27
本文介绍了通过 SNS 和 Lambda 的电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题.我的主要动机是在 ec2 实例发生状态更改时发送电子邮件.

I am facing an issue. My main motive is to send an email whenever there is state change happened to ec2 instances.

我直接使用 SNS 尝试了云观看事件及其工作,但我收到的电子邮件模板没有正确的信息来理解.

I tried cloud watch events directly with SNS and its work also but the email template which I am receiving is not having the proper information to understood.

我期待电子邮件模板中的服务器名称及其 IP,但 SNS 没有给我修改它的选项.所以我在想的是让 lambda 参与进来,以便

I was expecting Server name and its IP in the email template which SNS does not give me the option to modify it. So What I am thinking is to involve lambda so that

  • 用于监控 EC2 实例状态变化的 cloudwatch 事件和
  • 向 Lambda 提供输入,该 Lambda 将具有自定义的电子邮件模板,即
  • 然后调用 SNS 向收件人发送电子邮件.

如果您认为这是否符合我的预期,请告诉我.并就如何让 Lambda 介于 Cloud watch 和 SNS 之间提供一些见解

Let me know if you think this is correct method for what I am expecting or not. and give some insights on how to get Lambda in between Cloud watch and SNS

谢谢&问候

推荐答案

Amazon CloudWatch Events 控制台所示,由实例状态更改触发的示例事件是:

As shown in the Amazon CloudWatch Events console, a sample event triggered by an instance state change is:

{
  "version": "0",
  "id": "7bf73129-1428-4cd3-a780-95db273d1602",
  "detail-type": "EC2 Instance State-change Notification",
  "source": "aws.ec2",
  "account": "123456789012",
  "time": "2015-11-11T21:29:54Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
  ],
  "detail": {
    "instance-id": "i-abcd1111",
    "state": "pending"
  }
}

CloudWatch 事件然后可以直接触发 AWS Lambda 函数,传递此信息.

CloudWatch events can then directly trigger an AWS Lambda function, passing in this information.

Lambda 函数可以使用实例 ID 来检索有关实例的更多详细信息(例如服务器名称、IP 地址).

The Lambda function can use the Instance ID to retrieve further details about the instance (eg server name, IP address).

该函数可以:

  • 将文本发送到 Amazon SNS 主题,该主题可以将信息转发给订阅者(通过电子邮件或短信),
  • 通过Amazon Simple Email Service (SES)发送电子邮件,它可以发送格式复杂的电子邮件
  • Send text to an Amazon SNS Topic, which can forward the information to subscribers (via email or SMS), OR
  • Send the emails via Amazon Simple Email Service (SES), which can send emails with complex formatting

如果您不介意基于文本的内容,则使用 SNS 将是最简单的.

Using SNS would be the easiest, if you don't mind the text-based content.

以下是一些示例代码,它们将在实例更改状态时从 Amazon CloudWatch Events 接收事件,然后向 Amazon SNS 主题发送消息,并提供更多详细信息:

Here is some sample code that will receive an event from Amazon CloudWatch Events when an instance changes state, then send a message to an Amazon SNS topic with further details:

import boto3

def lambda_handler(event, context):

    # Extract Instance ID from event
    instance_id = event['detail']['instance-id']

    # Obtain information about the instance
    ec2_client = boto3.client('ec2')
    instance_info = ec2_client.describe_instances(InstanceIds=[instance_id])
    instance = instance_info['Reservations'][0]['Instances'][0]

    # Extract name tag
    name_tags = [t['Value'] for t in instance['Tags'] if t['Key']=='Name']
    name = name_tags[0] if name_tags is not None else ''

    # Send message to SNS
    MY_SNS_TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:123456789012:foo'
    sns_client = boto3.client('sns')
    sns_client.publish(
        TopicArn = MY_SNS_TOPIC_ARN,
        Subject = 'Instance Change State: ' + instance_id,
        Message = 'Instance: ' + instance_id + ' has changed state
' +
                  'State: ' + instance['State']['Name'] + '
' +
                  'IP Address: ' + instance['PublicIpAddress'] + '
' +
                  'Name: ' + name
    )

设置:

  • 创建一个 SNS 主题来接收消息并将主题 ARN 放入代码中
  • 创建 SNS 主题的订阅者(最简单的方法是在测试时通过 SMS)
  • 创建 AWS Lambda 函数(如上所示)
  • 创建 Amazon CloudWatch 事件以触发 EC2 实例状态更改,并将目标设置为 Lambda 函数

这篇关于通过 SNS 和 Lambda 的电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆