lambda扫描SQS后terminate EC2 instance
import boto3
REGION = 'cn-north-1'
EC2 = boto3.client('ec2', region_name=REGION)
sqs = boto3.client('sqs')
queue_url = 'https://sqs.*******'
def lambda_terminate_ec2(event, context):
#message = event['message']
response = sqs.receive_message(
QueueUrl=queue_url,
AttributeNames=['********'],
MaxNumberOfMessages=1,
MessageAttributeNames=['All'],
VisibilityTimeout=0,
WaitTimeSeconds=0
)
message = response['Messages'][0]
receipt_handle = message['ReceiptHandle']
msg = message['Body']
instance = EC2.terminate_instances(
InstanceIds=[msg],
DryRun= False
)
# Delete received message from queue
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
return msg
上一篇: Linux批量删除指定后缀名的文件
下一篇: 【转】Java中的 lambda 表达式