Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Move the Lambda Invoke CodePipeline Action #401

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 1 addition & 80 deletions packages/@aws-cdk/aws-codepipeline/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import events = require('@aws-cdk/aws-events');
import lambda = require('@aws-cdk/aws-lambda');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { Artifact } from './artifact';
Expand Down Expand Up @@ -30,7 +29,7 @@ export interface ActionArtifactBounds {
maxOutputs: number;
}

function defaultBounds(): ActionArtifactBounds {
export function defaultBounds(): ActionArtifactBounds {
return {
minInputs: 0,
maxInputs: 5,
Expand Down Expand Up @@ -442,84 +441,6 @@ export class ApprovalAction extends Action {
}
}

export interface InvokeLambdaProps {
/**
* The lambda function to invoke.
*/
lambda: lambda.LambdaRef;

/**
* String to be used in the event data parameter passed to the Lambda
* function
*
* See an example JSON event in the CodePipeline documentation.
*
* https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html#actions-invoke-lambda-function-json-event-example
*/
userParameters?: any;

/**
* Adds the "codepipeline:PutJobSuccessResult" and
* "codepipeline:PutJobFailureResult" for '*' resource to the Lambda
* execution role policy.
*
* NOTE: the reason we can't add the specific pipeline ARN as a resource is
* to avoid a cyclic dependency between the pipeline and the Lambda function
* (the pipeline references) the Lambda and the Lambda needs permissions on
* the pipeline.
*
* @see
* https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html#actions-invoke-lambda-function-create-function
*
* @default true
*/
addPutJobResultPolicy?: boolean;
}
/**
* @link https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html
*/
export class InvokeLambdaAction extends Action {
constructor(parent: Stage, name: string, props: InvokeLambdaProps) {
super(parent, name, {
category: ActionCategory.Invoke,
provider: 'Lambda',
artifactBounds: defaultBounds(),
configuration: {
FunctionName: props.lambda.functionName,
UserParameters: props.userParameters
}
});

// allow pipeline to list functions
parent.pipeline.addToRolePolicy(new cdk.PolicyStatement()
.addAction('lambda:ListFunctions')
.addResource('*'));

// allow pipeline to invoke this lambda functionn
parent.pipeline.addToRolePolicy(new cdk.PolicyStatement()
.addAction('lambda:InvokeFunction')
.addResource(props.lambda.functionArn));

// allow lambda to put job results for this pipeline.
const addToPolicy = props.addPutJobResultPolicy !== undefined ? props.addPutJobResultPolicy : true;
if (addToPolicy) {
props.lambda.addToRolePolicy(new cdk.PolicyStatement()
.addResource('*') // to avoid cycles (see docs)
.addAction('codepipeline:PutJobSuccessResult')
.addAction('codepipeline:PutJobFailureResult'));
}
}

/**
* Add an input artifact
* @param artifact
*/
protected addInputArtifact(artifact: Artifact): Action {
super.addInputArtifact(artifact);
return this;
}
}

// export class TestAction extends Action {
// constructor(parent: Stage, name: string, provider: string, artifactBounds: ActionArtifactBounds, configuration?: any) {
// super(parent, name, {
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-codepipeline/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"dependencies": {
"@aws-cdk/aws-events": "^0.7.3-beta",
"@aws-cdk/aws-iam": "^0.7.3-beta",
"@aws-cdk/aws-lambda": "^0.7.3-beta",
"@aws-cdk/aws-s3": "^0.7.3-beta",
"@aws-cdk/cdk": "^0.7.3-beta",
"@aws-cdk/util": "^0.7.3-beta"
Expand Down
83 changes: 0 additions & 83 deletions packages/@aws-cdk/aws-codepipeline/test/test.action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { expect, haveResource } from '@aws-cdk/assert';
import lambda = require('@aws-cdk/aws-lambda');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import codepipeline = require('../lib');
Expand Down Expand Up @@ -86,87 +84,6 @@ export = {
outputArtifacts: [{ name: 'TestOutput' }],
runOrder: 1
});
test.done();
},

'InvokeLambdaAction can be used to invoke lambda functions from a pipeline'(test: Test) {
const stack = new cdk.Stack();

const fction = new lambda.Lambda(stack, 'Function', {
code: new lambda.LambdaInlineCode('bla'),
handler: 'index.handler',
runtime: lambda.LambdaRuntime.NodeJS43,
});

const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');

new codepipeline.InvokeLambdaAction(new codepipeline.Stage(pipeline, 'Stage'), 'InvokeAction', {
lambda: fction,
userParameters: 'foo-bar/42'
});

expect(stack).to(haveResource('AWS::CodePipeline::Pipeline', {
"ArtifactStore": {
"Location": {
"Ref": "PipelineArtifactsBucket22248F97"
},
"Type": "S3"
},
"RoleArn": {
"Fn::GetAtt": [
"PipelineRoleD68726F7",
"Arn"
]
},
"Stages": [
{
"Actions": [
{
"ActionTypeId": {
"Category": "Invoke",
"Owner": "AWS",
"Provider": "Lambda",
"Version": "1"
},
"Configuration": {
"FunctionName": {
"Ref": "Function76856677"
},
"UserParameters": "foo-bar/42"
},
"InputArtifacts": [],
"Name": "InvokeAction",
"OutputArtifacts": [],
"RunOrder": 1
}
],
"Name": "Stage"
}
]
}));

expect(stack).to(haveResource('AWS::IAM::Policy', {
"PolicyDocument": {
"Statement": [
{
"Action": [
"codepipeline:PutJobSuccessResult",
"codepipeline:PutJobFailureResult"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "FunctionServiceRoleDefaultPolicy2F49994A",
"Roles": [
{
"Ref": "FunctionServiceRole675BB04A"
}
]
}));

test.done();
}
};
Expand Down
86 changes: 86 additions & 0 deletions packages/@aws-cdk/aws-codepipeline/test/test.general-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { AmazonS3Source } from '../lib/actions';
import { Pipeline } from '../lib/pipeline';
import { Stage } from '../lib/stage';
import { validateName } from '../lib/validation';

interface NameValidationTestCase {
name: string;
shouldPassValidation: boolean;
explanation: string;
}

export = {
'name validation'(test: Test) {
const cases: NameValidationTestCase[] = [
{ name: 'BlahBleep123.@-_', shouldPassValidation: true, explanation: 'should be valid' },
{ name: '', shouldPassValidation: false, explanation: 'the empty string should be invalid' },
{ name: ' BlahBleep', shouldPassValidation: false, explanation: 'spaces should be invalid' },
{ name: '!BlahBleep', shouldPassValidation: false, explanation: '\'!\' should be invalid' }
];

cases.forEach(testCase => {
const name = testCase.name;
const validationBlock = () => { validateName('test thing', name); };
if (testCase.shouldPassValidation) {
test.doesNotThrow(validationBlock, Error, `${name} failed validation but ${testCase.explanation}`);
} else {
test.throws(validationBlock, Error, `${name} passed validation but ${testCase.explanation}`);
}
});

test.done();
},

'Stage validation': {
'should fail if Stage has no Actions'(test: Test) {
const stage = stageForTesting();

test.deepEqual(stage.validate().length, 1);

test.done();
}
},

'Pipeline validation': {
'should fail if Pipeline has no Stages'(test: Test) {
const stack = new cdk.Stack();
const pipeline = new Pipeline(stack, 'Pipeline');

test.deepEqual(pipeline.validate().length, 1);

test.done();
},

'should fail if Pipeline has a Source Action in a non-first Stage'(test: Test) {
const stack = new cdk.Stack();
const pipeline = new Pipeline(stack, 'Pipeline');
const firstStage = new Stage(pipeline, 'FirstStage');
const secondStage = new Stage(pipeline, 'SecondStage');

const bucket = new s3.Bucket(stack, 'PipelineBucket');
new AmazonS3Source(firstStage, 'FirstAction', {
artifactName: 'FirstArtifact',
bucket,
bucketKey: 'key',
});
new AmazonS3Source(secondStage, 'SecondAction', {
artifactName: 'SecondAction',
bucket,
bucketKey: 'key',
});

test.deepEqual(pipeline.validate().length, 1);

test.done();
}
}
};

function stageForTesting(): Stage {
const stack = new cdk.Stack();
const pipeline = new Pipeline(stack, 'pipeline');
return new Stage(pipeline, 'stage');
}
31 changes: 0 additions & 31 deletions packages/@aws-cdk/aws-codepipeline/test/test.generalValidation.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda-codepipeline/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.js
*.js.map
*.d.ts
node_modules
*.generated.ts
dist
tsconfig.json
tslint.json

.jsii

.LAST_BUILD
.nyc_output
coverage
.nycrc
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-lambda-codepipeline/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-lambda-codepipeline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## AWS CodePipline Actions for AWS Lambda

This module contains an Action that allows you to invoke a Lambda function from CodePipeline.

Example usage:

```ts
import codepipeline = require('@aws-cdk/aws-codepipeline');
import lambda = require('@aws-cdk/aws-lambda');
import lambdaCodepipeline = require('@aws-cdk/aws-lambda-codepipeline');

// see the @aws-cdk/aws-lambda module for more documentation on how to create Lamda functions
const lambdaFun = new lambda.Lambda(// ...
);

const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
const lambdaStage = new codepipeline.Stage(pipeline, 'Lambda');
new lambdaCodepipeline.PipelineInvokeAction(lambdaStage, 'Lambda', {
lambda: lambdaFun,
});
```

See [the AWS documentation](https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html)
on how to write a Lambda function invoked from CodePipeline.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-codepipeline/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './pipeline-action';
Loading