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

feat(lambda): layer version removal policy #12792

Merged
merged 5 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn.currentVersion.addAlias('live');

## Layers

The `lambda.LayerVersion` class can be used to define Lambda layers and manage
The `lambda.LayerVersion` class can be used to define Lambda layer versions and manage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change and add a statement at the end of this section about configuring removal policies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added better documentation to the README. Let me know if it's too much detail

granting permissions to other AWS accounts or organizations.

[Example of Lambda Layer usage](test/integ.layer-version.lit.ts)
Expand Down
14 changes: 13 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/layers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IResource, Resource } from '@aws-cdk/core';
import { IResource, RemovalPolicy, Resource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Code } from './code';
import { CfnLayerVersion, CfnLayerVersionPermission } from './lambda.generated';
Expand Down Expand Up @@ -28,6 +28,14 @@ export interface LayerVersionOptions {
* @default - A name will be generated.
*/
readonly layerVersionName?: string;

/**
* Whether to retain the previous version of the layer when a new version is added
* or when the stack is deleted.
*
* @default - The previous version is deleted.
*/
readonly removalPolicy?: RemovalPolicy;
}

export interface LayerVersionProps extends LayerVersionOptions {
Expand Down Expand Up @@ -198,6 +206,10 @@ export class LayerVersion extends LayerVersionBase {
licenseInfo: props.license,
});

if (props.removalPolicy) {
resource.applyRemovalPolicy(props.removalPolicy);
}

props.code.bindToResource(resource, {
resourceProperty: 'Content',
});
Expand Down
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/layers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,21 @@ describe('layers', () => {
},
}, ResourcePart.CompleteDefinition);
});

test('creating a layer with a removal policy', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new lambda.LayerVersion(stack, 'layer', {
code: lambda.Code.fromAsset(path.join(__dirname, 'layer-code')),
removalPolicy: cdk.RemovalPolicy.RETAIN,
});

// THEN
expect(canonicalizeTemplate(SynthUtils.toCloudFormation(stack))).toHaveResource('AWS::Lambda::LayerVersion', {
UpdateReplacePolicy: 'Retain',
DeletionPolicy: 'Retain',
}, ResourcePart.CompleteDefinition);
});
});