aws / aws/aws-cdk

(apigateway): metricsEnabled can be inconsistent across deployments

Open
#24,096 1 comment 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-apigateway bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

API-Gateway's RestApi defines two mechanisms in which to specify `metricsEnabled`:

1. Via StageOptions: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.StageOptions.html
2. Via MethodDeploymentOptions: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodDeploymentOptions.html

If we define a RestApi object with cloudwatch metrics enabled:

```ts
const api = new RestApi(this, 'Api', {
restApiName: 'MyApi',
deployOptions: {
metricsEnabled: true,
methodOptions: {
'/*/*': {
throttlingRateLimit: 200,
throttlingBurstLimit: 600,
},
},
},
});
```

The generated CFN template contains the following snippet:

```json
"MethodSettings": [
{
"DataTraceEnabled": false,
"HttpMethod": "*",
"MetricsEnabled": true,
"ResourcePath": "/*"
},
{
"DataTraceEnabled": false,
"HttpMethod": "*",
"ResourcePath": "/*",
"ThrottlingBurstLimit": 600,
"ThrottlingRateLimit": 200
}
],
```

The code above is logically equivalent to:

```ts
const api = new RestApi(this, 'Api', {
restApiName: 'MyApi',
deployOptions: {
metricsEnabled: true,
throttlingRateLimit: 200,
throttlingBurstLimit: 600,
},
});
```

However, the CFN template produced is:

```json
"MethodSettings": [
{
"DataTraceEnabled": false,
"HttpMethod": "*",
"MetricsEnabled": true,
"ResourcePath": "/*",
"ThrottlingBurstLimit": 600,
"ThrottlingRateLimit": 200
}
],
```

### Expected Behavior

Both code snippets should produce the same cloudformation template, as they are logically equivalent. Meaning - `metricsEnabled` is always true in this particular case.

### Current Behavior

When deploying the first example, `metricsEnabled` can be true or false, and this can change (seemingly randomly) on each new deployment.

### Reproduction Steps

See bug description for details.

### Possible Solution

When compiling the CFN template, it would be awesome if we could either:

1. Fail synthesis because we are providing two conflicting MethodSettings.
2. Recognize these are the same, and make a call on behalf of the user to combine these together
3. If `metricsEnabled`is set at the top level, we should make `metricsEnabled`in `methodOptions`a required property, requiring the user to specify whether or not cloudwatch metrics should be enabled.

### Additional Information/Context

_No response_

### CDK CLI Version

2.64.0 (build fb67c77)

### Framework Version

_No response_

### Node.js Version

14.19.2

### OS

macOS

### Language

Typescript

### Language Version

4.9.5

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the aws_apigateway RestApi deployOptions handling and compare StageOptions with MethodDeploymentOptions during CloudFormation synthesis. Reproduce both snippets and inspect the generated MethodSettings; done means logically equivalent settings no longer produce inconsistent MetricsEnabled behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.