Skip to content

darllanfavero/aws

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 

Repository files navigation

AWS Builder Challenge #2 - CDK Infrastructure

CDK Version TypeScript License

This CDK project creates a complete, production-ready AWS infrastructure for Ajit N K's personal website as part of AWS Builder Challenge #2. The architecture follows AWS Well-Architected Framework principles with emphasis on security, cost optimization, and operational excellence.

πŸ—οΈ Architecture Overview

Core Infrastructure Components

  • 🌐 Route 53: DNS management with custom domain (ajitnk.in)
  • πŸͺ£ Amazon S3: Private bucket with encryption, versioning, and lifecycle policies
  • ⚑ CloudFront: Global CDN with custom domain, SSL certificate, and security headers
  • πŸ”§ AWS Lambda: Serverless contact form processing with input validation
  • πŸšͺ API Gateway: RESTful API with throttling and CORS configuration
  • πŸ“§ Amazon SNS: Email notifications for contact form submissions
  • πŸ”’ AWS Certificate Manager: SSL/TLS certificates for HTTPS

Architecture Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Route 53  │───▢│  CloudFront  │───▢│  S3 Bucket  β”‚
β”‚     DNS     β”‚    β”‚     CDN      β”‚    β”‚   Website   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚ API Gateway  │───▢│   Lambda    β”‚
                   β”‚   REST API   β”‚    β”‚ Contact Formβ”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                                              β–Ό
                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                       β”‚     SNS     β”‚
                                       β”‚ Notificationsβ”‚
                                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

Ensure you have the following installed and configured:

# Install AWS CDK CLI globally
npm install -g aws-cdk@2.87.0

# Configure AWS credentials (choose one method)
aws configure  # Interactive configuration
# OR
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_DEFAULT_REGION=us-east-1

# Verify AWS configuration
aws sts get-caller-identity

Installation & Setup

# Clone and navigate to project
cd aws-builder-challenge-cdk

# Install dependencies
npm install

# Verify TypeScript compilation
npm run build

# Synthesize CloudFormation templates (recommended for validation)
cdk synth

First-Time Deployment

# Bootstrap CDK in your AWS account (one-time setup)
cdk bootstrap

# Deploy all stacks
npm run deploy

# Or deploy with custom domain parameter
cdk deploy --parameters domainName=ajitnk.in

# Monitor deployment progress
# Deployment typically takes 15-20 minutes due to CloudFront distribution creation

πŸ“‹ Post-Deployment Steps

After deployment, CDK will output Route 53 name servers. Update your domain registrar (GoDaddy) with these name servers:

  1. Go to GoDaddy DNS Management
  2. Change name servers to the Route 53 name servers from CDK output
  3. Wait 5-15 minutes for DNS propagation
  4. Check email for SNS subscription confirmation
  5. Upload website files using the automation scripts

🎯 CDK Outputs

The stack provides these outputs for automation scripts:

  • WebsiteBucketName: S3 bucket name for file uploads
  • CloudFrontDistributionId: For cache invalidation
  • WebsiteUrl: Final website URL (https://ajitnk.in)
  • ApiGatewayUrl: Contact form API endpoint
  • Route53NameServers: Name servers for GoDaddy configuration
  • SNSTopicArn: Email notification topic

πŸ’° Cost Optimization

The infrastructure is optimized for AWS Free Tier:

  • S3: Versioning with lifecycle rules
  • CloudFront: Price Class 100 (US, Canada, Europe only)
  • Lambda: 256MB memory, 30s timeout
  • API Gateway: Throttling configured
  • Route 53: ~$0.50/month (only paid service)

πŸ”’ Security Features

  • Private S3 bucket with Origin Access Control (OAC)
  • HTTPS enforcement via CloudFront
  • Security headers (HSTS, CSP, X-Frame-Options)
  • Input validation and sanitization in Lambda
  • Rate limiting on API Gateway
  • Encrypted S3 storage

πŸ› οΈ Development

Useful Commands

npm run build     # Compile TypeScript to JS
npm run watch     # Watch for changes and compile
npm run test      # Run unit tests
npm run deploy    # Build and deploy all stacks
npm run destroy   # Delete all stacks
npm run bootstrap # Bootstrap CDK (one-time setup)
npm run diff      # Compare deployed stack with current state
npm run synth     # Emit the synthesized CloudFormation template

# Direct CDK commands
cdk deploy        # Deploy this stack to your default AWS account/region
cdk diff          # Compare deployed stack with current state
cdk synth         # Emit the synthesized CloudFormation template
cdk destroy       # Delete the stack

Development Workflow

For efficient development, follow this workflow:

# 1. Make changes to your CDK code
# 2. Synthesize to validate (recommended over npm run build)
cdk synth

# 3. Check differences before deployment
cdk diff

# 4. Deploy changes
npm run deploy

Important: Use cdk synth instead of npm run build for validation as it:

  • Automatically compiles TypeScript when needed
  • Validates CDK constructs and catches deployment issues
  • Generates CloudFormation templates for inspection
  • Provides more informative error messages

πŸ“ Project Structure

lib/
β”œβ”€β”€ website-stack.ts      # Main stack orchestration
β”œβ”€β”€ dns-stack.ts          # Route 53 hosted zone
β”œβ”€β”€ s3-stack.ts          # S3 bucket configuration
β”œβ”€β”€ cloudfront-stack.ts  # CloudFront distribution and SSL
└── api-stack.ts         # Lambda + API Gateway + SNS

bin/
└── app.ts               # CDK app entry point

cdk.out/                 # Generated CloudFormation templates
node_modules/            # Dependencies
package.json             # Project configuration and scripts
tsconfig.json           # TypeScript configuration
cdk.json                # CDK configuration

🏷️ Resource Tagging

All resources are tagged for cost tracking and organization:

  • Project: AWS-Builder-Challenge-2
  • Owner: Ajit-N-K
  • Environment: Production
  • Service: [S3, CloudFront, Lambda, etc.]
  • Purpose: [Website-Storage, Website-CDN, etc.]

πŸ”§ Configuration

The stack accepts these parameters:

  • domainName: Custom domain name (default: ajitnk.in)

Example with custom domain:

cdk deploy --parameters domainName=yourdomain.com

πŸ“ž Contact Form Integration

The contact form backend includes:

  • Input validation (name, email, message required)
  • HTML sanitization to prevent XSS
  • Rate limiting for security
  • Email notifications via SNS
  • CORS configuration for web integration

🌐 DNS Configuration

After deployment:

  1. Note the Route 53 name servers from CDK output
  2. Update your domain registrar's name servers
  3. DNS propagation typically takes 5-15 minutes
  4. Both ajitnk.in and www.ajitnk.in will work

πŸ“Š Monitoring

Basic monitoring is included:

  • CloudWatch metrics for all services
  • Lambda function logs
  • API Gateway access logs
  • CloudFront access logs (optional)

πŸ” Troubleshooting

Common Issues

Deployment fails with "Domain already exists":

  • Check if Route 53 hosted zone already exists
  • Ensure domain name parameter matches your actual domain

CloudFront distribution takes too long:

  • CloudFront distributions typically take 15-20 minutes to deploy
  • This is normal AWS behavior, not a project issue

SNS email not received:

  • Check spam folder
  • Confirm email subscription from AWS SNS
  • Verify SNS topic ARN in outputs

Website not accessible:

  • Ensure DNS propagation is complete (5-15 minutes)
  • Check CloudFront distribution status
  • Verify S3 bucket has website files uploaded

Debug Commands

# Check stack status
aws cloudformation describe-stacks --stack-name WebsiteStack

# View CloudFormation events
aws cloudformation describe-stack-events --stack-name WebsiteStack

# Check CDK differences
cdk diff

# Validate CloudFormation template
cdk synth > template.yaml
aws cloudformation validate-template --template-body file://template.yaml

🎯 AWS Builder Challenge Compliance

This infrastructure demonstrates:

  • βœ… Infrastructure as Code (CDK)
  • βœ… Serverless architecture
  • βœ… Security best practices
  • βœ… Cost optimization
  • βœ… Professional custom domain
  • βœ… Global content delivery
  • βœ… Working contact form
  • βœ… HTTPS encryption

πŸš€ Next Steps

After successful deployment:

  1. Upload Website Content: Use the provided S3 bucket name to upload your website files
  2. Test Contact Form: Verify the API Gateway endpoint works correctly
  3. Monitor Costs: Use AWS Cost Explorer to track spending
  4. Set Up Alerts: Configure CloudWatch alarms for monitoring
  5. Backup Strategy: Consider implementing automated backups for critical data

πŸ“š Additional Resources

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Ajit N K (@ajitnk2006)

  • Website: ajitnk.in
  • AWS Builder Challenge #2 Submission

Ready for AWS Builder Challenge #2 submission! πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors