Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): CustomResourceProvider assets are staged in node_modules #20953

Merged
merged 15 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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