1
1
import cloudwatch = require( '@aws-cdk/aws-cloudwatch' ) ;
2
2
import { Construct , Stack } from '@aws-cdk/cdk' ;
3
- import { FunctionBase , IFunction } from './function-base' ;
3
+ import { IFunction , QualifiedFunctionBase } from './function-base' ;
4
4
import { IVersion } from './lambda-version' ;
5
5
import { CfnAlias } from './lambda.generated' ;
6
6
7
+ export interface IAlias extends IFunction {
8
+ /**
9
+ * Name of this alias.
10
+ *
11
+ * @attribute
12
+ */
13
+ readonly aliasName : string ;
14
+
15
+ /**
16
+ * The underlying Lambda function version.
17
+ */
18
+ readonly version : IVersion ;
19
+ }
20
+
7
21
/**
8
22
* Properties for a new Lambda alias
9
23
*/
@@ -47,10 +61,30 @@ export interface AliasProps {
47
61
readonly additionalVersions ?: VersionWeight [ ] ;
48
62
}
49
63
64
+ export interface AliasAttributes {
65
+ readonly aliasName : string ;
66
+ readonly aliasVersion : IVersion ;
67
+ }
68
+
50
69
/**
51
70
* A new alias to a particular version of a Lambda function.
52
71
*/
53
- export class Alias extends FunctionBase {
72
+ export class Alias extends QualifiedFunctionBase implements IAlias {
73
+ public static fromAliasAttributes ( scope : Construct , id : string , attrs : AliasAttributes ) : IAlias {
74
+ class Imported extends QualifiedFunctionBase implements IAlias {
75
+ public readonly aliasName = attrs . aliasName ;
76
+ public readonly version = attrs . aliasVersion ;
77
+ public readonly lambda = attrs . aliasVersion . lambda ;
78
+ public readonly functionArn = `${ attrs . aliasVersion . lambda . functionArn } :${ attrs . aliasName } ` ;
79
+ public readonly functionName = `${ attrs . aliasVersion . lambda . functionName } :${ attrs . aliasName } ` ;
80
+ public readonly grantPrincipal = attrs . aliasVersion . grantPrincipal ;
81
+ public readonly role = attrs . aliasVersion . role ;
82
+
83
+ protected readonly canCreatePermissions = false ;
84
+ }
85
+ return new Imported ( scope , id ) ;
86
+ }
87
+
54
88
/**
55
89
* Name of this alias.
56
90
*
@@ -65,6 +99,10 @@ export class Alias extends FunctionBase {
65
99
*/
66
100
public readonly functionName : string ;
67
101
102
+ public readonly lambda : IFunction ;
103
+
104
+ public readonly version : IVersion ;
105
+
68
106
/**
69
107
* ARN of this alias
70
108
*
@@ -75,21 +113,17 @@ export class Alias extends FunctionBase {
75
113
76
114
protected readonly canCreatePermissions : boolean = true ;
77
115
78
- /**
79
- * The actual Lambda function object that this Alias is pointing to
80
- */
81
- private readonly underlyingLambda : IFunction ;
82
-
83
116
constructor ( scope : Construct , id : string , props : AliasProps ) {
84
117
super ( scope , id ) ;
85
118
119
+ this . lambda = props . version . lambda ;
86
120
this . aliasName = props . aliasName ;
87
- this . underlyingLambda = props . version . lambda ;
121
+ this . version = props . version ;
88
122
89
123
const alias = new CfnAlias ( this , 'Resource' , {
90
124
name : props . aliasName ,
91
125
description : props . description ,
92
- functionName : this . underlyingLambda . functionName ,
126
+ functionName : this . version . lambda . functionName ,
93
127
functionVersion : props . version . version ,
94
128
routingConfig : this . determineRoutingConfig ( props )
95
129
} ) ;
@@ -101,26 +135,23 @@ export class Alias extends FunctionBase {
101
135
this . functionArn = alias . aliasArn ;
102
136
}
103
137
104
- /**
105
- * Role associated with this alias
106
- */
107
- public get role ( ) {
108
- return this . underlyingLambda . role ;
138
+ public get grantPrincipal ( ) {
139
+ return this . version . grantPrincipal ;
109
140
}
110
141
111
- public get grantPrincipal ( ) {
112
- return this . underlyingLambda . grantPrincipal ;
142
+ public get role ( ) {
143
+ return this . version . role ;
113
144
}
114
145
115
146
public metric ( metricName : string , props : cloudwatch . MetricOptions = { } ) : cloudwatch . Metric {
116
147
// Metrics on Aliases need the "bare" function name, and the alias' ARN, this differes from the base behavior.
117
148
return super . metric ( metricName , {
118
149
dimensions : {
119
- FunctionName : this . underlyingLambda . functionName ,
150
+ FunctionName : this . lambda . functionName ,
120
151
// construct the ARN from the underlying lambda so that alarms on an alias
121
152
// don't cause a circular dependency with CodeDeploy
122
153
// see: https://github.com/awslabs/aws-cdk/issues/2231
123
- Resource : `${ this . underlyingLambda . functionArn } :${ this . aliasName } `
154
+ Resource : `${ this . lambda . functionArn } :${ this . aliasName } `
124
155
} ,
125
156
...props
126
157
} ) ;
0 commit comments