Skip to content

codeofpeace/aws-simple-cdk-infra

Repository files navigation

Simple AWS CDK Project

AWS CDK project with TypeScript that creates an EC2 instance in a VPC.

What's Included

  • VPC with 2 availability zones
  • Security Group (no inbound rules)
  • EC2 Instance (t2.micro) with Windows Server 2022

Prerequisites

  • Node.js and npm
  • AWS CLI configured (aws configure)
  • AWS Account with appropriate permissions

Quick Start

# Install dependencies
npm install

# Configure AWS credentials (first time)
aws configure

# Bootstrap AWS environment (first time per region)
npx cdk bootstrap

# Deploy the stack
npx cdk deploy

# Clean up when done
npx cdk destroy

Detailed Commands

1. Synthesize CloudFormation template

npx cdk synth

Converts your CDK code into a CloudFormation template and displays it. Use this to preview what resources will be created without actually deploying.

This command generates the CloudFormation JSON/YAML that AWS will use to create your infrastructure. It's useful for debugging and understanding exactly what CDK will provision.

2. Bootstrap your AWS environment (first time only)

npx cdk bootstrap

Sets up the necessary AWS resources for CDK deployments in your account/region. This creates an S3 bucket and other resources needed for CDK operations. Only needs to be run once per account/region combination.

Bootstrap is a one-time, per-region setup that prepares AWS to let CDK deploy resources. No manual configuration is required for standard usage. It's basically CDK saying: "Here's a workspace in your AWS account so I can deploy safely."

3. Deploy the stack

npx cdk deploy

Deploys your CDK stack to AWS. It will show you the changes and ask for confirmation before creating resources. This command:

  • Synthesizes your CDK code into CloudFormation
  • Uploads assets to the bootstrap bucket
  • Creates/updates the CloudFormation stack
  • Provisions all defined AWS resources (VPC, EC2, Security Group, etc.)

4. Compare deployed stack with current state

npx cdk diff

Shows the differences between your local CDK code and the deployed stack in AWS. Helpful before deploying updates to understand what will change.

The output uses color coding: green for additions, red for deletions, and yellow for modifications. This lets you review changes before committing them to AWS.

5. Destroy the stack

npx cdk destroy

Removes all resources created by the stack from your AWS account. This will delete the EC2 instance, VPC, Security Group, and all associated resources.

Use with caution as this operation is irreversible. All data and resources will be permanently deleted from AWS.

6. List all stacks

npx cdk list

Displays all stack names defined in your CDK app. Useful for multi-stack applications to verify stack names before deployment.

7. Check project health

npx cdk doctor

Checks your CDK project for potential issues and displays useful information about your environment, including CDK version and AWS environment variables.

This helps troubleshoot configuration problems and ensures your environment is properly set up.

8. Run unit tests

npm test

Runs Jest unit tests to validate your infrastructure code before deployment. Tests verify that your CDK constructs create the expected AWS resources with correct configurations.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published