Enabling API Gateway Access Logs Managed by a SAM Project
Overview
This is a story about the trouble I ran into when enabling access logs for an API Gateway managed by a SAM project, in order to comply with the AWS Config rule api-gw-execution-logging-enabled.
For an API Gateway created with Events.*.Type = Api, you cannot configure access log enablement within SAM.
I investigated how to enable access logs for the API Gateway.
SAM template.yml configuration
1 | Resources: |
Conclusion
As of 2023-07-07, the only option is to create a new API Gateway and switch over to it.
In that case, unless you use a custom domain, the API Gateway domain will change.
By separately creating an API Gateway such as Type: AWS::Serverless::Api and referencing it via RestApiId, I was able to enable access logs and X-Ray Tracing.
1 | Resources: |
If you cannot accept a domain change, it is possible to enable access logs from the console.
I have confirmed that the above settings remain unchanged even after sam deploy.
What I Tried (List of Failures)
When I defined it with AWS::Serverless::Api and tried to import the API Gateway, the following error occurred:
1
ResourceTypes [AWS::Serverless::Api] are not supported for Import
Reference: Resources that support import and drift detection operations
When I defined it with AWS::ApiGateway::RestApi and tried to import the API Gateway, the following error occurred:
1
You have modified resources [ServerlessRestApi, ServerlessRestApiDeployment2ababeb14f, ServerlessRestApiProdStage, FunctionMessagePermissionProd] in your template that are not being imported. Update, create or delete operations cannot be executed during import operations.
It seems the settings for other resources were insufficient.
When I tried to import only AWS::ApiGateway::Stage, the following error occurred:
1
abcd1a2b3c|Prod already exists in stack arn:aws:cloudformation:ap-northeast-1:123456789012:stack/Bot-Stack/e50458c0-1234-12ab-a12f-123a4b5c6d7e
That’s all.
I hope this is helpful.
Enabling API Gateway Access Logs Managed by a SAM Project
https://kenzo0107.github.io/en/2023/07/07/aws-sam-enable-apigateway-accesslog/