Joseph Katzman
Joseph Katzman

Reputation: 2083

Cannot create CDK stack for AWS S3 Glacier in CDK app?

I decided to use AWS S3 Glacier for data archiving. All of IaC was written by using CDK apps (TypeScript). That's why I decided to create a new CDK app and define S3GlacierStack. As far as I understood, StorageClass is the most appropriate way to do it. As you can see, it supports TypeScript. So I decided to create a simple CDK app and try to create S3 bucket by using the code example from AWS CDK docs. Unfortunately, I faced the following error when I run cdk list command: Error: Cannot use 'noncurrent' rules on a nonversioned bucket. What did I do wrong? What is the recommended approach to use AWS S3 Glacier Flexible Retrieval storage class in CDK apps? Here is the code of my CDK app.

cdk-app-1.ts

import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { S3GlacierStack } from '../lib/s3-glacier-stack';

const app = new cdk.App();
new S3GlacierStack(app, 'S3GlacierStack', {});

s3-glacier-stack.ts

import * as cdk from 'aws-cdk-lib';
import * as s3 from "aws-cdk-lib/aws-s3";
import { Construct } from 'constructs';

export class S3GlacierStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {

    super(scope, id, props);

    const bucket = new s3.Bucket(this, 's3-glacier', {
      lifecycleRules: [{
        abortIncompleteMultipartUploadAfter: cdk.Duration.minutes(30),
        enabled: false,
        expiration: cdk.Duration.days(30),
        expirationDate: new Date(),
        expiredObjectDeleteMarker: false,
        id: 's3-glacier',
        noncurrentVersionExpiration: cdk.Duration.days(30),
        // the properties below are optional
        noncurrentVersionsToRetain: 123,
        noncurrentVersionTransitions: [{
          storageClass: s3.StorageClass.GLACIER,
          transitionAfter: cdk.Duration.days(30),
          // the properties below are optional
          noncurrentVersionsToRetain: 123,
        }],
        objectSizeGreaterThan: 500,
        prefix: 'prefix',
        objectSizeLessThan: 10000,
        transitions: [{
          storageClass: s3.StorageClass.GLACIER,
          // the properties below are optional
          transitionAfter: cdk.Duration.days(30),
          transitionDate: new Date(),
        }],
      }]
    });
  }
}

package.json

{
  "name": "cdk-app-1",
  "version": "0.1.0",
  "bin": {
    "cdk-app-1": "bin/cdk-app-1.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@types/jest": "^27.5.2",
    "@types/node": "10.17.27",
    "@types/prettier": "2.6.0",
    "jest": "^27.5.1",
    "ts-jest": "^27.1.4",
    "aws-cdk": "2.50.0",
    "ts-node": "^10.9.1",
    "typescript": "~3.9.7"
  },
  "dependencies": {
    "aws-cdk-lib": "2.50.0",
    "constructs": "^10.0.0",
    "source-map-support": "^0.5.21"
  }
}

This is the error I see in my terminal when I run cdk list command. enter image description here

Upvotes: 1

Views: 397

Answers (1)

fedonev
fedonev

Reputation: 25819

Enable object versioning on your Bucket with versioned: true.

The CDK error message is telling you that your noncurrent rule (what to do with previous object versions) makes no sense if bucket versioning isn't enabled.

Upvotes: 2

Related Questions