aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.62k stars 3.91k forks source link

synthetics-alpha: Asset not found when Canary belongs to Stage #27089

Closed konokenj closed 1 year ago

konokenj commented 1 year ago

Describe the bug

When using Canary with custom test code asset in Stage, CDK fails to synthesize.

Expected Behavior

Successfully synthesized with code asset.

Current Behavior

Fail to synshesize with error:

Error: ENOENT: no such file or directory, lstat 'asset.1e64e462d93160eb5230b00e665705bfaf2299d9c302ab56dd093bccbe387c4f'
    at Object.lstatSync (node:fs:1668:3)
    at AssetCode.validateCanaryAsset (/path/to/project/node_modules/@aws-cdk/aws-synthetics-alpha/lib/code.ts:139:15)
    at AssetCode.bind (/path/to/project/node_modules/@aws-cdk/aws-synthetics-alpha/lib/code.ts:109:10)
    at Canary.createCode (/path/to/project/node_modules/@aws-cdk/aws-synthetics-alpha/lib/canary.ts:492:26)
    at new Canary (/path/to/project/node_modules/@aws-cdk/aws-synthetics-alpha/lib/canary.ts:319:18)
    at Object.<anonymous> (/path/to/project/bin/quick/canary-alpha.ts:24:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module.m._compile (/path/to/project/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .ts] (/path/to/project/node_modules/ts-node/src/index.ts:1621:12) {
  errno: -2,
  syscall: 'lstat',
  code: 'ENOENT',
  path: 'asset.1e64e462d93160eb5230b00e665705bfaf2299d9c302ab56dd093bccbe387c4f'
}

Subprocess exited with error 1

Reproduction Steps

1/ Make sure successfully synthesized Canary without Stage

import 'source-map-support/register';
import { App, Duration, RemovalPolicy, Stack, Stage } from 'aws-cdk-lib';
import * as synthetics from '@aws-cdk/aws-synthetics-alpha';
import { Bucket } from 'aws-cdk-lib/aws-s3';

const app = new App();
const stack = new Stack(app, 'CanryAlpha', {
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: 'us-west-2',
  },
});

const canaryBucket = new Bucket(stack, 'CanaryBucket', {
  autoDeleteObjects: true,
  removalPolicy: RemovalPolicy.DESTROY,
});

new synthetics.Canary(stack, 'Canary', {
  schedule: synthetics.Schedule.rate(Duration.minutes(1)),
  test: synthetics.Test.custom({
    code: synthetics.Code.fromAsset('lambda/canary-app'),
    handler: 'index.handler',
  }),
  runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_3_5,
  environmentVariables: {
    TARGETHOST: 'https://example.com',
    TARGETPATH: '/',
  },
  artifactsBucketLocation: { bucket: canaryBucket },
});

2/ Add Stage and make the Stack belong to it

+const stage = new Stage(app, 'Dev');
+const stack = new Stack(stage, 'CanryAlpha', {
-const stack = new Stack(app, 'CanryAlpha', {
Full code ```typescript import 'source-map-support/register'; import { App, Duration, RemovalPolicy, Stack, Stage } from 'aws-cdk-lib'; import * as synthetics from '@aws-cdk/aws-synthetics-alpha'; import { Bucket } from 'aws-cdk-lib/aws-s3'; const app = new App(); const stage = new Stage(app, 'Dev'); const stack = new Stack(stage, 'CanryAlpha', { env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: 'us-west-2', }, }); const canaryBucket = new Bucket(stack, 'CanaryBucket', { autoDeleteObjects: true, removalPolicy: RemovalPolicy.DESTROY, }); new synthetics.Canary(stack, 'Canary', { schedule: synthetics.Schedule.rate(Duration.minutes(1)), test: synthetics.Test.custom({ code: synthetics.Code.fromAsset('lambda/canary-app'), handler: 'index.handler', }), runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_3_5, environmentVariables: { TARGETHOST: 'https://example.com', TARGETPATH: '/', }, artifactsBucketLocation: { bucket: canaryBucket }, }); ```

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.94.0 (build 987c329)

Framework Version

No response

Node.js Version

v18.17.1

OS

macOS Ventura 13.5.2(22G91)

Language

Typescript

Language Version

TypeScript 5.2.2

Other information

No response

indrora commented 1 year ago

A few minor questions:

konokenj commented 1 year ago

@indrora Thank you for responding.

Have you packaged your canary as is described in the documentation?

Yes. It can be synthesized successfully with same code in aws-synthetics-alpha:2.38.1-alpha.0. This issue occured in upgrading it to 2.95.1-alpha.0.

image

Does using path.join(__dirname__, 'lambda/canary-app') for the asset path help?

Same error occurs with using it. Without Stage, synthesized successfully.

    code: synthetics.Code.fromAsset(path.join(__dirname, '../../lambda/canary-app')),

Also, I think other assets are able to synthesize without path.join.

kaizencc commented 1 year ago

I was able to reproduce this. Curious behavior. Hopefully I can find a fix soon, but thanks for reporting.

Also, I think other assets are able to synthesize without path.join.

path.join is just the best practice for specifying file paths in code. It should have no bearing on whether or not your app synthesizes, but makes running your app deterministic.

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.