# 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 › LogRetention
Implements: IConstruct, IDependable

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

## Example

```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,
});
```

## Initializer

```ts
new LogRetention(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](/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](/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) = RemovalPolicy.RETAIN — The removalPolicy for the log group when the stack is deleted.

- `retention`: [RetentionDays](/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](/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](/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
