Skip to content

ahmadalibagheri/cdktf-typescript-aws-s3bucket

Repository files navigation

Documentation

  • Explore the Terraform for Terraform CLI >= v1.0+
  • Explore the Nodejs for npm CLI >= v14+
  • Explore the Yarn for Yarn CLI >= v1.21 (optional - NPM will work as an alternative)
  • Explore the CDK for cdktf CLI

Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.

$ export AWS_ACCESS_KEY_ID=AAAAAA $ export AWS_SECRET_ACCESS_KEY=AAAAA

typescript-aws-s3bucket

A CDK for Terraform application in TypeScript for VPC configuraiton.

Usage

Install project dependencies

yarn install

Generate CDK for Terraform constructs for Terraform provides and modules used in the project.

cdktf get

You can now edit the main.ts file if you want to modify any code.

vim main.ts import { Construct } from 'constructs'; import { App, TerraformOutput, TerraformStack } from 'cdktf'; import { AwsProvider, s3 } from './.gen/providers/aws'; class MyStack extends TerraformStack { constructor(scope: Construct, name: string) { super(scope, name); new AwsProvider(this, 'aws', { region: 'us-east-1', }); const BUCKET_NAME = 'cdktf-typescript-demo-us-east-1'; const bucket = new s3.S3Bucket(this, 'aws_s3_bucket', { bucket: BUCKET_NAME, lifecycleRule: [ { enabled: true, id: 'abort-multipart', prefix: '/', abortIncompleteMultipartUploadDays: 7 }, { enabled: true, transition: [{ days: 30, storageClass: 'STANDARD_IA' }] }, { enabled: true, noncurrentVersionTransition: [{ days: 30, storageClass: 'STANDARD_IA' }] }, { enabled: false, transition: [{ days: 90, storageClass: 'ONEZONE_IA' }] }, { enabled: false, noncurrentVersionTransition: [{ days: 90, storageClass: 'ONEZONE_IA' }] }, { enabled: false, transition: [{ days: 365, storageClass: 'GLACIER' }] }, { enabled: false, noncurrentVersionTransition: [{ days: 365, storageClass: 'ONEZONE_IA' }] }, ], tags: { Team: 'Devops', Company: 'Your compnay', }, policy: `{  "Version": "2012-10-17",  "Statement": [  {  "Sid": "PublicReadGetObject",  "Effect": "Allow",  "Principal": "*",  "Action": [  "s3:GetObject"  ],  "Resource": [  "arn:aws:s3:::${BUCKET_NAME}/*"  ]  }  ]  }`, }); new TerraformOutput(this, 'S3 id', { value: bucket.id, }); new TerraformOutput(this, 'S3 arn', { value: bucket.arn, }); } } const app = new App(); new MyStack(app, 'aws-s3'); app.synth();

Compile the TypeScript application

tsc

At this step you can run code with two different way:

The first way:

Generate Terraform configuration

cdktf synth

The above command will create a folder called cdktf.out that contains all Terraform JSON configuration that was generated.

Run Terraform commands

cd cdktf.out terraform init terraform plan terraform apply

The second way:

Run cdktf commands

cdktf deploy

About

AWS S3bucket configuration with typescript and cdktf

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published