7

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:

  • webhook_url
  • message

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

1 Answer 1

11

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' }
Sign up to request clarification or add additional context in comments.

1 Comment

@Eric If you are creating pipeline using CloudFormation or CLI, remember to escape the quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.