Skip to content

Commit 901b225

Browse files
authored
fix(core): CustomResourceProvider assets are staged in node_modules (#20953)
Instead of using the source code directory as a staging directory (which, from the point of view of the consumer, is inside the `node_modules` directory), create a temporary directory for staging. Fixes #17460. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7c4cd96 commit 901b225

File tree

17 files changed

+1012
-26
lines changed

17 files changed

+1012
-26
lines changed

packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import * as cxapi from '@aws-cdk/cx-api';
44
import { Construct } from 'constructs';
5+
import * as fse from 'fs-extra';
56
import { AssetStaging } from '../asset-staging';
67
import { FileAssetPackaging } from '../assets';
78
import { CfnResource } from '../cfn-resource';
89
import { Duration } from '../duration';
10+
import { FileSystem } from '../fs';
911
import { Lazy } from '../lazy';
1012
import { Size } from '../size';
1113
import { Stack } from '../stack';
@@ -200,16 +202,17 @@ export class CustomResourceProvider extends Construct {
200202

201203
const stack = Stack.of(scope);
202204

203-
// copy the entry point to the code directory
204-
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(props.codeDirectory, `${ENTRYPOINT_FILENAME}.js`));
205-
206205
// verify we have an index file there
207206
if (!fs.existsSync(path.join(props.codeDirectory, 'index.js'))) {
208207
throw new Error(`cannot find ${props.codeDirectory}/index.js`);
209208
}
210209

210+
const stagingDirectory = FileSystem.mkdtemp('cdk-custom-resource');
211+
fse.copySync(props.codeDirectory, stagingDirectory);
212+
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(stagingDirectory, `${ENTRYPOINT_FILENAME}.js`));
213+
211214
const staging = new AssetStaging(this, 'Staging', {
212-
sourcePath: props.codeDirectory,
215+
sourcePath: stagingDirectory,
213216
});
214217

215218
const assetFileName = staging.relativeStagedPath(stack);

packages/@aws-cdk/core/test/custom-resource-provider/custom-resource-provider.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ describe('custom resource provider', () => {
1919
});
2020

2121
// THEN
22-
expect(fs.existsSync(path.join(TEST_HANDLER, '__entrypoint__.js'))).toEqual(true);
2322
const cfn = toCloudFormation(stack);
2423

2524
// The asset hash constantly changes, so in order to not have to chase it, just look
2625
// it up from the output.
2726
const staging = stack.node.tryFindChild('Custom:MyResourceTypeCustomResourceProvider')?.node.tryFindChild('Staging') as AssetStaging;
2827
const assetHash = staging.assetHash;
28+
const sourcePath = staging.sourcePath;
2929
const paramNames = Object.keys(cfn.Parameters);
3030
const bucketParam = paramNames[0];
3131
const keyParam = paramNames[1];
3232
const hashParam = paramNames[2];
3333

34+
expect(fs.existsSync(path.join(sourcePath, '__entrypoint__.js'))).toEqual(true);
35+
3436
expect(cfn).toEqual({
3537
Resources: {
3638
CustomMyResourceTypeCustomResourceProviderRoleBD5E655F: {
@@ -139,9 +141,12 @@ describe('custom resource provider', () => {
139141
// Then
140142
const lambda = toCloudFormation(stack).Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
141143
expect(lambda).toHaveProperty('Metadata');
142-
expect(lambda.Metadata).toEqual({
143-
'aws:asset:path': `${__dirname}/mock-provider`,
144+
145+
expect(lambda.Metadata).toMatchObject({
144146
'aws:asset:property': 'Code',
147+
148+
// The asset path should be a temporary folder prefixed with 'cdk-custom-resource'
149+
'aws:asset:path': expect.stringMatching(/^.*\/cdk-custom-resource\w{6}\/?$/),
145150
});
146151

147152
});

packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,5 @@ const app = new App({
5151
'@aws-cdk/core:newStyleStackSynthesis': '1',
5252
},
5353
});
54-
new PipelineStack(app, 'PipelineStack', {
55-
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
56-
});
54+
new PipelineStack(app, 'PipelineStack');
5755
app.synth();

packages/@aws-cdk/pipelines/test/integ.newpipeline.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,5 @@ const app = new App({
5656
'@aws-cdk/core:newStyleStackSynthesis': '1',
5757
},
5858
});
59-
new PipelineStack(app, 'PipelineStack', {
60-
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
61-
});
59+
new PipelineStack(app, 'PipelineStack');
6260
app.synth();

packages/@aws-cdk/pipelines/test/pipeline-security.integ.snapshot/asset.207d1c59082dd1ab722c445b190e0c2cbb20d6c3e981cf674a60159b56338e86/__entrypoint__.js

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/pipelines/test/pipeline-security.integ.snapshot/asset.207d1c59082dd1ab722c445b190e0c2cbb20d6c3e981cf674a60159b56338e86/index.js

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/pipelines/test/pipeline-with-assets-single-upload.integ.snapshot/asset.207d1c59082dd1ab722c445b190e0c2cbb20d6c3e981cf674a60159b56338e86/__entrypoint__.js

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)