简体   繁体   English

python lambda aws ec2 代码停止

[英]python lambda code for aws ec2 gets stopped

could any one please help me the lambda code, whenever AWS Ec2 instances get stopped, we need to get the email notifications with sns.任何人都可以帮我 lambda 代码,每当 AWS Ec2 实例停止时,我们需要使用 sns 获取 email 通知。 In the email we need instance name.在 email 中我们需要实例名称。 I could able to get instance id but not the instance name.我可以获取实例 ID,但不能获取实例名称。

AWS CloudTrail allows you to identify and track EC2 instance lifecycle API calls (launch, start, stop, terminate). AWS CloudTrail 允许您识别和跟踪 EC2 实例生命周期 API 调用(启动、开始、停止、终止)。 See How do I use AWS CloudTrail to track API calls to my Amazon EC2 instances?请参阅如何使用 AWS CloudTrail 跟踪对我的 Amazon EC2 实例的 API 次调用?

And you can trigger a Lambda function to run arbitrary code when CloudTrail logs certain events.当 CloudTrail 记录某些事件时,您可以触发 Lambda function 运行任意代码。 See Triggering a Lambda function with AWS CloudTrail events .请参阅使用 AWS CloudTrail 事件触发 Lambda function

You can also create an Amazon CloudWatch alarm that monitors an Amazon EC2 instance and triggers a Lambda via CloudWatch Events.您还可以创建一个 Amazon CloudWatch 警报来监控 Amazon EC2 实例并通过 CloudWatch Events 触发 Lambda。

You can create a rule in Amazon CloudWatch Events that:您可以在Amazon CloudWatch Events中创建一个规则:

  • Triggers when an instance enters the Stopped state实例进入Stopped时触发 state
  • Sends a message to an Amazon SNS Topic向 Amazon SNS 主题发送消息

Like this:像这样:

Amazon CloudWatch 事件规则

If you want to modify the message that is being sent, then configure the Rule to trigger an AWS Lambda function instead.如果您想要修改正在发送的消息,则配置规则以触发 AWS Lambda function Your function should:您的 function 应该:

  • Extract the instance information (eg InstanceId ) from the event parameterevent参数中提取实例信息(例如InstanceId
  • Call describe-instances to obtain the Name of the instance (presumably the Tag with a Key of Name )调用describe-instances以获取实例的名称(大概是键为Name的标签)
  • Publish a message to the Amazon SNS Topic向 Amazon SNS 主题发布消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM