Development/AWS

Hosting React Apps on AWS S3 + CloudFront: Managing Infrastructure as Code with CDK

kozylife 2025. 7. 28. 19:56

When deploying React SPAs (Single Page Applications), many developers consider S3 static hosting as their go-to solution. However, using S3 alone presents several challenges that can impact security, performance, and user experience. In this article, we'll explore how to leverage AWS CDK to host React applications on S3 + CloudFront in a secure and efficient manner.

Common Challenges with React SPA Hosting

1. Security Configuration Complexity

To enable static website hosting on S3, you need to make your bucket public. However, this approach isn't recommended from a security standpoint. Finding the right balance between public access and security policies presents a significant challenge.

2. SPA Routing Issues

The most common problem with SPAs using React Router is the 404 error that occurs when users directly access specific routes. For instance, when someone tries to access /about directly, S3 can't find the corresponding file and returns a 404 error.

3. Performance Limitations

Hosting directly on S3 without a global CDN can result in slow loading times. This is particularly noticeable when accessing S3 buckets in distant regions, such as accessing a US-based S3 bucket from Asia.

4. Manual Setup Hassles

Configuring everything through repetitive clicks in the AWS console is inefficient and prone to human error.

5. Inadequate Deployment Process

The inconvenience of manually uploading files and invalidating cache every time you deploy can slow down your development workflow.

The Solution: S3 + CloudFront + CDK

Here's the architecture that addresses all these challenges:

User Request
    ↓
CloudFront (CDN)
    ↓
Origin Access Identity
    ↓
S3 Bucket (Static Files)

Key Components

S3 Bucket: Stores React build files (kept completely private) Origin Access Identity: Allows only CloudFront to access S3 CloudFront Distribution: Global CDN + HTTPS enforcement + SPA routing support Error Response Rules: Redirects 404/403 errors to index.html

Implementing with CDK

AWS CDK (Cloud Development Kit) allows you to manage infrastructure as code, providing several key benefits:

  • Version Control: Track infrastructure changes through Git
  • Reusability: Easily deploy the same structure across multiple environments
  • Consistency: Prevent errors caused by manual configuration
  • Automation: Seamlessly integrate with CI/CD pipelines

Basic Usage

# Install dependencies
npm install

# CDK bootstrap (first time only)
cdk bootstrap

# Deploy stack
cdk deploy

# Remove stack
cdk destroy

Advanced Features and Optimization Strategies

Security Enhancements

  • WAF rule implementation
  • Geo-restriction settings
  • Detailed IAM permission configuration

Performance Optimization

  • Differentiated TTL settings by file type
  • Gzip/Brotli compression optimization
  • HTTP/2 Push for critical resource preloading

Operational Management

  • CloudWatch dashboard + alarm setup
  • CloudTrail + CloudWatch logging activation
  • Automated cache invalidation
  • Rollback mechanisms to previous versions

Environment Separation

Configure separate stacks for development/staging/production environments to build a secure deployment pipeline.

Important Considerations

This project was written for Proof of Concept purposes. Before using it in a production environment, please review the following items:

  • Development Environment Settings: Review removalPolicy: DESTROY and autoDeleteObjects: true configurations
  • Basic Security: Only minimal security settings are applied
  • Simple Error Handling: Provides only basic SPA routing support
  • Limited Regions: PRICE_CLASS_100 (North America/Europe only)

Conclusion

Hosting React SPAs with AWS CDK provides significant benefits in terms of security, performance, and operations. By managing infrastructure as code, you can build a more stable and scalable deployment environment without the hassle of manual configuration. For production environments, it's crucial to build a more robust system by incorporating additional considerations such as enhanced security, monitoring, and disaster recovery planning.

 

This article serves as an educational example that demonstrates the basic concepts and implementation methods of S3 + CloudFront hosting. For production use, it should be expanded to reflect additional considerations from security, performance, and operational perspectives.