AWS CDK project with TypeScript that creates an EC2 instance in a VPC.
- VPC with 2 availability zones
- Security Group (no inbound rules)
- EC2 Instance (t2.micro) with Windows Server 2022
- Node.js and npm
- AWS CLI configured (
aws configure) - AWS Account with appropriate permissions
# 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 destroynpx cdk synthConverts 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.
npx cdk bootstrapSets 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."
npx cdk deployDeploys 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.)
npx cdk diffShows 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.
npx cdk destroyRemoves 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.
npx cdk listDisplays all stack names defined in your CDK app. Useful for multi-stack applications to verify stack names before deployment.
npx cdk doctorChecks 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.
npm testRuns Jest unit tests to validate your infrastructure code before deployment. Tests verify that your CDK constructs create the expected AWS resources with correct configurations.