aws / aws/aws-cdk

(aws-logs): allow prefixing auto-generated log group names

Open
#19,353 9 comments 48 reactions 0 assignees View on GitHub
@aws-cdk/aws-logs effort/medium feature-request p1
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Description

It would be nice to retain the flexibility of the auto-generated log group names provided by CDK & CloudFormation while also being able to apply a prefix to work around certain restrictions, as described [here](https://docs.aws.amazon.com/step-functions/latest/dg/bp-cwl.html).

### Use Case

I've got a client who uses AWS Step Functions heavily. They ran into the resource policy size issue described [here](https://docs.aws.amazon.com/step-functions/latest/dg/bp-cwl.html), so they needed to prefix their log groups with `/aws/vendedlogs/` to work around the problem.

So, they're now providing an explicit log group name, like this:

```ts
new logs.LogGroup(this, 'LogGroup', {
logGroupName: '/aws/vendedlogs/my-log-group-name'
});
```

However, like with most other constructs, it would be preferable to have the entropy of auto-generated names, instead of hard-coding a static name.

For example, this simple stack produces a log group with the name `CdkTestingStack-LogGroupF5B46931-PEAj1XoIi1Q5`:

```ts
export class CdkTestingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

new logs.LogGroup(this, 'LogGroup')
}
}
```

What we'd like to accomplish is easily producing a log group with the name `/aws/vendedlogs/CdkTestingStack-LogGroupF5B46931-PEAj1XoIi1Q5`. This way, we work around the resource policy limitation while retaining the auto-generated name.

### Proposed Solution

An easy backward-compatible way to introduce this behavior would be to add an optional `logGroupNamePrefix` parameter to the L2 `LogGroup` construct.

For example:

```ts
export class CdkTestingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

new logs.LogGroup(this, 'LogGroup', {
logGroupNamePrefix: '/aws/vendedlogs/'
})
}
}
```

would provide a log group with the name `/aws/vendedlogs/CdkTestingStack-LogGroupF5B46931-PEAj1XoIi1Q5`.

It could also work with statically provided names, such as:

```ts
export class CdkTestingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

new logs.LogGroup(this, 'LogGroup', {
logGroupName: 'MyLogGroup',
logGroupNamePrefix: '/aws/vendedlogs/'
})
}
}
```

which would provide a log group with the name `/aws/vendedlogs/MyLogGroup`.

### Other information

#### Possible Workarounds

##### L1 `LogGroup` Construct Modification

I considered reaching into the `CfnLogGroup` and using an `Fn::Join` to get this behavior without a CDK change. However, I think I'd also need to adjust the `arn` property, since it's set in the constructor as well: https://github.com/aws/aws-cdk/blob/d91b2e2259f9c1615aba2cb76ad4ab1fba945836/packages/@aws-cdk/aws-logs/lib/log-group.ts#L392-L420

That seemed a bit too fragile to be worthwhile.

##### Using the Physical Name Generator

I thought about using the `generatePhysicalName` method to recreate the autogenerated name. However, this seemed unsafe to do, since the function lives in `packages/@aws-cdk/core/lib/private/physical-name-generator.ts`. The `private` specifier makes me think twice about using it.

#### L2 StateMachine Construct

Since creating a `StateMachine` through the console automatically prefixes log groups with `/aws/vendedlogs`, it could be nice to also add this behavior to the L2 construct once the ability to provide a `prefix` is added.

### Acknowledge

- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

Contributor guide

Open the contributing guide

Research direction

Start in packages/@aws-cdk/aws-logs/lib/log-group.ts, especially the L2 LogGroup implementation and the CfnLogGroup/ARN handling around the referenced lines. Trace how generated and explicitly supplied names are currently produced, then verify that the requested prefix behavior preserves both cases and produces the expected log-group name and ARN.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.