`
eye-lij
  • 浏览: 3538 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics