'How to execute AWS SSM send command to run shell script with arguments from Lambda?

Currently working on a AWS Lambda function to execute shell script (with arguments) remotely on EC2 instance.

Shell script argument values are stored as environment variables in Lambda.

How to reference the env variables of Lambda inside SSM send command?

Have a code snippet like this: (but it doesn't work)

   response = ssm_client.send_command(
        InstanceIds=instances,
        DocumentName="AWS-RunShellScript",
        Parameters={
            "commands": ["sh /bin/TEST/test.sh -v {{os.environ:tar_version}}"]
        },
        OutputS3BucketName="tar",
        OutputS3Region="eu-west-1"
    )

Request you to please help me here.

Thanks



Solution 1:[1]

All you need to do is simple string formatting. Using Python's f-strings:

import os

tar_version = os.environ['TAR_VERSION']

response = ssm_client.send_command(
        InstanceIds=instances,
        DocumentName="AWS-RunShellScript",
        Parameters={
            "commands": [f"sh /bin/TEST/test.sh -v {tar_version}"]
        },
        OutputS3BucketName="tar",
        OutputS3Region="eu-west-1"
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paolo