# AWS CDK, CDKTF, and Projen: Tools for Cloud Infrastructure Automation ## 1. AWS Cloud Development Kit (AWS CDK) [AWS Cloud Development Kit (CDK)](https://aws.amazon.com/cdk/) is an open-source framework that allows developers to define cloud infrastructure using programming languages such as TypeScript, Python, and Java. CDK generates AWS CloudFormation templates, enabling users to write infrastructure as code (IaC) in a more familiar and flexible programming environment. ### In a Non-AWS Cloud Context: For clouds that support an S3-compatible API and AWS Terraform providers but do not use AWS CloudFormation, CDK is not directly applicable. However, the [Cloud Development Kit for Terraform (CDKTF)](https://developer.hashicorp.com/terraform/cdktf) provides a similar approach to defining infrastructure using higher-level programming languages while using Terraform as the underlying provisioning engine. ## 2. Cloud Development Kit for Terraform (CDKTF) [Cloud Development Kit for Terraform (CDKTF)](https://developer.hashicorp.com/terraform/cdktf) extends the CDK model to Terraform. CDKTF allows developers to define cloud infrastructure in languages like TypeScript, Python, Java, and Go, while using Terraform to provision the resources. CDKTF is cloud-agnostic and supports any cloud provider with a Terraform provider, which includes those that offer AWS-compatible services like S3. ### In a Non-AWS Cloud Context: For non-AWS cloud environments that support AWS-compatible Terraform providers, CDKTF can be used to manage resources. CDKTF provides flexibility in defining infrastructure using programming languages, while Terraform handles resource provisioning through the respective provider. ## 3. Projen [Projen](https://github.com/projen/projen) is a tool designed to automate the setup and maintenance of project configurations, particularly for CDK, CDKTF, and TypeScript projects. Projen generates and maintains files such as configuration files, workflows, and scripts, ensuring consistency and reducing manual effort. ### In a Non-AWS Cloud Context: Projen can be used regardless of the cloud provider, as its primary focus is on automating project setup rather than managing cloud resources. It is particularly useful for CDKTF projects, helping to automate the creation and maintenance of configuration files and keeping projects organized. ## Summary: In environments with S3-compatible APIs and support for AWS Terraform providers, CDKTF and Projen are useful tools for managing cloud infrastructure and project configurations. CDKTF allows developers to define infrastructure using familiar programming languages and relies on Terraform for resource management, while Projen helps automate project configuration. The AWS CDK itself is not directly applicable in non-AWS environments that do not support CloudFormation, but CDKTF serves as a suitable alternative. --- ## Sample Code: Deploying an S3-Compatible Bucket with Two Objects Using CDKTF Here’s a sample TypeScript program using CDKTF to deploy an S3-compatible bucket with two objects (files) copied from the local filesystem, using the `projen` AWS profile: ```typescript import { App, TerraformStack } from 'cdktf'; import { AwsProvider } from '@cdktf/provider-aws'; import { S3Bucket, S3BucketObject } from '@cdktf/provider-aws/lib/s3'; import * as fs from 'fs'; import * as path from 'path'; class MyStack extends TerraformStack { constructor(scope: App, id: string) { super(scope, id); // Initialize AWS provider using the "projen" profile new AwsProvider(this, 'AWS', { profile: 'projen', skipCredentialsValidation: true, skipRegionValidation: true, skipRequestingAccountId: true, }); // Define the S3-compatible bucket const bucket = new S3Bucket(this, 'MyBucket', { bucketPrefix: 'tutorial-bucket', }); // Add two objects (files) to the bucket from the filesystem const filesToUpload = { file1: path.resolve(__dirname, 'file1.txt'), file2: path.resolve(__dirname, 'file2.txt'), }; // Ensure the files exist on the local filesystem for (const [fileKey, filePath] of Object.entries(filesToUpload)) { if (!fs.existsSync(filePath)) { throw new Error(`File not found: ${filePath}. Make sure it exists.`); } new S3BucketObject(this, `BucketObject-${fileKey}`, { bucket: bucket.bucket, key: path.basename(filePath), // Use file name as the object key source: filePath, // Path to the file on the local system }); } } } const app = new App(); new MyStack(app, 'my-stack'); app.synth(); ``` ### Equivalent Terraform Configuration: For comparison, here is the equivalent configuration in Terraform HCL: ```hcl provider "aws" { profile = "projen" skip_credentials_validation = true skip_region_validation = true skip_requesting_account_id = true } resource "aws_s3_bucket" "my_bucket" { bucket_prefix = "tutorial-bucket" } resource "aws_s3_object" "file1" { bucket = aws_s3_bucket.my_bucket.bucket key = "file1.txt" source = "./file1.txt" } resource "aws_s3_object" "file2" { bucket = aws_s3_bucket.my_bucket.bucket key = "file2.txt" source = "./file2.txt" } ``` ### Build and Deploy Options You can build and deploy the project using either the `cdktf` CLI or Projen. If you're using only `cdktf`, run: ``` cdktf deploy ``` If you are using Projen to manage the project, you can build and deploy it using the following commands: ``` npx projen build npx projen deploy ``` Both approaches will synthesize and deploy the infrastructure using Terraform in the background.