Eric Nord
Eric Nord

Reputation: 4885

Invoke Lambda from CodePipeline with multiple UserParameters

This tutorial shows how to Invoke a Lambda from CodePipeline passing a single parameter:

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

I've built a slackhook lambda that needs to get 2 parameters:

Passing in JSON via the CodePipeline editor results in the JSON block being sent in ' ' so it can't be parsed directly.

UserParameter passed in:

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

User Parameter in Event payload

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

When trying to apply multiple UserParameters directly in the CLoudFormation like this:

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

Create an error - Configuration must only contain simple objects or strings.

Any guesses on how to get multiple UserParameters passing from a CloudFormation template into a Lambda would be much appreciated.

Here is the lambda code for reference: https://github.com/byu-oit-appdev/aws-codepipeline-lambda-slack-webhook

Upvotes: 7

Views: 5625

Answers (1)

wjordan
wjordan

Reputation: 20390

You should be able to pass multiple UserParameters as a single JSON-object string, then parse the JSON in your Lambda function upon receipt.

This is exactly how the Python example in the documentation handles this case:

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)

Similarly, using JSON.parse should work fine in Node.JS to parse a JSON-object string (as shown in your Event payload example) into a usable JSON object:

> 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' }

Upvotes: 11

Related Questions