This project demonstrates how to create an EC2 instance with AWS CDK, including:
- VPC with public subnets
- Security groups for SSH access
- EC2 instance with Amazon Linux 2023
- CloudFormation Init for instance configuration
- Asset deployment via S3
- CloudWatch integration
- AWS CLI configured with appropriate credentials
- Node.js 16 or later
- TypeScript
You can customize the deployment with these environment variables:
| Variable | Description | Default |
|---|---|---|
LOG_LEVEL |
Logging level | INFO |
SSH_PUB_KEY |
Your SSH public key for instance access | (empty) |
CPU_TYPE |
CPU architecture (ARM64 or X86) |
ARM64 |
INSTANCE_SIZE |
Instance size (LARGE, XLARGE, XLARGE2, XLARGE4) |
LARGE |
# Install dependencies
npm install
# Build the project
npm run build
# Deploy the stack
npx cdk deployAfter deployment, the CDK will output commands to connect to your instance:
- Using SSH:
ssh ec2-user@<public-dns-name> - Using SSM:
aws ssm start-session --target <instance-id>
npm testnpx cdk destroybin/app.ts- Entry point for CDK applicationlib/ec2-stack.ts- Main stack definitionlib/constructs/- CDK constructs for VPC and EC2 serverlib/utils/- Utility functions and validatorslib/resources/- Configuration files and assets for the EC2 instancetest/- Jest tests for the CDK constructs