-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathalias.ts
263 lines (227 loc) · 7.77 KB
/
alias.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import { Construct } from '@aws-cdk/core';
import { EventInvokeConfigOptions } from './event-invoke-config';
import { IFunction, QualifiedFunctionBase } from './function-base';
import { extractQualifierFromArn, IVersion } from './lambda-version';
import { CfnAlias } from './lambda.generated';
export interface IAlias extends IFunction {
/**
* Name of this alias.
*
* @attribute
*/
readonly aliasName: string;
/**
* The underlying Lambda function version.
*/
readonly version: IVersion;
}
/**
* Options for `lambda.Alias`.
*/
export interface AliasOptions extends EventInvokeConfigOptions {
/**
* Description for the alias
*
* @default No description
*/
readonly description?: string;
/**
* Additional versions with individual weights this alias points to
*
* Individual additional version weights specified here should add up to
* (less than) one. All remaining weight is routed to the default
* version.
*
* For example, the config is
*
* version: "1"
* additionalVersions: [{ version: "2", weight: 0.05 }]
*
* Then 5% of traffic will be routed to function version 2, while
* the remaining 95% of traffic will be routed to function version 1.
*
* @default No additional versions
*/
readonly additionalVersions?: VersionWeight[];
/**
* Specifies a provisioned concurrency configuration for a function's alias.
*
* @default No provisioned concurrency
*/
readonly provisionedConcurrentExecutions?: number;
}
/**
* Properties for a new Lambda alias
*/
export interface AliasProps extends AliasOptions {
/**
* Name of this alias
*/
readonly aliasName: string;
/**
* Function version this alias refers to
*
* Use lambda.addVersion() to obtain a new lambda version to refer to.
*/
readonly version: IVersion;
}
export interface AliasAttributes {
readonly aliasName: string;
readonly aliasVersion: IVersion;
}
/**
* A new alias to a particular version of a Lambda function.
*/
export class Alias extends QualifiedFunctionBase implements IAlias {
public static fromAliasAttributes(scope: Construct, id: string, attrs: AliasAttributes): IAlias {
class Imported extends QualifiedFunctionBase implements IAlias {
public readonly aliasName = attrs.aliasName;
public readonly version = attrs.aliasVersion;
public readonly lambda = attrs.aliasVersion.lambda;
public readonly functionArn = `${attrs.aliasVersion.lambda.functionArn}:${attrs.aliasName}`;
public readonly functionName = `${attrs.aliasVersion.lambda.functionName}:${attrs.aliasName}`;
public readonly grantPrincipal = attrs.aliasVersion.grantPrincipal;
public readonly role = attrs.aliasVersion.role;
protected readonly canCreatePermissions = false;
protected readonly qualifier = attrs.aliasName;
}
return new Imported(scope, id);
}
/**
* Name of this alias.
*
* @attribute
*/
public readonly aliasName: string;
/**
* ARN of this alias
*
* Used to be able to use Alias in place of a regular Lambda. Lambda accepts
* ARNs everywhere it accepts function names.
*/
public readonly functionName: string;
public readonly lambda: IFunction;
public readonly version: IVersion;
/**
* ARN of this alias
*
* Used to be able to use Alias in place of a regular Lambda. Lambda accepts
* ARNs everywhere it accepts function names.
*/
public readonly functionArn: string;
protected readonly qualifier: string;
protected readonly canCreatePermissions: boolean = true;
constructor(scope: Construct, id: string, props: AliasProps) {
super(scope, id, {
physicalName: props.aliasName,
});
this.lambda = props.version.lambda;
this.aliasName = this.physicalName;
this.version = props.version;
const alias = new CfnAlias(this, 'Resource', {
name: this.aliasName,
description: props.description,
functionName: this.version.lambda.functionName,
functionVersion: props.version.version,
routingConfig: this.determineRoutingConfig(props),
provisionedConcurrencyConfig: this.determineProvisionedConcurrency(props)
});
this.functionArn = this.getResourceArnAttribute(alias.ref, {
service: 'lambda',
resource: 'function',
resourceName: `${this.lambda.functionName}:${this.physicalName}`,
sep: ':',
});
this.qualifier = extractQualifierFromArn(alias.ref);
if (props.onFailure || props.onSuccess || props.maxEventAge || props.retryAttempts !== undefined) {
this.configureAsyncInvoke({
onFailure: props.onFailure,
onSuccess: props.onSuccess,
maxEventAge: props.maxEventAge,
retryAttempts: props.retryAttempts,
});
}
// ARN parsing splits on `:`, so we can only get the function's name from the ARN as resourceName...
// And we're parsing it out (instead of using the underlying function directly) in order to have use of it incur
// an implicit dependency on the resource.
this.functionName = `${this.stack.parseArn(this.functionArn, ":").resourceName!}:${this.aliasName}`;
}
public get grantPrincipal() {
return this.version.grantPrincipal;
}
public get role() {
return this.version.role;
}
public metric(metricName: string, props: cloudwatch.MetricOptions = {}): cloudwatch.Metric {
// Metrics on Aliases need the "bare" function name, and the alias' ARN, this differes from the base behavior.
return super.metric(metricName, {
dimensions: {
FunctionName: this.lambda.functionName,
// construct the name from the underlying lambda so that alarms on an alias
// don't cause a circular dependency with CodeDeploy
// see: https://github.com/aws/aws-cdk/issues/2231
Resource: `${this.lambda.functionName}:${this.aliasName}`
},
...props
});
}
/**
* Calculate the routingConfig parameter from the input props
*/
private determineRoutingConfig(props: AliasProps) {
if (!props.additionalVersions || props.additionalVersions.length === 0) {
return undefined;
}
this.validateAdditionalWeights(props.additionalVersions);
return {
additionalVersionWeights: props.additionalVersions.map(vw => {
return {
functionVersion: vw.version.version,
functionWeight: vw.weight
};
})
};
}
/**
* Validate that the additional version weights make sense
*
* We validate that they are positive and add up to something <= 1.
*/
private validateAdditionalWeights(weights: VersionWeight[]) {
const total = weights.map(w => {
if (w.weight < 0 || w.weight > 1) { throw new Error(`Additional version weight must be between 0 and 1, got: ${w.weight}`); }
return w.weight;
}).reduce((a, x) => a + x);
if (total > 1) {
throw new Error(`Sum of additional version weights must not exceed 1, got: ${total}`);
}
}
/**
* Validate that the provisionedConcurrentExecutions makes sense
*
* Member must have value greater than or equal to 1
*/
private determineProvisionedConcurrency(props: AliasProps): CfnAlias.ProvisionedConcurrencyConfigurationProperty | undefined {
if (!props.provisionedConcurrentExecutions) {
return undefined;
}
if (props.provisionedConcurrentExecutions <= 0) {
throw new Error('provisionedConcurrentExecutions must have value greater than or equal to 1');
}
return {provisionedConcurrentExecutions: props.provisionedConcurrentExecutions};
}
}
/**
* A version/weight pair for routing traffic to Lambda functions
*/
export interface VersionWeight {
/**
* The version to route traffic to
*/
readonly version: IVersion;
/**
* How much weight to assign to this version (0..1)
*/
readonly weight: number;
}