Skip to content

Commit

Permalink
feat(codepipeline): add support for string user parameters to the Lam…
Browse files Browse the repository at this point in the history
…bda invoke action (#16946)

feat(codepipeline-actions): Add support for string user parameters

closes #16776 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ayush987goyal committed Oct 13, 2021
1 parent 589f284 commit e19ea31
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/README.md
Expand Up @@ -917,6 +917,25 @@ const lambdaAction = new codepipeline_actions.LambdaInvokeAction({
});
```

The Lambda Action supports custom user parameters that pipeline
will pass to the Lambda function:

```ts
import * as lambda from '@aws-cdk/aws-lambda';

const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
const lambdaAction = new codepipeline_actions.LambdaInvokeAction({
actionName: 'Lambda',
lambda: fn,
userParameters: {
foo: 'bar',
baz: 'qux',
},
// OR
userParametersString: 'my-parameter-string',
});
```

The Lambda invoke action emits variables.
Unlike many other actions, the variables are not static,
but dynamic, defined by the function calling the `PutJobSuccessResult`
Expand Down
Expand Up @@ -38,10 +38,24 @@ export interface LambdaInvokeActionProps extends codepipeline.CommonAwsActionPro
* A set of key-value pairs that will be accessible to the invoked Lambda
* inside the event that the Pipeline will call it with.
*
* Only one of `userParameters` or `userParametersString` can be specified.
*
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html#actions-invoke-lambda-function-json-event-example
* @default - no user parameters will be passed
*/
readonly userParameters?: { [key: string]: any };

/**
* The string representation of the user parameters that will be
* accessible to the invoked Lambda inside the event
* that the Pipeline will call it with.
*
* Only one of `userParametersString` or `userParameters` can be specified.
*
* @default - no user parameters will be passed
*/
readonly userParametersString?: string;

/**
* The lambda function to invoke.
*/
Expand Down Expand Up @@ -71,6 +85,10 @@ export class LambdaInvokeAction extends Action {
});

this.props = props;

if (props.userParameters && props.userParametersString) {
throw new Error('Only one of userParameters or userParametersString can be specified');
}
}

/**
Expand Down Expand Up @@ -121,7 +139,7 @@ export class LambdaInvokeAction extends Action {
return {
configuration: {
FunctionName: this.props.lambda.functionName,
UserParameters: Stack.of(scope).toJsonString(this.props.userParameters),
UserParameters: this.props.userParametersString ?? Stack.of(scope).toJsonString(this.props.userParameters),
},
};
}
Expand Down
Expand Up @@ -3,9 +3,9 @@ import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
import * as sns from '@aws-cdk/aws-sns';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import { App, Aws, Lazy, SecretValue, Stack, Token } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as cpactions from '../../lib';

/* eslint-disable quote-props */
Expand Down Expand Up @@ -100,6 +100,36 @@ describe('', () => {

});

test('properly assings userParametersString to UserParameters', () => {
const stack = stackIncludingLambdaInvokeCodePipeline({
userParamsString: '**/*.template.json',
});

expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
'Stages': [
{},
{
'Actions': [
{
'Configuration': {
'UserParameters': '**/*.template.json',
},
},
],
},
],
});
});

test('throws if both userParameters and userParametersString are supplied', () => {
expect(() => stackIncludingLambdaInvokeCodePipeline({
userParams: {
key: Token.asString(null),
},
userParamsString: '**/*.template.json',
})).toThrow(/Only one of userParameters or userParametersString can be specified/);
});

test("assigns the Action's Role with read permissions to the Bucket if it has only inputs", () => {
const stack = stackIncludingLambdaInvokeCodePipeline({
lambdaInput: new codepipeline.Artifact(),
Expand Down Expand Up @@ -302,6 +332,7 @@ describe('', () => {

interface HelperProps {
readonly userParams?: { [key: string]: any };
readonly userParamsString?: string;
readonly lambdaInput?: codepipeline.Artifact;
readonly lambdaOutput?: codepipeline.Artifact;
}
Expand Down Expand Up @@ -334,6 +365,7 @@ function stackIncludingLambdaInvokeCodePipeline(props: HelperProps, app?: App) {
runtime: lambda.Runtime.NODEJS_10_X,
}),
userParameters: props.userParams,
userParametersString: props.userParamsString,
inputs: props.lambdaInput ? [props.lambdaInput] : undefined,
outputs: props.lambdaOutput ? [props.lambdaOutput] : undefined,
}),
Expand Down

0 comments on commit e19ea31

Please sign in to comment.