-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcustom-resource-provider-base.ts
283 lines (244 loc) · 8.78 KB
/
custom-resource-provider-base.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as path from 'path';
import { Construct } from 'constructs';
import * as fs from 'fs-extra';
import { CustomResourceProviderOptions, INLINE_CUSTOM_RESOURCE_CONTEXT } from './shared';
import * as cxapi from '../../../cx-api';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
import { CfnResource } from '../cfn-resource';
import { Duration } from '../duration';
import { FileSystem } from '../fs';
import { PolicySynthesizer, getPrecreatedRoleConfig } from '../helpers-internal';
import { Lazy } from '../lazy';
import { Size } from '../size';
import { Stack } from '../stack';
import { Token } from '../token';
const ENTRYPOINT_FILENAME = '__entrypoint__';
const ENTRYPOINT_NODEJS_SOURCE = path.join(__dirname, '..', '..', '..', 'custom-resource-handlers', 'dist', 'core', 'nodejs-entrypoint-handler', 'index.js');
/**
* Initialization properties for `CustomResourceProviderBase`
*/
export interface CustomResourceProviderBaseProps extends CustomResourceProviderOptions {
/**
* 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 name to use for the provider.
*/
readonly runtimeName: string;
}
/**
* Base class for creating a custom resource provider
*/
export abstract class CustomResourceProviderBase extends Construct {
/**
* The hash of the lambda code backing this provider. Can be used to trigger updates
* on code changes, even when the properties of a custom resource remain unchanged.
*/
public get codeHash(): string {
if (!this._codeHash) {
throw new Error('This custom resource uses inlineCode: true and does not have a codeHash');
}
return this._codeHash;
}
private _codeHash?: string;
private policyStatements?: any[];
private role?: CfnResource;
/**
* The ARN of the provider's AWS Lambda function which should be used as the `serviceToken` when defining a custom
* resource.
*/
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: CustomResourceProviderBaseProps) {
super(scope, id);
const stack = Stack.of(scope);
// 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`);
}
if (props.policyStatements) {
for (const statement of props.policyStatements) {
this.addToRolePolicy(statement);
}
}
const { code, codeHandler, metadata } = this.createCodePropAndMetadata(props, stack);
const config = getPrecreatedRoleConfig(this, `${this.node.path}/Role`);
const assumeRolePolicyDoc = [{ Action: 'sts:AssumeRole', Effect: 'Allow', Principal: { Service: 'lambda.amazonaws.com' } }];
const managedPolicyArn = 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole';
// need to initialize this attribute, but there should never be an instance
// where config.enabled=true && config.preventSynthesis=true
this.roleArn = '';
if (config.enabled) {
// gives policyStatements a chance to resolve
this.node.addValidation({
validate: () => {
PolicySynthesizer.getOrCreate(this).addRole(`${this.node.path}/Role`, {
missing: !config.precreatedRoleName,
roleName: config.precreatedRoleName ?? id+'Role',
managedPolicies: [{ managedPolicyArn: managedPolicyArn }],
policyStatements: this.policyStatements ?? [],
assumeRolePolicy: assumeRolePolicyDoc as any,
});
return [];
},
});
this.roleArn = Stack.of(this).formatArn({
region: '',
service: 'iam',
resource: 'role',
resourceName: config.precreatedRoleName,
});
}
if (!config.preventSynthesis) {
this.role = new CfnResource(this, 'Role', {
type: 'AWS::IAM::Role',
properties: {
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: assumeRolePolicyDoc,
},
ManagedPolicyArns: [
{ 'Fn::Sub': managedPolicyArn },
],
Policies: Lazy.any({ produce: () => this.renderPolicies() }),
},
});
this.roleArn = Token.asString(this.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: code,
Timeout: timeout.toSeconds(),
MemorySize: memory.toMebibytes(),
Handler: codeHandler,
Role: this.roleArn,
Runtime: props.runtimeName,
Environment: this.renderEnvironmentVariables(props.environment),
Description: props.description ?? undefined,
},
});
if (this.role) {
handler.addDependency(this.role);
}
if (metadata) {
Object.entries(metadata).forEach(([k, v]) => handler.addMetadata(k, v));
}
this.serviceToken = Token.asString(handler.getAtt('Arn'));
}
/**
* Add an IAM policy statement to the inline policy of the
* provider's lambda function's role.
*
* **Please note**: this is a direct IAM JSON policy blob, *not* a `iam.PolicyStatement`
* object like you will see in the rest of the CDK.
*
*
* @example
* declare const myProvider: CustomResourceProvider;
*
* myProvider.addToRolePolicy({
* Effect: 'Allow',
* Action: 's3:GetObject',
* Resource: '*',
* });
*/
public addToRolePolicy(statement: any): void {
if (!this.policyStatements) {
this.policyStatements = [];
}
this.policyStatements.push(statement);
}
private renderPolicies() {
if (!this.policyStatements) {
return undefined;
}
const policies = [{
PolicyName: 'Inline',
PolicyDocument: {
Version: '2012-10-17',
Statement: this.policyStatements,
},
}];
return policies;
}
private renderEnvironmentVariables(env?: { [key: string]: string }) {
if (!env || Object.keys(env).length === 0) {
return undefined;
}
env = { ...env }; // Copy
// Always use regional endpoints
env.AWS_STS_REGIONAL_ENDPOINTS = 'regional';
// 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 };
}
/**
* Returns the code property for the custom resource as well as any metadata.
* If the code is to be uploaded as an asset, the asset gets created in this function.
*/
private createCodePropAndMetadata(props: CustomResourceProviderBaseProps, stack: Stack): {
code: Code;
codeHandler: string;
metadata?: {[key: string]: string};
} {
let codeHandler = 'index.handler';
const inlineCode = this.node.tryGetContext(INLINE_CUSTOM_RESOURCE_CONTEXT);
if (!inlineCode) {
const stagingDirectory = FileSystem.mkdtemp('cdk-custom-resource');
fs.copySync(props.codeDirectory, stagingDirectory, { filter: (src, _dest) => !src.endsWith('.ts') });
if (props.useCfnResponseWrapper ?? true) {
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(stagingDirectory, `${ENTRYPOINT_FILENAME}.js`));
codeHandler = `${ENTRYPOINT_FILENAME}.handler`;
}
const staging = new AssetStaging(this, 'Staging', {
sourcePath: stagingDirectory,
});
const assetFileName = staging.relativeStagedPath(stack);
const asset = stack.synthesizer.addFileAsset({
fileName: assetFileName,
sourceHash: staging.assetHash,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
});
this._codeHash = staging.assetHash;
fs.rmSync(stagingDirectory, { recursive: true, force: true });
return {
code: {
S3Bucket: asset.bucketName,
S3Key: asset.objectKey,
},
codeHandler,
metadata: this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT) ? {
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: assetFileName,
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code',
} : undefined,
};
}
return {
code: {
ZipFile: fs.readFileSync(path.join(props.codeDirectory, 'index.js'), 'utf-8'),
},
codeHandler,
};
}
}
export type Code = {
ZipFile: string;
} | {
S3Bucket: string;
S3Key: string;
};