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): function.addAlias() simplifies Alias creation #20034

Merged
merged 4 commits into from
Apr 28, 2022
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
7 changes: 2 additions & 5 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ const fn = new lambda.Function(this, 'MyFunction', {
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});

fn.currentVersion.addAlias('live');
fn.addAlias('live');
```

## Function URL
Expand Down Expand Up @@ -687,10 +687,7 @@ You can use Application AutoScaling to automatically configure the provisioned c
import * as autoscaling from '@aws-cdk/aws-autoscaling';

declare const fn: lambda.Function;
const alias = new lambda.Alias(this, 'Alias', {
aliasName: 'prod',
version: fn.latestVersion,
});
const alias = fn.addAlias('prod');

// Create AutoScaling target
const as = alias.addAutoScaling({ maxCapacity: 50 });
Expand Down
27 changes: 27 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { Runtime } from './runtime';
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line
import { LogRetentionRetryOptions } from './log-retention';
import { AliasOptions, Alias } from './alias';
import { addAlias } from './util';

/**
* X-Ray Tracing Modes (https://docs.aws.amazon.com/lambda/latest/dg/API_TracingConfig.html)
Expand Down Expand Up @@ -959,6 +961,31 @@ export class Function extends FunctionBase {
});
}

/**
* Defines an alias for this function.
*
* The alias will automatically be updated to point to the latest version of
* the function as it is being updated during a deployment.
*
* ```ts
* declare const fn: lambda.Function;
*
* fn.addAlias('Live');
*
* // Is equivalent to
*
* new lambda.Alias(this, 'AliasLive', {
* aliasName: 'Live',
* version: fn.currentVersion,
* });
*
* @param aliasName The name of the alias
* @param options Alias options
*/
public addAlias(aliasName: string, options?: AliasOptions): Alias {
return addAlias(this, this.currentVersion, aliasName, options);
}

/**
* The LogGroup where the Lambda function's logs are made available.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/lambda-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface IVersion extends IFunction {
* Defines an alias for this version.
* @param aliasName The name of the alias
* @param options Alias options
*
* @deprecated Calling `addAlias` on a `Version` object will cause the Alias to be replaced on every function update. Call `function.addAlias()` or `new Alias()` instead.
*/
addAlias(aliasName: string, options?: AliasOptions): Alias;
}
Expand Down Expand Up @@ -244,6 +246,7 @@ export class Version extends QualifiedFunctionBase implements IVersion {
* Defines an alias for this version.
* @param aliasName The name of the alias (e.g. "live")
* @param options Alias options
* @deprecated Calling `addAlias` on a `Version` object will cause the Alias to be replaced on every function update. Call `function.addAlias()` or `new Alias()` instead.
*/
public addAlias(aliasName: string, options: AliasOptions = {}): Alias {
return addAlias(this, this, aliasName, options);
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,18 @@ describe('function', () => {
Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda/$LATEST', Match.stringLikeRegexp(warningMessage));
});

test('function.addAlias', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm understanding correctly, the issue with fn.currentVersion.addAlias() is that the Alias will be replaced on every function update. I think we should test that fn.addAlias() solves the issue. This test doesn't really tell me anything.

To be honest, I'm not clear how function.addAlias() changes this behavior. To me, it seems like both fn.addAlias() and fn.currentVersion.addAlias() results in the same thing: return addAlias(this, <current-version>, aliasName, options).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but the this is different! In one case it's the Function and the other it's the Version.

But I think I was mistaken anyway: I thought the Alias would be replaced if the Version object changed... but I'm now pretty convinced it doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this is just convenience.

// WHEN
fn.addAlias('prod');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Alias', {
Name: 'prod',
FunctionName: { Ref: 'MyLambdaCCE802FB' },
FunctionVersion: { 'Fn::GetAtt': ['MyLambdaCurrentVersionE7A382CC60ef151b20ae483ee1018f73f30bc10e', 'Version'] },
});
});

describe('permission on alias', () => {
test('of current version', () => {
// GIVEN
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Template } from '@aws-cdk/assertions';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';

describe('lambda version', () => {
test('can import a Lambda version by ARN', () => {
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('lambda version', () => {
});
});

test('addAlias can be used to add an alias that points to a version', () => {
testDeprecated('addAlias can be used to add an alias that points to a version', () => {
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'Fn', {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';

describe('singleton lambda', () => {
test('can add same singleton Lambda multiple times, only instantiated once in template', () => {
Expand Down Expand Up @@ -241,7 +242,7 @@ describe('singleton lambda', () => {
expect(singleton.runtime).toStrictEqual(lambda.Runtime.PYTHON_3_9);
});

test('current version of a singleton function', () => {
testDeprecated('current version of a singleton function', () => {
// GIVEN
const stack = new cdk.Stack();
const singleton = new lambda.SingletonFunction(stack, 'Singleton', {
Expand Down