Skip to content

Commit

Permalink
fix(core): CustomResourceProvider assets are staged in node_modules (#…
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
otaviomacedo authored Jul 18, 2022
1 parent 7c4cd96 commit 901b225
Show file tree
Hide file tree
Showing 17 changed files with 1,012 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as fs from 'fs';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import * as fse from 'fs-extra';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
import { CfnResource } from '../cfn-resource';
import { Duration } from '../duration';
import { FileSystem } from '../fs';
import { Lazy } from '../lazy';
import { Size } from '../size';
import { Stack } from '../stack';
Expand Down Expand Up @@ -200,16 +202,17 @@ export class CustomResourceProvider extends Construct {

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 stagingDirectory = FileSystem.mkdtemp('cdk-custom-resource');
fse.copySync(props.codeDirectory, stagingDirectory);
fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE, path.join(stagingDirectory, `${ENTRYPOINT_FILENAME}.js`));

const staging = new AssetStaging(this, 'Staging', {
sourcePath: props.codeDirectory,
sourcePath: stagingDirectory,
});

const assetFileName = staging.relativeStagedPath(stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ describe('custom resource provider', () => {
});

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

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

expect(fs.existsSync(path.join(sourcePath, '__entrypoint__.js'))).toEqual(true);

expect(cfn).toEqual({
Resources: {
CustomMyResourceTypeCustomResourceProviderRoleBD5E655F: {
Expand Down Expand Up @@ -139,9 +141,12 @@ describe('custom resource provider', () => {
// Then
const lambda = toCloudFormation(stack).Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
expect(lambda).toHaveProperty('Metadata');
expect(lambda.Metadata).toEqual({
'aws:asset:path': `${__dirname}/mock-provider`,

expect(lambda.Metadata).toMatchObject({
'aws:asset:property': 'Code',

// The asset path should be a temporary folder prefixed with 'cdk-custom-resource'
'aws:asset:path': expect.stringMatching(/^.*\/cdk-custom-resource\w{6}\/?$/),
});

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@ const app = new App({
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
});
new PipelineStack(app, 'PipelineStack', {
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
});
new PipelineStack(app, 'PipelineStack');
app.synth();
4 changes: 1 addition & 3 deletions packages/@aws-cdk/pipelines/test/integ.newpipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,5 @@ const app = new App({
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
});
new PipelineStack(app, 'PipelineStack', {
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
});
new PipelineStack(app, 'PipelineStack');
app.synth();

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading

0 comments on commit 901b225

Please sign in to comment.