Claude Skills Guide

Claude Code for CDKTF Terraform CDK Workflow

The Cloud Development Kit for Terraform (CDKTF) enables you to define cloud infrastructure using familiar programming languages like TypeScript, Python, Go, and C#. By combining the expressive power of general-purpose languages with Terraform’s infrastructure provisioning capabilities, CDKTF offers a powerful approach to infrastructure-as-code. This guide shows you how to integrate Claude Code into your CDKTF workflow to accelerate development, improve code quality, and streamline infrastructure management.

Understanding CDKTF and Its Workflow

CDKTF bridges the gap between developer-friendly programming languages and Terraform’s declarative infrastructure model. Instead of writing HCL (HashiCorp Configuration Language), you can use TypeScript, Python, Go, C#, or Java to define your infrastructure. CDKTF then synthesizes your code into Terraform configuration files that Terraform can deploy.

This approach brings several advantages: strong typing, IDE autocomplete, testing frameworks, and the ability to use existing software engineering practices. However, it also introduces new challenges around testing, state management, and workflow integration that Claude Code can help address.

Setting Up Claude Code for CDKTF Projects

Before diving into practical examples, ensure Claude Code understands your CDKTF project structure. Create a CLAUDE.md file in your project root with the following context:

# CDKTF Project Context

This is a Terraform Cloud Development Kit (CDKTF) project using [TypeScript/Python/Go].

## Project Structure
- /src - CDKTF application code
- /src/stacks - Infrastructure stack definitions
- /src/constructs - Reusable infrastructure constructs
- /cdk.out - Synthesized Terraform output

## Key Patterns
- All stacks extend the base Stack class
- Use constructs for reusable components
- Store Terraform outputs in cdk.out
- Run synth before deploy: cdk synth
- Deploy with: cdk deploy

## Coding Standards
- Use TypeScript strict mode
- Import constructs from @cdktf/provider-aws (or other providers)
- Always define sensible defaults for optional props
- Include tags on all resources

This context helps Claude understand your project structure and apply appropriate patterns when generating code.

Generating CDKTF Infrastructure with Claude

One of the most powerful use cases for Claude Code in CDKTF projects is infrastructure code generation. Instead of manually writing every resource definition, you can describe your desired infrastructure in natural language and let Claude generate the TypeScript, Python, or Go code.

For example, suppose you need to create an AWS Lambda function with API Gateway integration. You can ask Claude:

Create a CDKTF TypeScript construct for an HTTP API Lambda function. 
Include:
- API Gateway HTTP API with CORS enabled
- Lambda function with Node.js runtime
- Environment variables for configuration
- Proper IAM role with minimal permissions
- CloudWatch Logs for monitoring

Claude will generate the complete construct with all the necessary imports, props interfaces, and resource definitions:

import { Construct } from 'constructs';
import { 
  HttpApi, 
  HttpMethod, 
  LambdaProxyIntegration 
} from '@cdktf/provider-aws/lib/api-gateway-v2';
import { 
  Function, 
  FunctionRuntime, 
  FunctionHandler 
} from '@cdk.tf/lambda/function';
import { 
  Role, 
  RolePolicy, 
  IamRoleServicePrincipal 
} from '@cdktf/provider-aws/iam';
import { 
  LogGroup, 
  LogRetention 
} from '@cdktf/provider-aws/cloudwatch';

export interface LambdaApiProps {
  functionName: string;
  handler: string;
  runtime: FunctionRuntime;
  environment?: Record<string, string>;
  memorySize?: number;
  timeout?: number;
}

export class LambdaApi extends Construct {
  public readonly api: HttpApi;
  public readonly lambda: Function;

  constructor(scope: Construct, id: string, props: LambdaApiProps) {
    super(scope, id);

    // Create IAM role for Lambda
    const role = new Role(this, 'lambda-role', {
      name: `${props.functionName}-role`,
      assumeRolePolicy: JSON.stringify({
        Version: '2012-10-17',
        Statement: [{
          Action: 'sts:AssumeRole',
          Effect: 'Allow',
          Principal: { Service: 'lambda.amazonaws.com' }
        }]
      })
    });

    // Create Lambda function
    this.lambda = new Function(this, 'lambda-function', {
      functionName: props.functionName,
      handler: props.handler,
      runtime: props.runtime,
      environment: {
        variables: props.environment || {}
      },
      memorySize: props.memorySize || 256,
      timeout: props.timeout || 30,
      role: role.arn
    });

    // Create API Gateway HTTP API
    this.api = new HttpApi(this, 'http-api', {
      name: props.functionName,
      corsConfig: {
        allowOrigins: ['*'],
        allowMethods: [HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS],
        allowHeaders: ['Content-Type', 'Authorization']
      }
    });

    // Integrate API with Lambda
    new LambdaProxyIntegration(this, 'lambda-integration', {
      api: this.api,
      lambda: this.lambda
    });

    // Create CloudWatch Log Group
    new LogGroup(this, 'log-group', {
      name: `/aws/lambda/${props.functionName}`,
      retentionInDays: 7
    });
  }
}

Building Reusable Infrastructure Constructs

Claude excels at helping you build composable, reusable constructs. Rather than defining infrastructure repeatedly, you can create abstractions that encapsulate common patterns.

For instance, you might want a standard pattern for database-backed services:

Create a CDKTF TypeScript construct for a standard microservice 
with: PostgreSQL database, Redis cache, application load balancer,
and ECS Fargate service. Include proper networking and security groups.

Claude generates a comprehensive construct that you can reuse across multiple services, significantly reducing boilerplate code and ensuring consistency.

Reviewing and Refactoring CDKTF Code

Beyond code generation, Claude Code serves as an excellent code reviewer for CDKTF infrastructure. It can identify issues like missing tags, insecure configurations, or inefficient resource definitions:

Review this CDKTF stack for security issues, best practices,
and potential cost optimizations. Check for:
- Missing encryption settings
- Overly permissive IAM policies
- Missing tags or logging
- Potential drift issues

This kind of review helps catch problems before they reach production and ensures your infrastructure follows organizational standards.

Integrating CDKTF with CI/CD Pipelines

Claude can also help you set up automated workflows for CDKTF projects. You might ask:

Create a GitHub Actions workflow for CDKTF that:
- Runs synth on pull requests
- Plans infrastructure changes
- Requires approval for production deploys
- Runs integration tests
- Posts results to Slack

Claude will generate a complete workflow file with proper caching, secret management, and deployment strategies.

Best Practices for CDKTF Development with Claude

When working with Claude Code on CDKTF projects, keep these recommendations in mind:

Provide Complete Context: Include your provider versions, Terraform version, and any organizational standards in your CLAUDE.md file. This ensures Claude generates compatible code.

Iterate on Code Generation: Start with a simple construct and ask Claude to add complexity incrementally. This produces more accurate results than asking for everything at once.

Review Before Deploying: Always run cdk diff to see what changes Claude’s code will make to your infrastructure before deploying.

Test Infrastructure Code: Use CDKTF’s testing capabilities. Claude can help write unit tests for your constructs to ensure they behave as expected.

Conclusion

Claude Code transforms CDKTF development by bringing intelligent assistance to every phase of infrastructure development. From generating boilerplate code and building reusable constructs to reviewing for security and setting up CI/CD pipelines, Claude accelerates your workflow while improving code quality. Start by setting up proper context in your project, then iterate with specific, focused requests to get the most out of your CDKTF and Claude Code combination.

Built by theluckystrike — More at zovo.one