Skip to content

fix(aws-apigateway): CloudWatch logging should be disabled by default (under feature flag)#21546

Merged
mergify[bot] merged 7 commits intoaws:mainfrom
corymhall:corymhall/apigateway/disable-cloudwatchrole
Aug 12, 2022
Merged

fix(aws-apigateway): CloudWatch logging should be disabled by default (under feature flag)#21546
mergify[bot] merged 7 commits intoaws:mainfrom
corymhall:corymhall/apigateway/disable-cloudwatchrole

Conversation

@corymhall
Copy link
Copy Markdown
Contributor

@corymhall corymhall commented Aug 10, 2022

Currently when you create a RestApi cloudwatch logging is enabled by
default. This will create an IAM role and a AWS::ApiGateway::Account
resource, which is what is used to allow API Gateway to write API logs
to CloudWatch logs. There can only be a single API Gateway account per
AWS environment (account/region), but CloudFormation will not throw an
error if you try to create additional accounts. Instead it will update
the existing account with the new configuration.

This can cause issues if customers create more than 1 RestApi.
The following scenario is an example.

  1. Create a single RestApi
    A new AWS::ApiGateway::Account and IAM role is created.
  2. Create a second RestApi
    Another AWS::ApiGateway::Account/IAM role is created which
    overwrites the first one. The first RestApi now uses the account/role
    created by this RestApi.
  3. Delete the second RestApi
    The AWS::ApiGateway::Account/IAM role is deleted along with the second
    RestApi. The first RestApi no longer has access to write to
    CloudWatch logs.

Because of this behavior, the correct thing to do is to disable
CloudWatch logs by default so that the user has to create the global
resource separately. This new behavior is behind a feature flag
@aws-cdk/aws-apigateway:disableCloudWatchLogs.

In addition, the default retention policy for both the API Gateway
account and IAM role has been set to RETAIN so that existing
implementations that do not use the feature flag can avoid the above
scenario. The resources will be unmanaged, but existing RestApis will
not break.

I've updated all the existing integration tests to use the old behavior by explicitly setting
cloudWatchLogs: true. I then added a new integration test for the new behavior.

closes #10878


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

… (under feature flag)

Currently when you create a RestApi cloudwatch logging is enabled by
default. This will create an IAM role and a `AWS::ApiGateway::Account`
resource, which is what is used to allow API Gateway to write API logs
to CloudWatch logs. There can only be a single API Gateway account per
AWS environment (account/region), but CloudFormation will not throw an
error if you try to create additional accounts. Instead it will update
the existing account with the new configuration.

This can cause issues if customers create more than 1 RestApi.
The following scenario is an example.

1. Create a single `RestApi`
A new `AWS::ApiGateway::Account` and IAM role is created.
2. Create a second `RestApi`
Another `AWS::ApiGateway::Account`/IAM role is created which
_overwrites_ the first one. The first RestApi now uses the account/role
created by this `RestApi`.
3. Delete the second `RestApi`
The `AWS::ApiGateway::Account`/IAM role is deleted along with the second
`RestApi`. The first `RestApi` no longer has access to write to
CloudWatch logs.

Because of this behavior, the correct thing to do is to disable
CloudWatch logs by default so that the user has to create the global
resource separately. This new behavior is behind a feature flag
`@aws-cdk/aws-apigateway:disableCloudWatchLogs`.

In addition, the default retention policy for both the API Gateway
account and IAM role has been set to `RETAIN` so that existing
implementations that do not use the feature flag can avoid the above
scenario. The resources will be unmanaged, but existing RestApis will
not break.

closes #10878
@gitpod-io
Copy link
Copy Markdown

gitpod-io bot commented Aug 10, 2022

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Aug 10, 2022
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 10, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team August 10, 2022 18:41
Comment on lines +847 to +848
set to `false`. To change this default behavior you can enable the feature flag
`@aws-cdk/aws-apigateway:disableCloudWatchRole`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checking - is this how we want to phrase feature flagged defaults? All new apps will have the flag enabled and it might confuse people. Maybe it'd be better to directly call out that the FF in the first sentence?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Just pushed an update, let me know what you think or feel free to suggest different wording.

Comment on lines +5 to +20
export class Test extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
const api = new apigateway.RestApi(this, 'my-api', {
retainDeployments: true,
});
api.root.addMethod('GET'); // must have at least one method or an API definition
}
}

const app = new cdk.App();
new IntegTest(app, 'cloudwatch-logs-disabled', {
testCases: [
new Test(app, 'default-api'),
],
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not missing the FF in the context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All features flags are enabled by default for integration tests.

@TheRealAmazonKendra
Copy link
Copy Markdown
Contributor

Might this be a good opportunity to update the integration tests to the new style?

@TheRealAmazonKendra TheRealAmazonKendra added the pr/do-not-merge This PR should not be merged at this time. label Aug 11, 2022
Copy link
Copy Markdown
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the tests certainly isn't mandatory for this change, so giving approval either way, but adding do-not-merge just in case you want to do that first. If not, we can remove the label and let this merge.

@corymhall
Copy link
Copy Markdown
Contributor Author

Might this be a good opportunity to update the integration tests to the new style?

I should have been the one to suggest this 🤣

@corymhall corymhall removed the pr/do-not-merge This PR should not be merged at this time. label Aug 12, 2022
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 12, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 12, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 746b9e6
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 12, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ApiGateway] RestApi updates account level role used for ApiGateway CloudWatch logging

4 participants