简体   繁体   中英

Execute Lambda from AWS SSM Automation with Parameters

I currently have a lambda function that updates a DynamoDB table with a value passed as a parameter. I am able to run the following within the Lambda console with a test parameter set to "TEST" :

import boto3
import json

def lambda_handler(event, context):
    # TODO implement
    update_ami(event)


def update_ami(ami_id):
    #DO STUFF

I am attempting to call this from an SSM Automation built from the following JSON document:

{  
   "description":"Test Execute Lambda Function.",
   "schemaVersion":"0.3",
   "assumeRole":"MYARN",
   "parameters":{},
   "mainSteps":[  
        {
            "name": "invokeMyLambdaFunction",
            "action": "aws:invokeLambdaFunction",
            "maxAttempts": 3,
            "timeoutSeconds": 120,
            "onFailure": "Abort",
            "inputs": {
                "FunctionName": "MyLambdaFunction",
                "Payload": "TESTER"

            }
        }
   ]
}

Executing this automation results in the following error:

Automation Step Execution fails when it is invoking the lambda function. Get Exception from Invoke API of lambda Service. Exception Message from Invoke API: [Could not parse request body into json: Unrecognized token 'TESTER': was expecting ('true', 'false' or 'null')

I have also tried passing the Payload input as a JSON object instead of a string, and adjusted my lambda method accordingly:

JSON Automation:

...
    "inputs": {
         "FunctionName": "MyLambdaFunction",
         "Payload": {
             "ami_id": "AMI-TESTER"
         }  
    }
...

Lambda Python:

def lambda_handler(event, context):
    # TODO implement
    update_ami(event['ami-id'])

This results in the following error coming from the Automation Document editor within the SSM console:

Input {ami_id=TESTER} is of type class java.util.LinkedHashMap, but expected type is String.

So in a nutshell... How do I pass a single string from an Automation document to a Lambda Function?

The error which is shown below looks to be issue with payload not passed as string:

Input {ami_id=TESTER} is of type class java.util.LinkedHashMap, but expected type is String

Please try to use escape character before double quotes.

    "inputs": {
             "FunctionName": "MyLambdaFunction",
             "Payload": "{
                 \"ami_id\": \"AMI-TESTER\"
             }"  
        }

AWS has provided proper syntax, if you see this Url

  {
     "name":"updateSsmParam",
     "action":"aws:invokeLambdaFunction",
     "timeoutSeconds":1200,
     "maxAttempts":1,
     "onFailure":"Abort",
     "inputs":{
        "FunctionName":"Automation-UpdateSsmParam",
        "Payload":"{\"parameterName\":\"latestAmi\", \"parameterValue\":\"{{createImage.ImageId}}\"}"
     }
  }

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