1

I am trying to call the AWS lambda function from cli by shell script. I want to send the current logged user name to lambda function as parameter. This is the code I tried:

 #!/bin/bash
    user=$(whoami)
    echo "=START="
    echo "$user"
    aws lambda invoke --invocation-type RequestResponse --function-name shell_lambda_invoke --region ap-south-1 --log-type Tail --payload '{"bashuser":"${user}", "InstanceId":"'i-0c4869ec747845b99'"}'

Thanks in advance.

2 Answers 2

3

Since Your bash variable is inside single quotes it won't get populated.

Test:

$ echo "${user}"
root

Your sample:

$ echo '{"bashuser":"${user}", "InstanceId":"'i-0c4869ec747845b99'"}'
{"bashuser":"${user}", "InstanceId":"i-0c4869ec747845b99"}

You need to interrupt quoting like this:

$ echo '{"bashuser":"'"${user}"'", "InstanceId":"'i-0c4869ec747845b99'"}'
{"bashuser":"root", "InstanceId":"i-0c4869ec747845b99"}
Sign up to request clarification or add additional context in comments.

2 Comments

I want to get the user name in: I tried waht you shared but doesn't work command: aws lambda invoke --invocation-type RequestResponse --function-name shell_lambda_invoke --region ap-south-1 --log-type Tail --payload '{"bashuser":" ' "${user}" ' ", "InstanceId":"'i-0c4869ec747845b99'"}'
the code is working fine when I tried with putty the same code doesn't work with Juicessh application. I am self answering question have look at it and thanks for you help:)
3

To invoke the lambda function using AWS CLI write the following code which works fine: I am sending payload to this function with instance ID and user name as I needed this in lambda if you wan't to send anything other you can send other details as input in payload section.

#!/bin/bash
user=$(whoami)
echo Please Enter the InstanceId
read InstanceId
aws lambda invoke --invocation-type RequestResponse --function-name wu_core_auto_start_stop_lambdainvoke --region us-east-1 --log-type Tail --payload '{"bashuser":"'"${user}"'", "InstanceId":"'"${InstanceId}"'"}' outputfile.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.