Deployment on AWS Fargate
Relevant source files
The following files were used as context for generating this wiki page:
Deployment on AWS Fargate within the Loco framework leverages Infrastructure as Code (IaC) via Terraform to provide a scalable, serverless environment for Rust applications. By utilizing containerization, Loco applications are packaged into Docker images, pushed to Amazon Elastic Container Registry (ECR), and orchestrated through Amazon Elastic Container Service (ECS) using the Fargate launch type. This approach eliminates the need for managing underlying EC2 instances while maintaining high performance and security.
The deployment process is facilitated by Loco's built-in CLI generators, which automate the creation of necessary containerization files such as Dockerfile and .dockerignore. This integration ensures that the application environment—including static assets and server configurations—is correctly mirrored in the production cloud environment.
Sources: docs-site/content/blog/deploy-aws.md:1-15, README.md:55-60
Deployment Architecture
The Loco deployment architecture on AWS Fargate is structured around a Virtual Private Cloud (VPC) with public and private subnets across multiple availability zones. An Application Load Balancer (ALB) handles incoming traffic, performing SSL termination and routing requests to the ECS service running in private subnets.
Infrastructure Components
The following table summarizes the key AWS resources utilized in a standard Loco Fargate deployment:
| Component | AWS Resource | Purpose |
|---|---|---|
| Container Registry | ECR Repository | Stores the versioned Docker images of the Loco app. |
| Orchestration | ECS Cluster | Manages the lifecycle and scaling of Fargate tasks. |
| Compute | Fargate Service | Runs the containerized application without managing servers. |
| Networking | VPC, Subnets, IGW | Provides isolated network infrastructure. |
| Load Balancing | Application Load Balancer | Distributes traffic and provides a public DNS entry. |
| Security | Security Groups | Controls ingress/egress for the ALB and ECS tasks. |
Sources: docs-site/content/blog/deploy-aws.md:118-200
Network and Traffic Flow
The system uses a NAT Gateway to allow tasks in private subnets to access the internet (e.g., for pulling updates or external API calls) while preventing direct unsolicited inbound access.
This diagram illustrates the flow of traffic from a user through the Load Balancer to the containerized Loco application located in a secure private subnet.
Containerization and Image Management
Loco simplifies the transition from local development to cloud deployment through its command-line interface. The cargo loco generate deployment command detects the application type and generates a standard Dockerfile.
Docker Generation Logic
The CLI identifies if the application is a Rest API, SaaS, or lightweight service and includes specific paths for static assets if a frontend is detected.
The sequence of steps required to transform a local Loco project into a cloud-ready container image.
Sources: src/cli.rs:370-385, snipdoc.yml:68-75, docs-site/content/blog/deploy-aws.md:40-60
Resource Configuration in Task Definitions
The ECS Task Definition specifies the hardware requirements and runtime parameters for the container.
- CPU/Memory: Default configuration uses 256 CPU units and 512 MiB of memory.
- Port Mapping: The container typically listens on port
5150. - Logging: Configured via
awslogsdriver to stream logs to CloudWatch. - Health Checks: The ALB target group monitors the
/_healthendpoint, expecting 200 or 202 status codes.
Sources: docs-site/content/blog/deploy-aws.md:144-175, docs-site/content/blog/deploy-aws.md:225-235
Terraform Configuration
Infrastructure as Code (IaC) is managed through main.tf and network.tf files. These files define the relationship between IAM roles and the execution environment.
IAM and Security
A critical component of the deployment is the ecsTaskExecutionRole. This role allows ECS to pull images from ECR and send logs to CloudWatch.
Class relationship showing how IAM roles are linked to ECS tasks to provide necessary permissions.
Load Balancer Configuration
The ALB is configured with two listeners:
- HTTP (Port 80): Automatically redirects all traffic to HTTPS (Port 443) with a 301 status code.
- HTTPS (Port 443): Utilizes an SSL certificate and forwards traffic to the Loco target group.
Sources: docs-site/content/blog/deploy-aws.md:240-265
Deployment Commands Reference
| Command | Action | Description |
|---|---|---|
cargo loco generate deployment | Generate Files | Creates the Dockerfile and deployment scripts. |
aws ecr create-repository | ECR Setup | Creates the remote repository to host images. |
terraform init | Initialize | Sets up the Terraform environment and providers. |
terraform apply | Deploy | Provisions all AWS resources and starts the service. |
cargo loco doctor | Verify | Ensures local environment is ready for build/deploy. |
Sources: docs-site/content/blog/deploy-aws.md:90-110, docs-site/content/blog/deploy-aws.md:360-380, snipdoc.yml:60-65
Conclusion
Deploying a Loco application on AWS Fargate provides a robust production environment characterized by high availability and minimal operational overhead. By integrating Docker for containerization and Terraform for infrastructure management, Loco ensures that the "one-person framework" philosophy extends to deployment, allowing developers to scale their Rust applications from single instances to multi-zone clusters with a standardized, repeatable process.
Sources: docs-site/content/blog/deploy-aws.md:390-405, README.md:100-110