简体   繁体   中英

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!')
}

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!')
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM