Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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

like image 371
Eric Nord Avatar asked Jan 26 '17 16:01

Eric Nord


People also ask

How do you call lambda function from CodePipeline?

From the CodePipeline console, edit the pipeline to add the function as an action in a stage in your pipeline. Choose Edit for the pipeline stage you want to change, and choose Add action group. On the Edit action page, in Action name, enter a name for your action. In Action provider, choose Lambda.

Can Lambda handle multiple triggers?

Your function can have multiple triggers. Each trigger acts as a client invoking your function independently. Each event that Lambda passes to your function has data from only one client or trigger.

Can Lambda run multiple threads?

Lambda supports Python 2.7 and Python 3.6, both of which have multiprocessing and threading modules.

How to add lambda functions to a CodePipeline pipeline?

From the CodePipeline console, edit the pipeline to add the function as an action in a stage in your pipeline. Choose Edit for the pipeline stage you want to change, and choose Add action group. On the Edit action page, in Action name, enter a name for your action. In Action provider, choose AWS Lambda.

What is Lambda in AWS code pipeline?

In the example provided by AWS, the Lambda function tests whether it can access a website without receiving an error. If it succeeds, the CodePipeline action and stage succeed, turn to green, and it automatically transitions to the next stage or completes the pipeline.

Can a Lambda invoke Action use variables from another action?

A Lambda invoke action can use variables from another action as part of its input and return new variables along with its output. For information about variables for actions in CodePipeline, see Variables .

How do I use codecommit with Lambda?

A manual approval action that consumes the new variables from your Lambda invoke action to provide a test URL and a test run ID Before you begin, you must have the following: You can create or use the pipeline with the CodeCommit source in Tutorial: Create a simple pipeline (CodeCommit repository) .


1 Answers

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' }
like image 99
wjordan Avatar answered Oct 13 '22 00:10

wjordan