Forem

Cover image for Workaround to Create an S3 glacier with CDK + TS
Marcos Henrique for AWS Community Builders

Posted on

4 1 1

Workaround to Create an S3 glacier with CDK + TS

I was working on an archive project, hen a company had been deleted, and we need to move all the objects stored in S3 to a Glacier Archive..

As the world isn't a fairy tale, the S3 Glacier is not supported in neither the CDK nor CloudFormation and doing this manually would not be a good solution in my context, so if you want to do the same as me you will face two possible solutions:

  • Custom Resource construct. The Custom Resource's job is to call the SDK APIs to create/update/delete the vault. It will be invoked during the deployment lifecycle by CloudFormation. There are several flavours of Custom Resource. You may be able to use the custom_resources.AwsCustomResource construct, which makes SDK calls easy. If more control is required, you will need to write your own Lambda in conjunction with a cdk.CustomResource.
  • A workaround using the transitionDate with the new Date() method on the lifecycle rule of the bucket

In my case I chose the second solution, so let's figure out how to do this in the code with CDK and Typescript.

Alright, let's roll up our sleeves and get to work on this code

When I was working on this solution I'd identify some issues:

  • Found mixed 'Date' and 'Days' based Transition actions in lifecycle rule, this error occurs because you are mixing transitionDate and transitionAfter in your lifecycle rule.
  • 'Date' must be at midnight GMT, this error occurs when you are just instantiating a new Date() without setting the hours as midnight

That being said, let's create a function to encapsulate this necessity:

  private mountTodayAtMidnight() {
    const today = new Date()
    today.setUTCHours(0, 0, 0, 0)

    return today
  }
Enter fullscreen mode Exit fullscreen mode

Now we can create our bucket with the transitionDate setting to today at midnight

import { Duration, RemovalPolicy } from 'aws-cdk-lib'
import { Cors } from 'aws-cdk-lib/aws-apigateway'
import { Bucket, StorageClass, HttpMethods } from 'aws-cdk-lib/aws-s3'
import { Construct } from 'constructs'

export default class ArchiveAssetsBucket extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id)

    new Bucket(this, 'archive-assets-bkt', {
      bucketName: 'archive-assets',
      removalPolicy: RemovalPolicy.DESTROY,
      cors: [
        {
          allowedOrigins: Cors.ALL_ORIGINS,
          allowedMethods: [HttpMethods.GET, HttpMethods.PUT, HttpMethods.DELETE],
          allowedHeaders: ['*'],
        },
      ],
      versioned: true,
      lifecycleRules: [
        {
          enabled: true,
          abortIncompleteMultipartUploadAfter: Duration.days(1),
          noncurrentVersionExpiration: Duration.days(1),
          noncurrentVersionsToRetain: 1,
          transitions: [
            {
              storageClass: StorageClass.GLACIER,
              transitionDate: this.mountTodayAtMidnight(), //the magic happens here
            },
          ],
        },
      ]
    })
  }
}
Enter fullscreen mode Exit fullscreen mode

I believe this isn't the best solution, but fits to me xD

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay