使用参数从AWS SSM自动执行Lambda
问题描述:
我目前有一个lambda函数,它使用作为参数传递的值更新DynamoDB表。我能够与测试参数设置为"TEST"
运行LAMBDA控制台中的以下内容:使用参数从AWS SSM自动执行Lambda
import boto3
import json
def lambda_handler(event, context):
# TODO implement
update_ami(event)
def update_ami(ami_id):
#DO STUFF
我试图从以下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"
}
}
]
}
执行此自动化会导致以下错误:
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')
我也试图传递Payload
输入作为一个JSON对象instea字符串d,并调整好自己的拉姆达相应方法:
JSON自动化:
...
"inputs": {
"FunctionName": "MyLambdaFunction",
"Payload": {
"ami_id": "AMI-TESTER"
}
}
...
LAMBDA的Python:
def lambda_handler(event, context):
# TODO implement
update_ami(event['ami-id'])
这将导致以下错误从内部自动化文档编辑器来了SSM控制台:
Input {ami_id=TESTER} is of type class java.util.LinkedHashMap, but expected type is String.
所以简而言之...如何传递单个字符串从自动化文档转换为Lambda函数?
答
(如下所示)的错误看起来是问题与不是字符串传递有效载荷:
Input {ami_id=TESTER} is of type class java.util.LinkedHashMap, but expected type is String
请尝试双引号前使用转义字符。
"inputs": {
"FunctionName": "MyLambdaFunction",
"Payload": "{
\"ami_id\": \"AMI-TESTER\"
}"
}
AWS提供了正确的语法,如果你看到this Url
{
"name":"updateSsmParam",
"action":"aws:invokeLambdaFunction",
"timeoutSeconds":1200,
"maxAttempts":1,
"onFailure":"Abort",
"inputs":{
"FunctionName":"Automation-UpdateSsmParam",
"Payload":"{\"parameterName\":\"latestAmi\", \"parameterValue\":\"{{createImage.ImageId}}\"}"
}
}