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

integ-tests-alpha: assertions provider function is not working #26359

Closed
WinterYukky opened this issue Jul 14, 2023 · 2 comments · Fixed by #26400
Closed

integ-tests-alpha: assertions provider function is not working #26359

WinterYukky opened this issue Jul 14, 2023 · 2 comments · Fixed by #26400
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort p1

Comments

@WinterYukky
Copy link
Contributor

Describe the bug

After marged #26271, Asserstions lambda function throw error at initialize phase and not working. This reason of problem is maybe to import aws-cdk-lib/custom-resources to use aws-sdk v2-v3 translate features.
aws-cdk-lib/custom-resources exports the some constructs, so when import the package, then bundle size is too large and lambda function is not working.

INIT_START Runtime Version: nodejs:18.v8	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:2755dc322c8dbb64760145d6403d14432af527bf4dd3cf03713aae10e0f8b552
--
2023-07-13T05:49:25.501Z	undefined	ERROR	Uncaught Exception 	{     "errorType": "TypeError",     "errorMessage": "Class extends value undefined is not a constructor or null",     "stack": [         "TypeError: Class extends value undefined is not a constructor or null",         "    at ../../aws-cdk-lib/core/lib/cfn-condition.ts (/var/task/index.js:796282:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/private/metadata-resource.ts (/var/task/index.js:807561:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/private/synthesis.ts (/var/task/index.js:814938:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/app.ts (/var/task/index.js:814966:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/stack.ts (/var/task/index.js:816570:5)",         "    at __init (/var/task/index.js:9:56)"     ] }
2023-07-13T05:49:38.254Z	undefined	ERROR	Uncaught Exception 	{     "errorType": "TypeError",     "errorMessage": "Class extends value undefined is not a constructor or null",     "stack": [         "TypeError: Class extends value undefined is not a constructor or null",         "    at ../../aws-cdk-lib/core/lib/cfn-condition.ts (/var/task/index.js:796282:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/private/metadata-resource.ts (/var/task/index.js:807561:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/private/synthesis.ts (/var/task/index.js:814938:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/app.ts (/var/task/index.js:814966:5)",         "    at __init (/var/task/index.js:9:56)",         "    at ../../aws-cdk-lib/core/lib/stack.ts (/var/task/index.js:816570:5)",         "    at __init (/var/task/index.js:9:56)"     ] }
START RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f Version: $LATEST
Unknown application error occurredRuntime.Unknown
END RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f
REPORT RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f	Duration: 13083.91 ms	Billed Duration: 13084 ms	Memory Size: 128 MB	Max Memory Used: 128 MB

Expected Behavior

Assertions function is working.

Current Behavior

Assertions function is not working.

  • handler size 26 MB is too large
  • runtime memory used 128 MB / 128 MB
  • error message is "Unknown application error occurredRuntime.Unknown"

Reproduction Steps

  1. Clone latest aws-cdk git repository
  2. yarn
  3. yarn build --skip-test
  4. cd packages/@aws-cdk-testing/framework-integ
  5. yarn integ --force test/aws-apigateway/test/integ.lambda-api.js

Possible Solution

Don't import aws-cdk-lib/custom-resources. Instead, consider the following pattern.

Additional Information/Context

bundle size

$ cd packages/@aws-cdk/integ-tests-alpha
$ yarn bundle
yarn run v1.22.19
$ esbuild --bundle lib/assertions/providers/lambda-handler/index.ts --target=node14 --platform=node --outfile=lib/assertions/providers/lambda-handler.bundle/index.js
  <Skip build messages>
  lib/assertions/providers/lambda-handler.bundle/index.js  25.8mb ⚠️

Done in 0.62s.

CDK CLI Version

2.88.0 (not release yet)

Framework Version

2.88.0 (not release yet)

Node.js Version

v18.15.0

OS

Amazon Linux 2

Language

Typescript

Language Version

TypeScript (5.1.3)

Other information

No response

@WinterYukky WinterYukky added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 14, 2023
@github-actions github-actions bot added the @aws-cdk/assertions Related to the @aws-cdk/assertv2 package label Jul 14, 2023
@pahud pahud added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 14, 2023
@pahud
Copy link
Contributor

pahud commented Jul 14, 2023

Thank you @WinterYukky for the detailed report.

@mergify mergify bot closed this as completed in #26400 Jul 18, 2023
mergify bot pushed a commit that referenced this issue Jul 18, 2023
## Summary
Assertions handler is too large and not working. This PR extracts the SDK v2 to v3 features into a separate package.
Reduces bundled size from `25.8mb` to `1.9mb` and restores the handlers functionality.

```bash
$ esbuild --bundle lib/assertions/providers/lambda-handler/index.ts --target=node14 --platform=node --outfile=lib/assertions/providers/lambda-handler.bundle/index.js

  lib/assertions/providers/lambda-handler.bundle/index.js  1.9mb ⚠️
```

## Why need this change?
After merged #26271, Assertions lambda function throw error at initialize phase and not working. This reason of problem is maybe to import `aws-cdk-lib/custom-resources` to use aws-sdk v2-v3 translate features. 
`aws-cdk-lib/custom-resources` exports the some constructs, so when import the package, then bundle size is too large and lambda function is not working.

```log
INIT_START Runtime Version: nodejs:18.v8	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:2755dc322c8dbb64760145d6403d14432af527bf4dd3cf03713aae10e0f8b552
--
2023-07-13T05:49:25.501Z	undefined	ERROR	Uncaught Exception     {
    "errorType": "TypeError",
    "errorMessage": "Class extends value undefined is not a constructor or null",
    "stack": [
        "TypeError: Class extends value undefined is not a constructor or null",
        "    at ../../aws-cdk-lib/core/lib/cfn-condition.ts (/var/task/index.js:796267:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/private/metadata-resource.ts (/var/task/index.js:807546:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/private/synthesis.ts (/var/task/index.js:814923:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/app.ts (/var/task/index.js:814951:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/stack.ts (/var/task/index.js:816555:5)",
        "    at __init (/var/task/index.js:9:56)"
    ]
}
START RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f Version: $LATEST
Unknown application error occurredRuntime.Unknown
END RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f
REPORT RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f	Duration: 13083.91 ms	Billed Duration: 13084 ms	Memory Size: 128 MB	Max Memory Used: 128 MB
```

## Solution

Extract the SDK v2 to v3 features into a new private package that can be bundled safely.
Also required changes to the `aws-custom-resource` v3 handler so it can used the shared code.
To make sure that the bundled files are free of start-up errors, I have added them into a post build script.

## Additional changes

**Update to Encode/Decode logic**
All parameters passed to a Custom Resource will be converted to string when passed to the Lambda Function handler. 
This is a feature/limitation by CloudFormation.
The previous implementation supported special handling for boolean values via a magic string.
However with the SDK v3 parameter types on API Calls are more strictly enforced, e.g. we can't pass a string anymore when a number is expected.
Therefore the encoding/decoding logic has been updated to indiscriminately encode _all_ parameters with `JSON.stringify()` and reverse the operation inside the assertions provider.

**Lambda Invoke Payload**
Withe the SDK v3, the type of the Payload returned from a Lambda Invoke operation is now a Buffer.
This makes sense from an SDK perspective since the returned payload might be a binary.
However assertions can only deal with JSON payloads, so we attempt to convert and parse the payload into a JSON object.
If that fails it stays unchanged.

## Acknowledgements 

Thanks @WinterYukky  for the initial investigation!

Closes #26359
Replaces #26360

----

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

bmoffatt pushed a commit to bmoffatt/aws-cdk that referenced this issue Jul 29, 2023
## Summary
Assertions handler is too large and not working. This PR extracts the SDK v2 to v3 features into a separate package.
Reduces bundled size from `25.8mb` to `1.9mb` and restores the handlers functionality.

```bash
$ esbuild --bundle lib/assertions/providers/lambda-handler/index.ts --target=node14 --platform=node --outfile=lib/assertions/providers/lambda-handler.bundle/index.js

  lib/assertions/providers/lambda-handler.bundle/index.js  1.9mb ⚠️
```

## Why need this change?
After merged aws#26271, Assertions lambda function throw error at initialize phase and not working. This reason of problem is maybe to import `aws-cdk-lib/custom-resources` to use aws-sdk v2-v3 translate features. 
`aws-cdk-lib/custom-resources` exports the some constructs, so when import the package, then bundle size is too large and lambda function is not working.

```log
INIT_START Runtime Version: nodejs:18.v8	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:2755dc322c8dbb64760145d6403d14432af527bf4dd3cf03713aae10e0f8b552
--
2023-07-13T05:49:25.501Z	undefined	ERROR	Uncaught Exception     {
    "errorType": "TypeError",
    "errorMessage": "Class extends value undefined is not a constructor or null",
    "stack": [
        "TypeError: Class extends value undefined is not a constructor or null",
        "    at ../../aws-cdk-lib/core/lib/cfn-condition.ts (/var/task/index.js:796267:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/private/metadata-resource.ts (/var/task/index.js:807546:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/private/synthesis.ts (/var/task/index.js:814923:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/app.ts (/var/task/index.js:814951:5)",
        "    at __init (/var/task/index.js:9:56)",
        "    at ../../aws-cdk-lib/core/lib/stack.ts (/var/task/index.js:816555:5)",
        "    at __init (/var/task/index.js:9:56)"
    ]
}
START RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f Version: $LATEST
Unknown application error occurredRuntime.Unknown
END RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f
REPORT RequestId: b6da27f5-d7ff-4f76-807b-c7f5d1f02d1f	Duration: 13083.91 ms	Billed Duration: 13084 ms	Memory Size: 128 MB	Max Memory Used: 128 MB
```

## Solution

Extract the SDK v2 to v3 features into a new private package that can be bundled safely.
Also required changes to the `aws-custom-resource` v3 handler so it can used the shared code.
To make sure that the bundled files are free of start-up errors, I have added them into a post build script.

## Additional changes

**Update to Encode/Decode logic**
All parameters passed to a Custom Resource will be converted to string when passed to the Lambda Function handler. 
This is a feature/limitation by CloudFormation.
The previous implementation supported special handling for boolean values via a magic string.
However with the SDK v3 parameter types on API Calls are more strictly enforced, e.g. we can't pass a string anymore when a number is expected.
Therefore the encoding/decoding logic has been updated to indiscriminately encode _all_ parameters with `JSON.stringify()` and reverse the operation inside the assertions provider.

**Lambda Invoke Payload**
Withe the SDK v3, the type of the Payload returned from a Lambda Invoke operation is now a Buffer.
This makes sense from an SDK perspective since the returned payload might be a binary.
However assertions can only deal with JSON payloads, so we attempt to convert and parse the payload into a JSON object.
If that fails it stays unchanged.

## Acknowledgements 

Thanks @WinterYukky  for the initial investigation!

Closes aws#26359
Replaces aws#26360

----

*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/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
2 participants