繁体   English   中英

通过(python)lambda在EC2上运行作业

[英]Run a Job on EC2 from a (python) lambda

将文件放入S3后,将触发Lambda函数,该实习生应触发EC2实例中的作业。 从Lambda获取访问EC2并运行作业的最佳方法是什么?

  • 这是python lambda代码,用于查找带有Environment:Dev标签Environment:Dev的ec2实例。
  • AWS 博客
  • S3使用Terraform github触发Lambda
import boto3

def trigger_handler(event, context):
    #Get IP addresses of EC2 instances
    client = boto3.client('ec2')
    instDict=client.describe_instances(
            Filters=[{'Name':'tag:Environment','Values':['Dev']}]
        )

    hostList=[]
    for r in instDict['Reservations']:
        for inst in r['Instances']:
            hostList.append(inst['PublicIpAddress'])

    #Invoke worker function for each IP address
    client = boto3.client('lambda')
    for host in hostList:
        print "Invoking worker_function on " + host
        invokeResponse=client.invoke(
            FunctionName='worker_function',
            InvocationType='Event',
            LogType='Tail',
            Payload='{"IP":"'+ host +'"}'
        )
        print invokeResponse

    return{
        'message' : "Trigger function finished"
    }

暂无
暂无

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

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