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

查看:170
本文介绍了通过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


  • cloudwatch事件来监视EC2实例的状态变化和

  • 将输入提供给Lambda,后者将具有自定义的电子邮件模板

  • 然后调用SNS将电子邮件发送给收件人。

让我知道您是否认为这是符合我预期的正确方法。并提供一些有关如何在Cloud Watch和SNS之间获得Lambda的见解。

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主题,后者可以将信息转发给订阅者(通过电子邮件或SMS), OR

  • 通过 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接收事件,然后发送消息至

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\n' +
                  'State: ' + instance['State']['Name'] + '\n' +
                  'IP Address: ' + instance['PublicIpAddress'] + '\n' +
                  'Name: ' + name
    )

要设置:


  • 创建一个SNS主题以接收消息并将主题ARN放入代码中

  • 创建SNS主题的订阅者(最简单的方法是在测试时通过SMS)

  • 创建AWS Lambda函数(如上所示)

  • 创建一个Amazon CloudWatch Event触发EC2实例状态更改,并将目标设置为Lambda函数

  • Create an SNS topic to receive the message and put the topic ARN in the code
  • Create a subscriber to the SNS topic (easiest is via SMS when testing)
  • Create the AWS Lambda function (shown above)
  • Create an Amazon CloudWatch Event to trigger off EC2 instance state change, and set the target to the Lambda function

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

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