使用多个 UserParameters 从 CodePipeline 调用 Lambda

Invoke Lambda from CodePipeline with multiple UserParameters

本教程展示了如何通过单个参数从 CodePipeline 调用 Lambda:

http://docs.aws.amazon.com/codepipeline/latest/userguide/how-to-lambda-integration.html

我构建了一个需要获取 2 个参数的 slackhook lambda:

通过 CodePipeline 编辑器传入 JSON 会导致 JSON 块在“ ”中发送,因此无法直接对其进行解析。

传入的用户参数:

{
  "webhook":"https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho",
  "message":"Staging build awaiting approval for production deploy"
}

事件负载中的用户参数

UserParameters: '{
  "webhook":"https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho",
  "message":"Staging build awaiting approval for production deploy"
}'

当尝试像这样在 CLoudFormation 中直接应用多个 UserParameters 时:

Name: SlackNotification
  ActionTypeId:
    Category: Invoke
    Owner: AWS
    Version: '1'
    Provider: Lambda
  OutputArtifacts: []
  Configuration:
    FunctionName: aws-notify2
    UserParameters:
       - webhook: !Ref SlackHook
       - message: !Join [" ",[!Ref app, !Ref env, "build has started"]]
  RunOrder: 1

创建错误 - 配置必须只包含简单的对象或字符串。

任何关于如何将多个 UserParameters 从 CloudFormation 模板传递到 Lambda 的猜测都将不胜感激。

这里是供参考的lambda代码: https://github.com/byu-oit-appdev/aws-codepipeline-lambda-slack-webhook

您应该能够将多个 UserParameters 作为单个 JSON-object 字符串传递,然后在收到后在您的 Lambda 函数中解析 JSON。

这正是文档中 Python example 处理这种情况的方式:

try:
    # Get the user parameters which contain the stack, artifact and file settings
    user_parameters = job_data['actionConfiguration']['configuration']['UserParameters']
    decoded_parameters = json.loads(user_parameters)

类似地,使用 JSON.parse 应该可以在 Node.JS 中正常工作,将 JSON-object 字符串(如您的事件负载示例所示)解析为可用的 JSON 对象:

> JSON.parse('{ "webhook":"https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho", "message":"Staging build awaiting approval for production deploy" }')
{ webhook: 'https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho',
  message: 'Staging build awaiting approval for production deploy' }