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

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

Closed
konokenj opened this issue Sep 11, 2023 · 4 comments · Fixed by #27167
Closed

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

konokenj opened this issue Sep 11, 2023 · 4 comments · Fixed by #27167
Assignees
Labels
@aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@konokenj
Copy link
Contributor

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
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

@konokenj konokenj added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 11, 2023
@github-actions github-actions bot added the @aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics label Sep 11, 2023
@indrora
Copy link
Contributor

indrora commented Sep 11, 2023

A few minor questions:

  • Have you packaged your canary as is described in the documentation?
  • Does using path.join(__dirname__, 'lambda/canary-app') for the asset path help?

@indrora indrora added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Sep 11, 2023
@konokenj
Copy link
Contributor Author

@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.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Sep 12, 2023
@kaizencc
Copy link
Contributor

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.

@kaizencc kaizencc assigned kaizencc and unassigned indrora Sep 15, 2023
@kaizencc kaizencc added p1 effort/small Small work item – less than a day of effort labels Sep 15, 2023
@mergify mergify bot closed this as completed in #27167 Sep 18, 2023
mergify bot pushed a commit that referenced this issue Sep 18, 2023
#27167)

Fixes #27089.

Canaries inside stages would fail at synth time after #26291 was introduced. This was due to a misunderstanding of `assetOutdir`. It turns out that `this.asset.assetPath` is a relative path of `Stage.of(scope).outdir`, not of `Stage.of(scope).assetOutdir`. These two out directories would be equivalent at the root level, but if there were nested stages, `assetOutdir` would equal `../${outdir}`.

This diagram may help:

```
CLOUD ASSEMBLY ROOT
  +-- asset.12345abcdef/
  +-- assembly-Stage
    +-- MyStack.template.json
    +-- MyStack.assets.json <- will contain { "path": "../asset.12345abcdef" }
```


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

HBobertz pushed a commit that referenced this issue Sep 18, 2023
#27167)

Fixes #27089.

Canaries inside stages would fail at synth time after #26291 was introduced. This was due to a misunderstanding of `assetOutdir`. It turns out that `this.asset.assetPath` is a relative path of `Stage.of(scope).outdir`, not of `Stage.of(scope).assetOutdir`. These two out directories would be equivalent at the root level, but if there were nested stages, `assetOutdir` would equal `../${outdir}`.

This diagram may help:

```
CLOUD ASSEMBLY ROOT
  +-- asset.12345abcdef/
  +-- assembly-Stage
    +-- MyStack.template.json
    +-- MyStack.assets.json <- will contain { "path": "../asset.12345abcdef" }
```


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants