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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…