首页 文章

使用多个UserParameters从CodePipeline调用Lambda

提问于
浏览
4

本教程介绍如何从CodePipeline调用Lambda传递单个参数:

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

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

  • webhook_url

  • 消息

通过CodePipeline编辑器传入JSON会导致JSON块被发送,因此无法直接解析 .

UserParameter传入:

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

Event有效内容中的用户参数

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

1 回答

  • 5

    您应该能够将多个 UserParameters 作为单个JSON对象字符串传递,然后在收到时解析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对象字符串(如事件有效负载示例中所示)解析为可用的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' }
    

相关问题