Skip to content
v2.273.0

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

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,
});
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
// 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();
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
});
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 ​

ts
new LogRetention(scope: Construct, id: string, props: LogRetentionProps)
python
LogRetention(scope: Construct, id: str, props: LogRetentionProps)
java
new LogRetention(Construct scope, String id, LogRetentionProps props)
csharp
new LogRetention(Construct scope, string id, LogRetentionProps props)
go
NewLogRetention(Scope Construct, Id *string, Props LogRetentionProps)

Construct Props ​

logGroupNamestringlog_group_namestrlogGroupNameStringLogGroupNamestringLogGroupName*stringrequired#
The log group name.
logGroupRegionstringlog_group_regionstrlogGroupRegionStringLogGroupRegionstringLogGroupRegion*string= same region as the stack#
The region where the log group should be created.
logRetentionRetryOptionsLogRetentionRetryOptionslog_retention_retry_optionsLogRetentionRetryOptionslogRetentionRetryOptionsLogRetentionRetryOptionsLogRetentionRetryOptionsLogRetentionRetryOptionsLogRetentionRetryOptionsLogRetentionRetryOptions= AWS SDK default retry options#
Retry options for all AWS API calls.
removalPolicyRemovalPolicyremoval_policyRemovalPolicyremovalPolicyRemovalPolicyRemovalPolicyRemovalPolicyRemovalPolicyRemovalPolicy= RemovalPolicy.RETAIN#
The removalPolicy for the log group when the stack is deleted.
retentionRetentionDaysretentionRetentionDaysretentionRetentionDaysRetentionRetentionDaysRetentionRetentionDaysrequired#
The number of days log events are kept in CloudWatch Logs.
roleIRoleroleIRoleroleIRoleRoleIRoleRoleIRole= A new role is created#
The IAM role for the Lambda function associated with the custom resource.

Properties ​

logGroupArnstringlog_group_arnstrlogGroupArnStringLogGroupArnstringLogGroupArn*stringrequired#
The ARN of the LogGroup.
nodeNodenodeNodenodeNodeNodeNodeNodeNoderequired#
The tree node.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringrequiredstatic#
Uniquely identifies this class.

Methods ​

isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

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

  • xxxxX — Any object.

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

toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func 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.

1 parameter · returns

Parameters

  • mixinsmixinsmixinsmixinsMixins — The mixins to apply.

Returns: This construct for chaining