Goural
Goural

Reputation: 79

I want to pass parameters in aws Lambda while invoking from cli

I am trying to invoke lambda from my pipeline using the below command where I need to pass the ami as input variable to lambda.

aws lambda invoke --function-name SuccessLambda --cli-binary-format raw-in-base64-out --payload '{"ami":"ami-1234"}' response.json

my lambda function should read this, please help with the syntax -

import json
import boto3
import os
ami=event.ami
sns = boto3.client('sns')
def lambda_handler(event, context):
    ami = $(event.ami)
    message = "The new ami %s is now available" % (ami)
    response = sns.publish(
            TopicArn = "arn:SuccessArtifactsNotificationTopic",
            Message = message,
            Subject=(Subject)
            )
    
    return {
      'statusCode': 200,
      'body': json.dumps('Success!')
}

Upvotes: 2

Views: 3956

Answers (2)

Febin Najeeb
Febin Najeeb

Reputation: 21

For passing single value in code, you must try this one:

aws lambda invoke --function-name SuccessArtifactsNotificationLambda --payload {"key": "value"} --region region

For passing multiple strings:

aws lambda invoke --function-name SuccessArtifactsNotificationLambda --payload {"key": "value", "key": "value"} --region region

Also, you can add parameters as need as per the needs:

-- response.json

--invocation-type RequestResponse : The preceding invoke-command specifies RequestResponse as the invocation type, which returns a response immediately in response to the function execution. Alternatively, you can specify Event as the invocation type to invoke the function asynchronously.

--cli-binary-format raw-in-base64-out

Upvotes: 0

Goural
Goural

Reputation: 79

Please find the below solution -

aws lambda invoke --function-name SuccessArtifactsNotificationLambda --cli-binary-format raw-in-base64-out --payload '{"ami":"ami-1234"}' response.json

This is how you can call the variable in lambda-

import json
import boto3
import os
sns = boto3.client('sns')
def lambda_handler(event, context):
    ami = event['ami']
    message = "The new ami %s is now available" % (ami)
    response = sns.publish(
            TopicArn = "arn:SuccessTopic",
            Message = message,
            Subject=(Subject)
            )
    
    return {
      'statusCode': 200,
      'body': json.dumps('Success!')
}

Upvotes: 2

Related Questions