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

你可能感兴趣的:(lambda,sqs,terminate,queue,receipt_handle)