CDK for Terraform AWS Provider

21.22.1 · active · verified Fri Apr 17

The `cdktf-cdktf-provider-aws` library provides a Pythonic interface to the AWS Terraform provider within the Cloud Development Kit for Terraform (CDKTF) ecosystem. It allows users to define AWS infrastructure using familiar Python constructs, which CDKTF then synthesizes into Terraform HCL for deployment. This library, currently at version 21.22.1, is updated frequently to reflect new versions of the underlying AWS Terraform provider and the `cdktf` core library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart defines a `TerraformStack` that provisions a simple AWS S3 bucket using the `cdktf-cdktf-provider-aws` library. It configures the AWS provider and creates an S3 bucket with a unique prefix and tags. The bucket's domain name is then exported as a Terraform output. Ensure `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY` environment variables are set for deployment, or configure them via other AWS credential methods. After running `python main.py` (assuming the code is in `main.py`), use `cdktf deploy my-cdktf-aws-stack` to provision the resources.

from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
from cdktf_cdktf_provider_aws.provider import AwsProvider
from cdktf_cdktf_provider_aws.s3_bucket import S3Bucket
import os

class MyAwsStack(TerraformStack):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        # Configure AWS Provider
        AwsProvider(self, "AWS", 
                    region=os.environ.get('AWS_REGION', 'us-east-1'),
                    access_key=os.environ.get('AWS_ACCESS_KEY_ID', ''),
                    secret_key=os.environ.get('AWS_SECRET_ACCESS_KEY', ''))

        # Create an S3 Bucket
        bucket = S3Bucket(self, "my-cdktf-unique-bucket",
                          bucket_prefix="my-cdktf-prefix-",
                          acl="private",
                          tags={
                              "Environment": "Development",
                              "Project": "CDKTF-Demo"
                          })

        # Output the bucket name
        TerraformOutput(self, "bucket_name",
                        value=bucket.bucket_domain_name,
                        description="The domain name of the created S3 bucket")

app = App()
MyAwsStack(app, "my-cdktf-aws-stack")
app.synth()

# To deploy, run: cdktf deploy my-cdktf-aws-stack

view raw JSON →