DEV Community

JImmyWong for AWS Community Builders

Posted on • Originally published at Medium on

Terraform CDK(CDKTF) create aws s3 bucket

Terraform CDK(CDKTF) create aws s3 bucket

since too many company need to know Terraform this tools, so i want to try Terraform CDK, then i record this experience

Before link, please read before the link to learn some idea

How to use aws CDK create s3?

Here is CDK for terraform installation doc

Install CDK for Terraform and run a quick start demo | Terraform | HashiCorp Developer

i used homebrew to install Terraform cli and cdktf, because cdktf need to call terraform, so i used same method to install two tools

Gen the new Project use like awscdk command

cdktf init --template="typescript" --providers="aws@~>4.0"
Enter fullscreen mode Exit fullscreen mode

Go to the main.ts(i used typescript) and rewrite the code

import {Construct} from "constructs";
import {App, TerraformOutput, TerraformStack} from "cdktf";
import {AwsProvider} from "@cdktf/provider-aws/lib/provider";
import {S3Bucket} from "@cdktf/provider-aws/lib/s3-bucket";

class MyStack extends TerraformStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        new AwsProvider(this, "AWS", {
            region: "us-west-1",
            accessKey: "", // Please add your aws accessKey
            secretKey: "" // Please add your aws secretKey
        });

        const S3_bucket = new S3Bucket(this, 'my-bucket', {
            bucket: 'happy170066982',
            serverSideEncryptionConfiguration: {
                rule: {
                    applyServerSideEncryptionByDefault: {
                        sseAlgorithm: 'AES256',
                    },
                },
            }
        });
        new TerraformOutput(this, 's3-bucket-arn', {
            value: S3_bucket.arn,
        });

    }
}

const app = new App();
new MyStack(app, "learn-cdktf");
app.synth();
Enter fullscreen mode Exit fullscreen mode

After please deploy

cdktf deploy --auto-approve
Enter fullscreen mode Exit fullscreen mode


here is the progress and will be change item


then, it successfully deployed to aws

Let us check in the cloud


it works

let’s go directory and read ./cdktf.out/stacks/learn-cdktf/cdk.tf.json

here is my cdktf.json

{
  "//": {
    "metadata": {
      "backend": "local",
      "stackName": "learn-cdktf",
      "version": "0.17.2"
    },
    "outputs": {
      "learn-cdktf": {
        "s3-bucket-arn": "s3-bucket-arn"
      }
    }
  },
  "output": {
    "s3-bucket-arn": {
      "value": "${aws_s3_bucket.my-bucket.arn}"
    }
  },
  "provider": {
    "aws": [
      {
        "access_key": "it should be your access_key",
        "region": "us-west-1",
        "secret_key": "it should be your secret_key"
      }
    ]
  },
  "resource": {
    "aws_s3_bucket": {
      "my-bucket": {
        "//": {
          "metadata": {
            "path": "learn-cdktf/my-bucket",
            "uniqueId": "my-bucket"
          }
        },
        "bucket": "happy170066982",
        "server_side_encryption_configuration": {
          "rule": {
            "apply_server_side_encryption_by_default": {
              "sse_algorithm": "AES256"
            }
          }
        }
      }
    }
  },
  "terraform": {
    "backend": {
      "local": {
        "path": ".../learn-cdktf/terraform.learn-cdktf.tfstate"
      }
    },
    "required_providers": {
      "aws": {
        "source": "aws",
        "version": "5.9.0"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

I think all to know what is this, it is terraform code, it will help you to generate origin terraform code, so i think it can help some people to overcome terraform this tools

This tools logic more like origin terraform , not like too much AWSCDK logic, in my learning this tools, is little bit hard

Welcome all provide some comment to improve the doc

Retry later

Top comments (0)

Retry later
Retry later