├── .gitignore ├── README.md ├── cdktf.json ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | imports/* 3 | !imports/__init__.py 4 | .terraform 5 | cdktf.out 6 | cdktf.log 7 | *terraform.*.tfstate* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Documentation 2 | 3 | * Explore the Terraform for Terraform [CLI](https://www.terraform.io/downloads.html) >= v1.0+ 4 | * Explore the CDK for cdktf [CLI](https://github.com/hashicorp/terraform-cdk#build) 5 | 6 | Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values. 7 | 8 | ```shell 9 | export AWS_ACCESS_KEY_ID=AAAAAA 10 | export AWS_SECRET_ACCESS_KEY=AAAAA 11 | ``` 12 | 13 | ## cdktf-python-aws-s3bucket 14 | 15 | AWS s3 bucket configuration with python and cdktf 16 | 17 | ### Usage 18 | 19 | Install project dependencies 20 | 21 | ```shell 22 | cdktf get 23 | ``` 24 | 25 | Generate CDK for Terraform constructs for Terraform provides and modules used in the project. 26 | 27 | ```bash 28 | cdktf synth 29 | ``` 30 | -------------------------------------------------------------------------------- /cdktf.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "python", 3 | "app": "python main.py", 4 | "projectId": "403756a5-886f-43ef-b916-60c7079688c6", 5 | "terraformProviders": [ 6 | "aws@~> 3.0" 7 | ], 8 | "terraformModules": [], 9 | "codeMakerOutput": "imports", 10 | "context": { 11 | "excludeStackIdFromLogicalIds": "true", 12 | "allowSepCharsInLogicalIds": "true" 13 | } 14 | } -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from constructs import Construct 3 | from cdktf import App, NamedRemoteWorkspace, TerraformStack, TerraformOutput, RemoteBackend 4 | from cdktf_cdktf_provider_aws import AwsProvider 5 | from cdktf import Fn 6 | from imports.aws import ( 7 | s3 8 | ) 9 | 10 | 11 | class MyStack(TerraformStack): 12 | def __init__(self, scope: Construct, ns: str): 13 | super().__init__(scope, ns) 14 | 15 | AwsProvider(self, "AWS", region="us-east-1") 16 | 17 | BUCKET_NAME = 'cdktf-python-demo-us-east-1' 18 | 19 | s3_lifecycle_rule = [ 20 | { "enabled": True, "id": "abort-multipart", "prefix": "/", "abortIncompleteMultipartUploadDays": 7 }, 21 | { "enabled": True, "transition": [{ "days": 30, "storageClass": "STANDARD_IA" }] }, 22 | { "enabled": True, "noncurrentVersionTransition": [{ "days": 30, "storageClass": "STANDARD_IA" }] }, 23 | { "enabled": False, "transition": [{ "days": 90, "storageClass": "ONEZONE_IA" }] }, 24 | { "enabled": False, "noncurrentVersionTransition": [{ "days": 90, "storageClass": "ONEZONE_IA" }] }, 25 | { "enabled": False, "transition": [{ "days": 365, "storageClass": "GLACIER" }] }, 26 | { "enabled": False, "noncurrentVersionTransition": [{ "days": 365, "storageClass": "ONEZONE_IA" }] }, 27 | ] 28 | s3_tags= { 29 | "Team": "Devops", 30 | "Company": "Your compnay" 31 | } 32 | s3_policy = { 33 | "Version": "2012-10-17", 34 | "Statement": [ 35 | { 36 | "Sid": "PublicReadGetObject", 37 | "Effect": "Allow", 38 | "Principal": "*", 39 | "Action": [ 40 | "s3:GetObject" 41 | ], 42 | "Resource": [ 43 | f"arn:aws:s3:::${BUCKET_NAME}/*" 44 | ] 45 | } 46 | ] 47 | } 48 | 49 | bucket = s3.S3Bucket(self, 50 | id=f"s3-{BUCKET_NAME}", 51 | bucket=f"s3-{BUCKET_NAME}", 52 | lifecycle_rule=s3_lifecycle_rule, 53 | tags=s3_tags, 54 | policy=str(s3_policy) 55 | ) 56 | 57 | 58 | TerraformOutput(self, 'S3 id',value=bucket.id) 59 | TerraformOutput(self, 'S3 arn', value=bucket.arn) 60 | ################ 61 | 62 | 63 | app = App() 64 | stack = MyStack(app, "aws") 65 | app.synth() 66 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | cdktf~=0.9.4 --------------------------------------------------------------------------------