简体   繁体   English

使用参数从 AWS SSM 自动化执行 Lambda

[英]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.我目前有一个 lambda 函数,它使用作为参数传递的值更新 DynamoDB 表。 I am able to run the following within the Lambda console with a test parameter set to "TEST" :我能够在 Lambda 控制台中运行以下测试参数设置为"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:我试图从以下 JSON 文档构建的 SSM 自动化中调用它:

{  
   "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:我还尝试将Payload输入作为 JSON 对象而不是字符串传递,并相应地调整了我的 lambda 方法:

JSON Automation: JSON 自动化:

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

Lambda Python:拉姆达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:这会导致 SSM 控制台中的自动化文档编辑器出现以下错误:

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?简而言之......如何将自动化文档中的单个字符串传递给 Lambda 函数?

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 AWS 提供了正确的语法,如果你看到这个 Url

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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