Suraj Naik
Suraj Naik

Reputation: 199

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

Upvotes: 1

Views: 3409

Answers (1)

Paolo
Paolo

Reputation: 25999

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"
    )

Upvotes: 3

Related Questions