1

I have the below python code inside Lambda function:

print("Executing shell script-")
response = ssm_client.send_command(
    InstanceIds=ec2_instance,
    DocumentName="AWS-RunShellScript",
    Parameters={
        "commands": [f"sh /bin/test/publishImage.sh -c {ecr} -r {ecr_repo} -r {region} -e {environment}"]
    },
    OutputS3BucketName="deploymentlogs",
    OutputS3Region=region
)
print("Done - Executing shell script.")

and when executed Lambda returns immediately with Status Code 200 in just few seconds whereas the publishImage.sh script takes around 3 minutes to complete its execution.

I would want Lambda to return after publishImage.sh script completes it execution irrespective of success or failure.

How can I achieve this ? Please help.

Thanks

Suraj Naik
  • 189
  • 2
  • 11
  • 2
    The SSM send_command is able to run across potentially thousands of EC2's. It isn't designed to be synchronous. See [this post](https://stackoverflow.com/questions/67440685/is-there-a-way-to-check-that-ssm-send-command-is-running-properly) to be able to check on the status. – stdunbar Apr 28 '22 at 15:12

1 Answers1

3

That's expected, because send_command does not wait for the completion of the command on the remote host.

If you want the lambda function to wait until the shell script has finished running, you will need to implement some kind of waiting logic to check the status of the command. You can check the status of the remote command execution by using the get_command_invocation function.

Paolo
  • 21,270
  • 6
  • 38
  • 69