Orphan Log Group
This is not (I'm pretty sure) related to this bug: https://github.com/aws/serverless-application-model/issues/1216
Log group created by template with:
Type: 'AWS::Logs::LogGroup
DeletionPolicy: Delete
Properties:
RetentionInDays: 30
LogGroupName
The above is not a copy paste, not the real template.
The ApiGW ( 'AWS::Serverless::Api' ) ref's the log group like this:
AccessLogSetting: {
'DestinationArn': {
'Fn::Sub': '${WalletApiGatewayAccessLogGroup.Arn}'
},
'Format':"{'method':'$context.httpMethod','path':'$context.path','requestId':'$context.requestId','resourcePath':'$context.resourcePath','status':'$context.status','responseLatency':'$context.responseLatency','responseLength':'$context.responseLength','sourceIp':'$context.identity.sourceIp','xrayTraceId':'$context.xrayTraceId','requestTime':'$context.requestTime','gatewayStage':'$context.stage','protocol':'$context.protocol','gatewayErrorMsg':'$context.error.message','integrationErrorMsg':'$context.integration.error','integrationLatency':'$context.integrationLatency'}"
}
If I remove the above reference, deploy and then delete the stack, the log group is created, then deleted. If not, the stack deletes without error, and the log group is left behind as an orphan. The 'DeletionPolicy: Delete' changes nothing.
If I remove the log group def and leave the reference to the log group in the apigw, the log group is not eventually created like with a lambda. The deployment fails.
I think this is a bug. If both the apigw and the log group are defined and created in/by CF then deleting the stack should delete the entire stack.
The stack is deployed by an Ubuntu 18 vm in Azure DevOps using SAM cli v1.36.0
AWS Region us-east-2
Thanks for the report! I will investigate this.
fyi, I have moved on and won't have time to recreate this. I included the log group resource always, but if the env is dev/qa the formatting ref to the log group is NoValue. A good enough work around.
Hi @larkinrobert , I spent some time investigating this problem. Here is what I found:
I first tested with a simple template that includes AWS::Logs:LogGroup and AWS::Serverless::Api resources as you mentioned. I was able to deploy this template successfully. I verified that the log group is created. Then I tried deleting the stack and I can verify that the log group is gone as you suggested.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
TestLogGroupName:
Type: 'AWS::Logs::LogGroup'
DeletionPolicy: Delete
Properties:
RetentionInDays: 30
LogGroupName: 'TestLogGroupName-RandomName'
MyApi:
Type: AWS::Serverless::Api
Properties:
OpenApiVersion: 3.0.1
StageName: Prod
AccessLogSetting:
DestinationArn: !GetAtt TestLogGroupName.Arn
Format: $context.requestId
Models:
User:
type: object
properties:
username:
type: string
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
InlineCode: |
exports.handler = async (event, context, callback) => {
return {
statusCode: 200,
body: 'Success'
}
}
Events:
None:
Type: Api
Properties:
RequestModel:
Model: User
Required: true
RestApiId:
Ref: MyApi
Method: get
Path: /none
The second thing I tried is to remove the reference to the log group in AWS::Serverless::Api specifically the AccessLogSettings. Everything else in AWS::Serverless::Api template stays the same except for AccessLogSettings:
MyApi:
Type: AWS::Serverless::Api
Properties:
OpenApiVersion: 3.0.1
StageName: Prod
Models:
User:
type: object
properties:
username:
type: string
I was also able to deploy the stack successfully and verified that the log group is created. However, unlike what you suggested, If not, the stack deletes without error, and the log group is left behind as an orphan, when I delete the stack, the log group is also deleted for me.
As for your comment about If I remove the log group def and leave the reference to the log group in the apigw, the log group is not eventually created like with a lambda. The deployment fails., this is actually expected behaviour. Apigateway doesn't create log group for you like lambda function.
I am closing this ticket as this is not an issue from my investigation. Feel free to reopen this ticket if you have any follow-up questions or if I misunderstand the problem.