(logs): LogRetention with removalPolicy DESTROY grants logs:DeleteLogGroup on the stack region instead of logGroupRegion
Describe the bug
When logs.LogRetention is created with both logGroupRegion (a region different from the stack's region) and removalPolicy: RemovalPolicy.DESTROY, the logs:DeleteLogGroup permission added to the custom resource handler's role is scoped to the stack's region, not to logGroupRegion.
At runtime the handler creates its CloudWatch Logs client with LogGroupRegion, so the DeleteLogGroup API call goes to the correct region — but the IAM policy only allows deleting a log group ARN in the stack region. As a result, cdk destroy fails with AccessDenied and the stack ends up in DELETE_FAILED.
The cause is in packages/aws-cdk-lib/aws-logs/lib/log-retention.ts:
// constructor
if (props.removalPolicy === cdk.RemovalPolicy.DESTROY) {
provider.grantDeleteLogGroup(props.logGroupName); // logGroupRegion is not passed
}
// LogRetentionFunction
public grantDeleteLogGroup(logGroupName: string) {
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['logs:DeleteLogGroup'],
resources: [cdk.Stack.of(this).formatArn({
// no `region` here -> defaults to the stack region
service: 'logs',
resource: 'log-group',
resourceName: `${logGroupName}:*`,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
})],
}));
}The same construct already handles the region correctly for the public logGroupArn property:
this.logGroupArn = cdk.Stack.of(this).formatArn({
region: props.logGroupRegion,
service: 'logs',
...
});A concrete case where this matters is AWS Chatbot: its log groups are always created in us-east-1 regardless of the stack region, so users in other regions must set logGroupRegion: 'us-east-1'.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Library Version
No response
Expected Behavior
The logs:DeleteLogGroup statement should be scoped to the log group ARN in logGroupRegion (when specified), so that cdk destroy succeeds and the log group in that region is deleted.
Current Behavior
cdk synth produces a policy statement for the stack region even though logGroupRegion is set:
- Action: logs:DeleteLogGroup
Effect: Allow
Resource:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:ap-northeast-1:" # stack region, not us-east-1
- Ref: AWS::AccountId
- :log-group:/aws/chatbot/verify-chatbot-logs:*cdk destroy then fails:
Received response status [FAILED] from custom resource. Message returned:
User: arn:aws:sts::<account>:assumed-role/VerifyChatbotLogs-LogRetention... is not authorized
to perform: logs:DeleteLogGroup on resource:
arn:aws:logs:us-east-1:<account>:log-group:/aws/chatbot/verify-chatbot-logs
because no identity-based policy allows the logs:DeleteLogGroup actionThe stack is left in DELETE_FAILED.
Reproduction Steps
Deploy the following stack to ap-northeast-1 (any region other than us-east-1 reproduces it). The log group does not need to exist beforehand; the handler creates it in us-east-1 on deploy.
import * as cdk from 'aws-cdk-lib';
import * as logs from 'aws-cdk-lib/aws-logs';
import { Construct } from 'constructs';
export class LogRetentionRegionStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new logs.LogRetention(this, 'LogRetention', {
logGroupName: '/aws/chatbot/verify-chatbot-logs',
logGroupRegion: 'us-east-1',
retention: logs.RetentionDays.TWO_WEEKS,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
}
}
const app = new cdk.App();
new LogRetentionRegionStack(app, 'LogRetentionRegionStack', {
env: { region: 'ap-northeast-1' },
});cdk deploy LogRetentionRegionStack # succeeds
cdk destroy LogRetentionRegionStack # fails with AccessDenied on logs:DeleteLogGroupInspecting the synthesized template shows the logs:DeleteLogGroup resource ARN uses ap-northeast-1.
Possible Solution
Pass props.logGroupRegion through to grantDeleteLogGroup and forward it to formatArn:
// constructor
if (props.removalPolicy === cdk.RemovalPolicy.DESTROY) {
provider.grantDeleteLogGroup(props.logGroupName, props.logGroupRegion);
}
// LogRetentionFunction
public grantDeleteLogGroup(logGroupName: string, region?: string) {
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['logs:DeleteLogGroup'],
resources: [cdk.Stack.of(this).formatArn({
region,
service: 'logs',
resource: 'log-group',
resourceName: `${logGroupName}:*`,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
})],
}));
}grantDeleteLogGroup is marked @internal, and when region is undefined formatArn falls back to the stack region as it does today, so this is backwards compatible.
I'm happy to open a PR for this.
Additional Information/Context
Workaround: pass a custom role to LogRetention that already allows logs:DeleteLogGroup on the us-east-1 log group ARN. IAM permissions are additive, so the extra (wrong-region) statement added by CDK is harmless.
Note that chatbot.SlackChannelConfiguration's logRetention prop hard-codes logGroupRegion: 'us-east-1' but exposes no removalPolicy, so it is not affected. Only direct users of logs.LogRetention hit this.
AWS CDK Library version (aws-cdk-lib)
2.269.0
AWS CDK CLI version
2.1141.0
Node.js Version
v24.13.0
OS
macOS 26.6
Language
TypeScript
Language Version
No response
Other information
No response
Source: aws/aws-cdk