-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
custom-resource-provider.ts
250 lines (217 loc) · 7.22 KB
/
custom-resource-provider.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
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
import { CfnResource } from '../cfn-resource';
import { Duration } from '../duration';
import { Size } from '../size';
import { Stack } from '../stack';
import { Token } from '../token';
const ENTRYPOINT_FILENAME = '__entrypoint__';
const ENTRYPOINT_NODEJS_SOURCE = path.join(__dirname, 'nodejs-entrypoint.js');
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '../construct-compat';
/**
* Initialization properties for `CustomResourceProvider`.
*
*/
export interface CustomResourceProviderProps {
/**
* A local file system directory with the provider's code. The code will be
* bundled into a zip asset and wired to the provider's AWS Lambda function.
*/
readonly codeDirectory: string;
/**
* The AWS Lambda runtime and version to use for the provider.
*/
readonly runtime: CustomResourceProviderRuntime;
/**
* A set of IAM policy statements to include in the inline policy of the
* provider's lambda function.
*
* @default - no additional inline policy
*
* @example
*
* [{ Effect: 'Allow', Action: 's3:PutObject*', Resource: '*' }]
*
*/
readonly policyStatements?: any[];
/**
* AWS Lambda timeout for the provider.
*
* @default Duration.minutes(15)
*/
readonly timeout?: Duration;
/**
* The amount of memory that your function has access to. Increasing the
* function's memory also increases its CPU allocation.
*
* @default Size.mebibytes(128)
*/
readonly memorySize?: Size;
/**
* Key-value pairs that are passed to Lambda as Environment
*
* @default - No environment variables.
*/
readonly environment?: { [key: string]: string };
/**
* A description of the function.
*
* @default - No description.
*/
readonly description?: string;
}
/**
* The lambda runtime to use for the resource provider. This also indicates
* which language is used for the handler.
*/
export enum CustomResourceProviderRuntime {
/**
* Node.js 12.x
*
* @deprecated Use {@link NODEJS_12_X}
*/
NODEJS_12 = 'nodejs12.x',
/**
* Node.js 12.x
*/
NODEJS_12_X = 'nodejs12.x',
/**
* Node.js 14.x
*/
NODEJS_14_X = 'nodejs14.x',
}
/**
* An AWS-Lambda backed custom resource provider.
*
*/
export class CustomResourceProvider extends CoreConstruct {
/**
* Returns a stack-level singleton ARN (service token) for the custom resource
* provider.
*
* @param scope Construct scope
* @param uniqueid A globally unique id that will be used for the stack-level
* construct.
* @param props Provider properties which will only be applied when the
* provider is first created.
* @returns the service token of the custom resource provider, which should be
* used when defining a `CustomResource`.
*/
public static getOrCreate(scope: Construct, uniqueid: string, props: CustomResourceProviderProps) {
return this.getOrCreateProvider(scope, uniqueid, props).serviceToken;
}
/**
* Returns a stack-level singleton for the custom resource provider.
*
* @param scope Construct scope
* @param uniqueid A globally unique id that will be used for the stack-level
* construct.
* @param props Provider properties which will only be applied when the
* provider is first created.
* @returns the service token of the custom resource provider, which should be
* used when defining a `CustomResource`.
*/
public static getOrCreateProvider(scope: Construct, uniqueid: string, props: CustomResourceProviderProps) {
const id = `${uniqueid}CustomResourceProvider`;
const stack = Stack.of(scope);
const provider = stack.node.tryFindChild(id) as CustomResourceProvider
?? new CustomResourceProvider(stack, id, props);
return provider;
}
/**
* The ARN of the provider's AWS Lambda function which should be used as the
* `serviceToken` when defining a custom resource.
*
* @example
* new CustomResource(this, 'MyCustomResource', {
* // ...
* serviceToken: myProvider.serviceToken, // <--- here
* })
*
*/
public readonly serviceToken: string;
/**
* The ARN of the provider's AWS Lambda function role.
*/
public readonly roleArn: string;
protected constructor(scope: Construct, id: string, props: CustomResourceProviderProps) {
super(scope, id);
const stack = Stack.of(scope);
// copy the entry point to the code directory
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(props.codeDirectory, `${ENTRYPOINT_FILENAME}.js`));
// verify we have an index file there
if (!fs.existsSync(path.join(props.codeDirectory, 'index.js'))) {
throw new Error(`cannot find ${props.codeDirectory}/index.js`);
}
const staging = new AssetStaging(this, 'Staging', {
sourcePath: props.codeDirectory,
});
const asset = stack.addFileAsset({
fileName: staging.relativeStagedPath(stack),
sourceHash: staging.sourceHash,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
});
const policies = !props.policyStatements ? undefined : [
{
PolicyName: 'Inline',
PolicyDocument: {
Version: '2012-10-17',
Statement: props.policyStatements,
},
},
];
const role = new CfnResource(this, 'Role', {
type: 'AWS::IAM::Role',
properties: {
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: [{ Action: 'sts:AssumeRole', Effect: 'Allow', Principal: { Service: 'lambda.amazonaws.com' } }],
},
ManagedPolicyArns: [
{ 'Fn::Sub': 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' },
],
Policies: policies,
},
});
this.roleArn = Token.asString(role.getAtt('Arn'));
const timeout = props.timeout ?? Duration.minutes(15);
const memory = props.memorySize ?? Size.mebibytes(128);
const handler = new CfnResource(this, 'Handler', {
type: 'AWS::Lambda::Function',
properties: {
Code: {
S3Bucket: asset.bucketName,
S3Key: asset.objectKey,
},
Timeout: timeout.toSeconds(),
MemorySize: memory.toMebibytes(),
Handler: `${ENTRYPOINT_FILENAME}.handler`,
Role: role.getAtt('Arn'),
Runtime: props.runtime,
Environment: this.renderEnvironmentVariables(props.environment),
Description: props.description ?? undefined,
},
});
handler.addDependsOn(role);
this.serviceToken = Token.asString(handler.getAtt('Arn'));
}
private renderEnvironmentVariables(env?: { [key: string]: string }) {
if (!env || Object.keys(env).length === 0) {
return undefined;
}
// Sort environment so the hash of the function used to create
// `currentVersion` is not affected by key order (this is how lambda does
// it)
const variables: { [key: string]: string } = {};
const keys = Object.keys(env).sort();
for (const key of keys) {
variables[key] = env[key];
}
return { Variables: variables };
}
}