From a79bc47aaa6737628562c251e2f1990b2c7b88ef Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 28 Apr 2022 20:30:23 +0200 Subject: [PATCH] feat(lambda): `function.addAlias()` simplifies Alias creation (#20034) Deprecate `version.addAlias()` in favor of a new method called `function.addAlias()`. - When Aliases get created underneath Versions, there's the risk that it's `currentVersion` whose logical ID changes on every deployment, causing a (probably failing) replacement of the `Alias` on every function update. - The most common use for `function.currentVersion` is to create an `Alias` for a Function. Might as well take out the middle man. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-lambda/README.md | 7 ++--- packages/@aws-cdk/aws-lambda/lib/function.ts | 27 +++++++++++++++++++ .../@aws-cdk/aws-lambda/lib/lambda-version.ts | 3 +++ .../@aws-cdk/aws-lambda/test/function.test.ts | 12 +++++++++ .../aws-lambda/test/lambda-version.test.ts | 3 ++- .../aws-lambda/test/singleton-lambda.test.ts | 3 ++- 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda/README.md b/packages/@aws-cdk/aws-lambda/README.md index 573d469bfccd6..bb5f4eec740b4 100644 --- a/packages/@aws-cdk/aws-lambda/README.md +++ b/packages/@aws-cdk/aws-lambda/README.md @@ -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 @@ -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 }); diff --git a/packages/@aws-cdk/aws-lambda/lib/function.ts b/packages/@aws-cdk/aws-lambda/lib/function.ts index 608bbf4e7bc02..68002c22c5b4b 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function.ts @@ -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) @@ -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. * diff --git a/packages/@aws-cdk/aws-lambda/lib/lambda-version.ts b/packages/@aws-cdk/aws-lambda/lib/lambda-version.ts index 50a3e5ade12dd..1249c487466dd 100644 --- a/packages/@aws-cdk/aws-lambda/lib/lambda-version.ts +++ b/packages/@aws-cdk/aws-lambda/lib/lambda-version.ts @@ -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; } @@ -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); diff --git a/packages/@aws-cdk/aws-lambda/test/function.test.ts b/packages/@aws-cdk/aws-lambda/test/function.test.ts index 9cadfd03afab9..e783535926eef 100644 --- a/packages/@aws-cdk/aws-lambda/test/function.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/function.test.ts @@ -547,6 +547,18 @@ describe('function', () => { Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda/$LATEST', Match.stringLikeRegexp(warningMessage)); }); + test('function.addAlias', () => { + // 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 diff --git a/packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts b/packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts index 5f6c416ae07db..80a540fa5c6c8 100644 --- a/packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts @@ -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', () => { @@ -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', { diff --git a/packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts b/packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts index 0f259e1866abd..bd40f612558e1 100644 --- a/packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts @@ -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', () => { @@ -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', {