|
1 | | -# Welcome to your CDK TypeScript project |
| 1 | +# Rabbits Playground AWS CDK |
2 | 2 |
|
3 | | -This is a blank project for CDK development with TypeScript. |
| 3 | +This project contains the AWS infrastructure for Rabbits Playground, ported from Terraform/Terragrunt to AWS CDK. |
4 | 4 |
|
5 | | -The `cdk.json` file tells the CDK Toolkit how to execute your app. |
| 5 | +## Architecture |
6 | 6 |
|
7 | | -## Useful commands |
| 7 | +The infrastructure includes: |
8 | 8 |
|
9 | | -* `npm run build` compile typescript to js |
10 | | -* `npm run watch` watch for changes and compile |
11 | | -* `npm run test` perform the jest unit tests |
12 | | -* `npx cdk deploy` deploy this stack to your default AWS account/region |
13 | | -* `npx cdk diff` compare deployed stack with current state |
14 | | -* `npx cdk synth` emits the synthesized CloudFormation template |
| 9 | +- **VPC**: Multi-AZ networking with public and private subnets |
| 10 | +- **EKS**: Kubernetes cluster with managed node groups (ARM64/Graviton instances) |
| 11 | +- **Bastion**: EC2 bastion host for secure access |
| 12 | +- **S3**: Static content and user content buckets with lifecycle policies |
| 13 | +- **CloudFront**: CDN for static content delivery |
| 14 | +- **Route53**: DNS management and routing |
| 15 | + |
| 16 | +## Project Structure |
| 17 | + |
| 18 | +``` |
| 19 | +├── bin/ |
| 20 | +│ └── rabbits-playground-aws-cdk.ts # CDK app entry point |
| 21 | +├── lib/ |
| 22 | +│ ├── constructs/ # Reusable constructs |
| 23 | +│ │ ├── vpc-construct.ts |
| 24 | +│ │ ├── eks-construct.ts |
| 25 | +│ │ ├── bastion-construct.ts |
| 26 | +│ │ ├── s3-construct.ts |
| 27 | +│ │ ├── cloudfront-construct.ts |
| 28 | +│ │ └── route53-construct.ts |
| 29 | +│ └── stacks/ |
| 30 | +│ └── infrastructure-stack.ts # Main infrastructure stack |
| 31 | +├── config/ |
| 32 | +│ └── environment.ts # Environment-specific configurations |
| 33 | +└── cdk.json # CDK configuration |
| 34 | +``` |
| 35 | + |
| 36 | +## Environments |
| 37 | + |
| 38 | +Two environments are supported: |
| 39 | +- **dev**: Development environment with cost-optimized settings |
| 40 | +- **prod**: Production environment with high-availability settings |
| 41 | + |
| 42 | +### Key Differences |
| 43 | + |
| 44 | +| Feature | Dev | Prod | |
| 45 | +|---------|-----|------| |
| 46 | +| VPC CIDR | 10.0.0.0/16 | 10.1.0.0/16 | |
| 47 | +| NAT Gateways | 1 (cost optimization) | 3 (multi-AZ HA) | |
| 48 | +| EKS Capacity | SPOT instances | ON_DEMAND instances | |
| 49 | +| EKS Instance Type | t4g.medium | t4g.large, t4g.xlarge | |
| 50 | +| EKS Nodes | 2 desired (1-3) | 3 desired (3-10) | |
| 51 | +| EKS API Access | Public | Private | |
| 52 | +| Log Retention | 7 days | 30 days | |
| 53 | +| S3 Versioning | Disabled | Enabled | |
| 54 | +| S3 Lifecycle | Disabled | Enabled (IA at 90d, Glacier at 180d) | |
| 55 | + |
| 56 | +## Prerequisites |
| 57 | + |
| 58 | +- Node.js 14.x or later |
| 59 | +- AWS CLI configured with appropriate credentials |
| 60 | +- AWS CDK CLI: `npm install -g aws-cdk` |
| 61 | + |
| 62 | +## Setup |
| 63 | + |
| 64 | +1. Install dependencies: |
| 65 | +```bash |
| 66 | +npm install |
| 67 | +``` |
| 68 | + |
| 69 | +2. Configure AWS credentials: |
| 70 | +```bash |
| 71 | +export AWS_ACCOUNT_ID=your-account-id |
| 72 | +export AWS_REGION=us-west-2 |
| 73 | +``` |
| 74 | + |
| 75 | +3. Bootstrap CDK (first time only): |
| 76 | +```bash |
| 77 | +cdk bootstrap aws://${AWS_ACCOUNT_ID}/${AWS_REGION} |
| 78 | +``` |
| 79 | + |
| 80 | +## Deployment |
| 81 | + |
| 82 | +### Deploy to Dev Environment |
| 83 | + |
| 84 | +```bash |
| 85 | +cdk deploy -c environment=dev |
| 86 | +``` |
| 87 | + |
| 88 | +### Deploy to Prod Environment |
| 89 | + |
| 90 | +```bash |
| 91 | +cdk deploy -c environment=prod |
| 92 | +``` |
| 93 | + |
| 94 | +## Useful Commands |
| 95 | + |
| 96 | +* `npm run build` - Compile TypeScript to JavaScript |
| 97 | +* `npm run watch` - Watch for changes and compile |
| 98 | +* `npm run test` - Run unit tests |
| 99 | +* `cdk diff -c environment=dev` - Compare deployed stack with current state |
| 100 | +* `cdk synth -c environment=dev` - Emit the synthesized CloudFormation template |
| 101 | +* `cdk deploy -c environment=dev` - Deploy stack to AWS account/region |
| 102 | +* `cdk destroy -c environment=dev` - Remove stack from AWS account/region |
| 103 | + |
| 104 | +## Configuration |
| 105 | + |
| 106 | +Environment-specific configurations are defined in `config/environment.ts`. Update these values as needed for your environments: |
| 107 | + |
| 108 | +- VPC CIDR ranges |
| 109 | +- EKS cluster settings |
| 110 | +- Bastion access CIDRs |
| 111 | +- S3 bucket names |
| 112 | +- Domain names |
| 113 | +- Route53 hosted zone settings |
| 114 | + |
| 115 | +## Connecting to Resources |
| 116 | + |
| 117 | +### EKS Cluster |
| 118 | + |
| 119 | +After deployment, configure kubectl: |
| 120 | +```bash |
| 121 | +aws eks update-kubeconfig --region us-west-2 --name rabbits-playground-dev |
| 122 | +``` |
| 123 | + |
| 124 | +### Bastion Host |
| 125 | + |
| 126 | +Connect via SSM Session Manager: |
| 127 | +```bash |
| 128 | +aws ssm start-session --target <instance-id> |
| 129 | +``` |
| 130 | + |
| 131 | +Or via SSH (if key pair configured): |
| 132 | +```bash |
| 133 | +ssh -i your-key.pem ec2-user@bastion.dev.example.com |
| 134 | +``` |
| 135 | + |
| 136 | +## Outputs |
| 137 | + |
| 138 | +After deployment, the stack outputs include: |
| 139 | + |
| 140 | +- VPC ID and subnet IDs |
| 141 | +- EKS cluster endpoint and security group |
| 142 | +- Bastion host IPs |
| 143 | +- S3 bucket names and ARNs |
| 144 | +- CloudFront distribution domain name |
| 145 | +- Route53 hosted zone details |
| 146 | + |
| 147 | +## Security Considerations |
| 148 | + |
| 149 | +- EKS nodes run in private subnets |
| 150 | +- Bastion host uses IMDSv2 and SSM for secure access |
| 151 | +- S3 buckets have encryption and public access blocking enabled |
| 152 | +- CloudFront enforces HTTPS-only access |
| 153 | +- Security groups restrict traffic by default |
| 154 | +- IAM roles follow principle of least privilege |
| 155 | + |
| 156 | +## Migration from Terraform |
| 157 | + |
| 158 | +This CDK implementation replaces the previous Terraform/Terragrunt setup with equivalent functionality: |
| 159 | + |
| 160 | +- Terraform modules → CDK constructs |
| 161 | +- Terragrunt environments → CDK context |
| 162 | +- Remote state in S3 → CloudFormation stacks |
| 163 | +- Outputs → CDK CfnOutput |
| 164 | + |
| 165 | +## Support |
| 166 | + |
| 167 | +For issues or questions, please refer to the AWS CDK documentation: |
| 168 | +- https://docs.aws.amazon.com/cdk/ |
| 169 | +- https://github.com/aws/aws-cdk |
0 commit comments