Skip to content

Commit a79bc47

Browse files
authored
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*
1 parent f199fad commit a79bc47

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

packages/@aws-cdk/aws-lambda/README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ const fn = new lambda.Function(this, 'MyFunction', {
336336
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
337337
});
338338

339-
fn.currentVersion.addAlias('live');
339+
fn.addAlias('live');
340340
```
341341

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

689689
declare const fn: lambda.Function;
690-
const alias = new lambda.Alias(this, 'Alias', {
691-
aliasName: 'prod',
692-
version: fn.latestVersion,
693-
});
690+
const alias = fn.addAlias('prod');
694691

695692
// Create AutoScaling target
696693
const as = alias.addAutoScaling({ maxCapacity: 50 });

packages/@aws-cdk/aws-lambda/lib/function.ts

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { Runtime } from './runtime';
2626
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
2727
// eslint-disable-next-line
2828
import { LogRetentionRetryOptions } from './log-retention';
29+
import { AliasOptions, Alias } from './alias';
30+
import { addAlias } from './util';
2931

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

964+
/**
965+
* Defines an alias for this function.
966+
*
967+
* The alias will automatically be updated to point to the latest version of
968+
* the function as it is being updated during a deployment.
969+
*
970+
* ```ts
971+
* declare const fn: lambda.Function;
972+
*
973+
* fn.addAlias('Live');
974+
*
975+
* // Is equivalent to
976+
*
977+
* new lambda.Alias(this, 'AliasLive', {
978+
* aliasName: 'Live',
979+
* version: fn.currentVersion,
980+
* });
981+
*
982+
* @param aliasName The name of the alias
983+
* @param options Alias options
984+
*/
985+
public addAlias(aliasName: string, options?: AliasOptions): Alias {
986+
return addAlias(this, this.currentVersion, aliasName, options);
987+
}
988+
962989
/**
963990
* The LogGroup where the Lambda function's logs are made available.
964991
*

packages/@aws-cdk/aws-lambda/lib/lambda-version.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export interface IVersion extends IFunction {
3030
* Defines an alias for this version.
3131
* @param aliasName The name of the alias
3232
* @param options Alias options
33+
*
34+
* @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.
3335
*/
3436
addAlias(aliasName: string, options?: AliasOptions): Alias;
3537
}
@@ -244,6 +246,7 @@ export class Version extends QualifiedFunctionBase implements IVersion {
244246
* Defines an alias for this version.
245247
* @param aliasName The name of the alias (e.g. "live")
246248
* @param options Alias options
249+
* @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.
247250
*/
248251
public addAlias(aliasName: string, options: AliasOptions = {}): Alias {
249252
return addAlias(this, this, aliasName, options);

packages/@aws-cdk/aws-lambda/test/function.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,18 @@ describe('function', () => {
547547
Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda/$LATEST', Match.stringLikeRegexp(warningMessage));
548548
});
549549

550+
test('function.addAlias', () => {
551+
// WHEN
552+
fn.addAlias('prod');
553+
554+
// THEN
555+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Alias', {
556+
Name: 'prod',
557+
FunctionName: { Ref: 'MyLambdaCCE802FB' },
558+
FunctionVersion: { 'Fn::GetAtt': ['MyLambdaCurrentVersionE7A382CC60ef151b20ae483ee1018f73f30bc10e', 'Version'] },
559+
});
560+
});
561+
550562
describe('permission on alias', () => {
551563
test('of current version', () => {
552564
// GIVEN

packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Template } from '@aws-cdk/assertions';
22
import * as cdk from '@aws-cdk/core';
33
import * as lambda from '../lib';
4+
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
45

56
describe('lambda version', () => {
67
test('can import a Lambda version by ARN', () => {
@@ -103,7 +104,7 @@ describe('lambda version', () => {
103104
});
104105
});
105106

106-
test('addAlias can be used to add an alias that points to a version', () => {
107+
testDeprecated('addAlias can be used to add an alias that points to a version', () => {
107108
// GIVEN
108109
const stack = new cdk.Stack();
109110
const fn = new lambda.Function(stack, 'Fn', {

packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam';
44
import * as s3 from '@aws-cdk/aws-s3';
55
import * as cdk from '@aws-cdk/core';
66
import * as lambda from '../lib';
7+
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
78

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

244-
test('current version of a singleton function', () => {
245+
testDeprecated('current version of a singleton function', () => {
245246
// GIVEN
246247
const stack = new cdk.Stack();
247248
const singleton = new lambda.SingletonFunction(stack, 'Singleton', {

0 commit comments

Comments
 (0)