Codepipeline Construct Library
To use first create a cdk project like this:
mkdir foo
cd foo
Next create folders for the lambda, tests and the cdk project for example:
mkdir resources
mkdir src
mkdir tests
Then generate the actual cdk project by (this will generate a default ResourceStack):
cd resources
cdk init app --language typescript
Develop the lambda code and tests, when done commit the project to codecommit for example (foo-lambda-repo)
Next create a stack for the lambda (foo-lambda-stack.ts) inside the lib folder of the cdk project /resources/lib like so:
...
import * as lambda from '@aws-cdk/aws-lambda'
export class FooLambdaStack extends cdk.Stack {
public readonly lambdaCode: lambda.CfnParametersCode;
constructor(scope: cdk.Construct, id: string, props?:cdk.StackProps) {
super(scope, id, props)
this.lambdaCode = lambda.Code.fromCfnParameters();
const func = new lambda.Function(this, 'FooLambda', {
code: this.lambdaCode,
handler: 'foo.handler',
runtime: lambda.Runtime.PYTHON_3_8,
description: `Generated on: ${new Date().toISOString()}`,
});
}
}
Next using the project generated Stack in this case ResourcesStack You can generate a lambda codepipeline like this:
...
import { LambdaPipeline } from 'cdk-pipelines'
export interface ResourcesStackProps extends cdk.StackProps {
readonly lambdaCode: lambda.CfnParametersCode;
}
export class ResourcesStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: CdkTestStackProps) {
super(scope, id, props);
new LambdaPipeline(this, "FooLambdaPipelineStack", {
codeRepositoryName:'foo-lambda-repo',
cdkFolderName: 'foo',
lambdaCodeFolderName: 'src',
lambdaCode: props.lambdaCode,
installCommands: [
'pip install -r requirements.txt'
],
buildCommands: [
'python -m pytest -v -s tests --disable-pytest-warnings'
],
artifactBaseDir:'src',
artifactFiles: [
'**/*'
],
runtime: {python : 3.8}
})
}
}
Next update the resources.ts file in the bin directory to use the two stacks lambda, pipeline like so:
...
import { FooLambdaStack } from '../lib/foo-lambda-stack';
import { ResourcesStack } from '../lib/resources-stack';
const app = new cdk.App();
const lambdaStack = new FooLambdaStack(app, 'FooLambdaStack');
new ResourcesStack(app, 'ResourcesStack', {
lambdaCode: lambdaStack.lambdaCode
});
Finally update your test under resources/test/resources.test.ts
The directory structure should look this this:
.
├── requirements.txt
├── resources
│ ├── README.md
│ ├── bin
│ │ └── resources.ts
│ ├── cdk.json
│ ├── jest.config.js
│ ├── lib
│ │ ├── foo-lambda-stack.ts
│ │ └── resources-stack.ts
│ ├── package-lock.json
│ ├── package.json
│ ├── test
│ │ └── resources.test.ts
│ └── tsconfig.json
├── src
│ └── lambda.py
└── tests
└── test_lambda.py
Before you deploy make sure this code is also commited to codecommit.
To deploy the codepipeline do this:
npm run build
cdk deploy ResourcesStack
This will build the codepipeline which will now pull the source code from the codecommit repo (foo-lambda-repo), and build the lambda stack from there.