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: awsApiCall assertion is not handling dates properly for GetMetricData with CloudWatch #27962

Closed
jose-clickup opened this issue Nov 13, 2023 · 2 comments · Fixed by #28398
Labels
@aws-cdk/custom-resources Related to AWS CDK Custom Resources @aws-cdk/integ-tests bug This issue is a bug. effort/medium Medium work item – several days of effort p1

Comments

@jose-clickup
Copy link

Describe the bug

When attempting to use the IntegTest(...).assertion.awsApiCall() method with CloudWatch and the GetMetricData function, it returns the error:

INFO	TypeError: input.StartTime.toISOString is not a function
    at serializeAws_queryGetMetricDataInput (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:2506:48)
    at serializeAws_queryGetMetricDataCommand (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:224:12)
    at serialize (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/commands/GetMetricDataCommand.js:30:71)
    at /var/runtime/node_modules/@aws-sdk/middleware-serde/dist-cjs/serializerMiddleware.js:12:27
    at /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:28
    at CloudWatchClient.send (/var/runtime/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:20:20)
    at AwsApiCallHandler.processEvent (/var/task/index.js:32265:35)
    at AwsApiCallHandler.handle (/var/task/index.js:31915:37)
    at Runtime.handler (/var/task/index.js:32320:35)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1147:29)

The awsApiCall() method executes an API call from the Lambda custom resource generated by the assertion. Here, the lib parses the parameters it receives with the encode() function which converts all values to strings and pass them as string properties to the Lambda function.

The issue comes up when we use an API call that requires Date() types like in this case for the GetMetrciData request. For Date() types, it returns a JSON string representation of the timestamp (e.g. "2023-11-08T14:53:09.225Z"). As the Lambda function uses @aws-sdk to make the API call, it expects a Date() object that will be converted to ISO string, but in this scenario, it receives a string type, and because of this, it returns the error TypeError: input.StartTime.toISOString is not a function

Expected Behavior

Get a response from the AWS API after running the assertion integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params);****

Current Behavior

After running the integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params); the code fails because the Date() object is not parsed properly.
INFO TypeError: input.StartTime.toISOString is not a function

Reproduction Steps

  1. Create the following files:

test/integ.cloudwatch-call.ts

import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { GetMetricDataCommandInput } from '@aws-sdk/client-cloudwatch' 
import { App } from 'aws-cdk-lib';

const app = new App();
const integ = new IntegTest(app, 'integ', {
    testCases: [],
});
const params: GetMetricDataCommandInput = {
    MetricDataQueries: [
        {
            Id: "id",
            Label: "label",
            MetricStat: {
                Metric: {
                    Namespace: "namespace",
                    MetricName: "metric",
                    Dimensions: [
                        {
                            Name: "key",
                            Value: "value",
                        },
                    ],
                },
                Period: 60,
                Stat: "MAX",
            },
        },
    ],
    StartTime: new Date(),
    EndTime: new Date(),
};
integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params);

package.json

{
  "name": "integ-tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-cdk/integ-runner": "^2.106.1-alpha.0",
    "@aws-cdk/integ-tests-alpha": "^2.106.1-alpha.0",
    "@aws-sdk/client-cloudwatch": "^3.449.0",
    "aws-cdk-lib": "^2.106.1",
    "constructs": "^10.3.0",
    "esbuild": "^0.19.5",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}
  1. Run yarn install
  2. Run npx integ-runner --parallel-regions us-west-2 --update-on-failed

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.106.1

Framework Version

No response

Node.js Version

v18.17.1

OS

macOS Version 14.1 (23B74)

Language

TypeScript

Language Version

No response

Other information

No response

@jose-clickup jose-clickup added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 13, 2023
@pahud
Copy link
Contributor

pahud commented Nov 14, 2023

Yes I can reproduce this error. Thank you for the report.

@pahud pahud added p1 @aws-cdk/custom-resources Related to AWS CDK Custom Resources effort/medium Medium work item – several days of effort needs-review and removed needs-triage This issue or PR still needs to be triaged. labels Nov 14, 2023
@mergify mergify bot closed this as completed in #28398 Mar 4, 2024
mergify bot pushed a commit that referenced this issue Mar 4, 2024
## Description
The following issue reports an error that occurs when calling an API that takes the `Date` type as a parameter, such as `GetMetricData` API, from a Custom Resource Lambda function, where the parameter is passed as `string` type to the AWS SDK.
#27962
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/cloudwatch/command/GetMetricDataCommand/#:~:text=Description-,EndTime,-Required

To resolve this error, the `string` type must be properly converted to `Date` type when calling the AWS SDK from Lambda.
In this PR, I added the conversion to Date type in the same way as the existing conversion to `number` and `Uint8Array` types.
`Uint8Array`: #27034
`number`: #27112

## Major changes
### `update-sdkv3-parameters-model.ts` script
If the type is `timestamp` in the `smithy` specification, write `d` to the state machine so that it can be converted to a Date type later.
https://smithy.io/2.0/spec/simple-types.html#timestamp

`update-sdkv3-parameters-model.sh` script was not called from anywhere, so I called it manually and updated the JSON file.
Please let me know if there is a problem.

### `sdk-v2-to-v3-adapter` module
I added code to convert value marked `d` in state machine to `Date` type.
If the conversion to `Date` type fails, the `Date` class does not throw an exception, so the error is handled in a slightly tricky way.
Also added a unit test for this process.

### `integ-tests-alpha` module
Added integ-test to verify that errors reported in the related issue have been resolved.
The IAM Policy added internally by the call to `adPolicyStatementFromSdkCall` looks like the following and does not call `GetMetricData` correctly, so the `addToRolePolicy` method was used to explicitly add a new Policy is added explicitly with the `addToRolePolicy` method.
```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "monitoring:GetMetricData"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        }
    ]
}
```
https://github.com/aws/aws-cdk/blob/1a9c30e55e58203bd0a61de82711cf10f1e04851/packages/aws-cdk-lib/custom-resources/lib/helpers-internal/sdk-v3-metadata.json#L174


fixes #27962

----

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

github-actions bot commented Mar 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/custom-resources Related to AWS CDK Custom Resources @aws-cdk/integ-tests bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants