# LogRetention

Creates a custom resource to control the retention policy of a CloudWatch Logs log group.

The log group is created if it doesn't already exist. The policy
is removed when `retentionDays` is `undefined` or equal to `Infinity`.
Log group can be created in the region that is different from stack region by
specifying `logGroupRegion`

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › LogRetention
Implements: [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)

- Source: [`aws-logs/lib/log-retention.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-logs/lib/log-retention.ts#L77)
- Type last changed: v2.196.0 · 16 May 2025

## Example

TypeScript:

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
import { aws_iam as iam } from 'aws-cdk-lib';
import { aws_logs as logs } from 'aws-cdk-lib';

declare const role: iam.Role;
const logRetention = new logs.LogRetention(this, 'MyLogRetention', {
  logGroupName: 'logGroupName',
  retention: logs.RetentionDays.ONE_DAY,

  // the properties below are optional
  logGroupRegion: 'logGroupRegion',
  logRetentionRetryOptions: {
    base: cdk.Duration.minutes(30),
    maxRetries: 123,
  },
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  role: role,
});
```

Python:

```python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
import aws_cdk as cdk
from aws_cdk import aws_iam as iam
from aws_cdk import aws_logs as logs

# role: iam.Role

log_retention = logs.LogRetention(self, "MyLogRetention",
    log_group_name="logGroupName",
    retention=logs.RetentionDays.ONE_DAY,

    # the properties below are optional
    log_group_region="logGroupRegion",
    log_retention_retry_options=logs.LogRetentionRetryOptions(
        base=cdk.Duration.minutes(30),
        max_retries=123
    ),
    removal_policy=cdk.RemovalPolicy.DESTROY,
    role=role
)
```

Java:

```java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.iam.*;
import software.amazon.awscdk.services.logs.*;

Role role;

LogRetention logRetention = LogRetention.Builder.create(this, "MyLogRetention")
        .logGroupName("logGroupName")
        .retention(RetentionDays.ONE_DAY)

        // the properties below are optional
        .logGroupRegion("logGroupRegion")
        .logRetentionRetryOptions(LogRetentionRetryOptions.builder()
                .base(Duration.minutes(30))
                .maxRetries(123)
                .build())
        .removalPolicy(RemovalPolicy.DESTROY)
        .role(role)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK;
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.Logs;

Role role;

var logRetention = new LogRetention(this, "MyLogRetention", new LogRetentionProps {
    LogGroupName = "logGroupName",
    Retention = RetentionDays.ONE_DAY,

    // the properties below are optional
    LogGroupRegion = "logGroupRegion",
    LogRetentionRetryOptions = new LogRetentionRetryOptions {
        Base = Duration.Minutes(30),
        MaxRetries = 123
    },
    RemovalPolicy = RemovalPolicy.DESTROY,
    Role = role
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var role Role

logRetention := awscdk.Aws_logs.NewLogRetention(this, jsii.String("MyLogRetention"), &LogRetentionProps{
	LogGroupName: jsii.String("logGroupName"),
	Retention: awscdk.*Aws_logs.RetentionDays_ONE_DAY,

	// the properties below are optional
	LogGroupRegion: jsii.String("logGroupRegion"),
	LogRetentionRetryOptions: &LogRetentionRetryOptions{
		Base: cdk.Duration_Minutes(jsii.Number(30)),
		MaxRetries: jsii.Number(123),
	},
	RemovalPolicy: cdk.RemovalPolicy_DESTROY,
	Role: role,
})
```

## Initializer

TypeScript:

```ts
new LogRetention(scope: Construct, id: string, props: LogRetentionProps)
```

Python:

```python
LogRetention(scope: Construct, id: str, props: LogRetentionProps)
```

Java:

```java
new LogRetention(Construct scope, String id, LogRetentionProps props)
```

C# / .NET:

```csharp
new LogRetention(Construct scope, string id, LogRetentionProps props)
```

Golang:

```go
NewLogRetention(Scope Construct, Id *string, Props LogRetentionProps)
```

## Construct Props

- `logGroupName`: string (required) — The log group name.

- `logGroupRegion`: string — The region where the log group should be created.

  **Default:** same region as the stack

- `logRetentionRetryOptions`: [LogRetentionRetryOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogRetentionRetryOptions.html) — Retry options for all AWS API calls.

  **Default:** AWS SDK default retry options

- `removalPolicy`: [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) = RemovalPolicy.RETAIN — The removalPolicy for the log group when the stack is deleted.

- `retention`: [RetentionDays](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.RetentionDays.html) (required) — The number of days log events are kept in CloudWatch Logs.

- `role`: [IRole](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The IAM role for the Lambda function associated with the custom resource.

  **Default:** A new role is created

## Properties

- `logGroupArn`: string — The ARN of the LogGroup.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

## Methods

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
