unable to pass dynamic parameter into ssm send command

0

Hi, I need to pass dynamic log path to Runshellscript ssm command . I tried the below way but getting logpath as string not taking any reference value which i was defined before step. tried below ways to pass value: {{logpath}} $logpath but no use, could you please help me to pass lopath value object in to ssm send command to run the shell script

logpath = f"{instance_id}:{accountId}:{_time}.log" print(logpath)

calmScanCmd=ssm_client.send_command(
    InstanceIds=['i-0c7f20d36a7338a62'],
    DocumentName='AWS-RunShellScript',
     Parameters={'commands': ["sh /tmp/executeScript.sh  **logpath** "]},

    Comment='calling clam scan script'
    )
print(calmScanCmd)
asked a month ago133 views
2 Answers
1
Accepted Answer

Hi

You can use f strings method for simple dynamic parameter insertion.

logpath = f"{instance_id}:{accountId}:{_time}.log"
print(logpath)

calmScanCmd = ssm_client.send_command(
    InstanceIds=['i-0c7f20d36a7338a62'],
    DocumentName='AWS-RunShellScript',
    Parameters={'commands': [f"sh /tmp/executeScript.sh {logpath}"]},
    Comment='calling clam scan script'
)
print(calmScanCmd)

Reference from StackFlow https://stackoverflow.com/questions/71893560/how-to-execute-aws-ssm-send-command-to-run-shell-script-with-arguments-from-lamb

profile picture
EXPERT
GK
answered a month ago
profile picture
EXPERT
reviewed a month ago
0

Hi,

You have a full sample of working code in this gist: https://gist.github.com/arnathan2k/30c8fc5b3fda7e3e100877bdd1eef995

def lambda_handler(event, context):
    try:
        ec2_client = boto3.client('ec2', region_name='us-west-1')
        print(contents)
        running_instances = [instance.get('InstanceId') for instance in ec2_client.describe_instance_status().get('InstanceStatuses') if instance.get('InstanceState').get('Code')==16]
        ssm_client = boto3.client('ssm', region_name='us-west-1')
        params={"commands":["mkdir:\winevt","robocopy c:\Windows\System32\winevt\logs c:\winevt","Compress-Archive c:\winevtcompress"],"workingDirectory":["c:\"],"executionTimeout":["3600"]}
        print(running_instances[0])
        response = ssm_client.send_command(DocumentName="AWS-RunPowerShellScript", InstanceIds=running_instances,Comment='logging the', TimeoutSeconds=600, Parameters=params)
    except Exception as e:
        raise e
    else:
        print('------------- succeded ---------------------')
    finally:
        return True

Best,

Didier

profile pictureAWS
EXPERT
answered a month ago
profile picture
EXPERT
reviewed 24 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions