如果您的Python Lambda代码在AWS EC2实例停止时遇到了问题,以下是处理该问题的步骤:
ec2:StopInstances
ec2:DescribeInstances
import boto3
def lambda_handler(event, context):
instance_id = event['detail']['instance-id']
ec2 = boto3.client('ec2')
ec2.stop_instances(InstanceIds=[instance_id])
print('Stopped instance: ' + instance_id)
此代码传递给函数的事件应如下所示:
{
"version": "0",
"id": "896ab788-8816-4ae7-9cea-xxxxxxxx",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "123456789012",
"time": "yyyy-mm-ddThh:mm:ssZ",
"region": "us-east-1",
"resources": [
"arn:aws:ec2:us-east-1:123456789012:instance/i-012345678910abcdefghijkl"
],
"detail": {
"instance-id": "i-012345678910abcdefghijkl",
"state": "stopped"
}
}
上一篇:AWSEC2实例升级-IP更改?
下一篇:AWSEC2实例停止响应