Search code examples
amazon-web-servicesaws-lambdaaws-ssm

Why my AWS Lambda function doesn't wait for SSM Send Command (which runs Shell script remotely on EC2) to complete?


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


Solution

  • 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.