Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

How do I run two command in Lambda Function

Currently I have this script in my lambda function

#-*- coding: utf-8 -*-
import time
import json
import boto3

region=['ap-southeast-1']
instance1 = 'i-XXXXXXXXXX'
instance2 = 'i-XXXXXXXXXX'

def lambda_handler(event, context):

    # boto3 client
    client = boto3.client('ec2')
    ssm_client = boto3.client('ssm')
    response = ssm_client.send_command(InstanceIds=[instance1],DocumentName="AWS-RunShellScript",Parameters={'commands': ['aws s3 sync cd/var/www/html s3://bucketname/']}, )
    response2 = ssm_client.send_command(InstanceIds=[instance2],DocumentName="AWS-RunShellScript",Parameters={'commands': ['aws s3 sync s3://bucketname /var/www/html/']}, )

    command_id = response['Command']['CommandId']
    command_id2 = response2['Command']['CommandId']
    
    time.sleep(60)

    output = ssm_client.get_command_invocation(CommandId=command_id,InstanceId=instance1,)
    output2 = ssm_client.get_command_invocation(CommandId=command_id2,InstanceId=instance2,)

   
    print(output)
    print(output2)

    return {
        'statusCode': 200,
        'body': json.dumps('Done!')
    }

the result is successful. but they run command at the same time. I want the second command run after sync to s3 are done.

What i want to do is:

instance1 > s3

after sync done

s3 > instance2

the reason i want to do this because instance1 cannot sync to instance2 for security reason.

question from:https://stackoverflow.com/questions/65645130/how-do-i-run-two-command-in-lambda-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In such scenario when you have a sequence of execution, I would say you may use AWS step functions:

1- create two separated lambda functions, one for instance1 > s3, and the second for s3 > instance2.

2- create AWS Step Functions to run a serverless workflow that coordinates the two AWS Lambda functions.

This example shows how to create function step state uses lambda


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...