Just Learn Code

Optimizing AWS Resource Management with CDK: Deleting S3 Buckets and Managing Orphaned Resources

Managing AWS resources can be a daunting task, especially when it comes to managing cloud infrastructure with the help of Amazon Web Services’ AWS Cloud Development Kit (CDK). In this article, we will explore two topics related to managing AWS resources with CDK: deleting S3 buckets on CDK destroy and orphaned resources on stack deletion.

Deleting S3 Buckets on CDK Destroy

Amazon S3 is a widely-used storage service that allows users to store and retrieve any amount of data. If not properly managed, S3 buckets can accumulate large amounts of data and become costly for your organization.

Additionally, when using CDK to orchestrate infrastructure, deleting S3 buckets can sometimes become a tedious task. By default, when deleting an AWS CDK stack, any resources created will be left running.

This is to prevent accidental deletion of data in case of a mistake. However, it can become problematic if left unchecked.

It is important to make sure all resources are deleted when no longer needed to avoid unnecessary expenses. One option is to set the RemovalPolicy to DESTROY.

This deletes the S3 bucket associated with the stack when the stack is deleted. To do this, add the RemovalPolicy to the S3 bucket’s property:

“`

const myBucket = new s3.Bucket(this, “MyBucket”, {

removalPolicy: cdk.RemovalPolicy.DESTROY,

})

“`

Another option is to use a Lambda function with the autoDeleteObjects property.

This can be used to empty the S3 bucket before destroying the stack. Here’s an example:

“`

const emptyBucketFunction = new lambda.Function(this, “EmptyBucketFunction”, {

runtime: lambda.Runtime.NODEJS_14_X,

handler: “index.handler”,

code: lambda.Code.fromAsset(“path/to/lambda/code”),

})

const myBucket = new s3.Bucket(this, “MyBucket”, {

autoDeleteObjects: true,

})

myBucket.onCloudFormationDelete(“EmptyBucketTrigger”, {

target: new targets.LambdaFunction(emptyBucketFunction)

})

“`

Orphaned Resources on Stack Deletion

When deleting a CDK stack, by default, the resources created will be retained by AWS. This can be problematic when managing resources as it can cause unnecessary expenses to the organization.

It is important to make sure that all resources are deleted when they’re no longer needed. However, AWS CloudFormation does not delete resources created outside of the CloudFormation stack, i.e., orphaned resources.

These orphaned resources can make it difficult to track which resources belong to which stack. To avoid this, it’s important to retain orphaned resources only if absolutely necessary.

By default, all resources created by the stack are retained when the stack is deleted. This can be changed to DELETE or RETAIN by setting the removalPolicy property.

For example:

“`

const myBucket = new s3.Bucket(this, “MyBucket”, {

removalPolicy: cdk.RemovalPolicy.RETAIN,

})

“`

However, you may want to opt-out of the default behavior and delete or retain specific resources by using the CDK opt-out flag. You can do this by setting the retainDeployments property to false when deploying the stack:

“`

cdk deploy –retain-deployments=false

“`

In summary, managing AWS resources can be challenging when it comes to managing cloud infrastructure.

With the help of CDK, deleting S3 buckets and managing orphaned resources can become more manageable. By following the proper procedures, you can save your organization time and money.

In summary, managing AWS resources with CDK requires careful consideration of deleting S3 buckets on CDK destroy and orphaned resources on stack deletion. To avoid unnecessary expenses, S3 buckets can be deleted automatically or emptied using Lambda functions.

Orphaned resources can be managed by opting out of the default behavior to retain or delete resources. It is essential to manage AWS resources correctly to avoid costly mistakes and unnecessary expenses.

Remembering the best practices outlined in this article will help organizations work efficiently and achieve their goals without complications.

Popular Posts