DEV Community

developerz.ai
developerz.ai

Posted on

Designing a Scalable CI/CD Pipeline for Rails Apps on AWS

Designing a Scalable CI/CD Pipeline for Rails Apps on AWS

Introduction

Continuous integration and delivery are essential for shipping reliable Rails applications quickly. This article walks through a production-grade pipeline built on AWS services, focusing on reproducibility, security, and low latency deployments.

Architecture Overview

The pipeline uses AWS CodeCommit for source control, CodeBuild for building Docker images, and CodePipeline to orchestrate the stages. Deployments target an Elastic Beanstalk environment that runs the Rails app behind an Application Load Balancer. Monitoring is provided by CloudWatch and X-Ray.

Setting up CodeCommit

Create a repository in CodeCommit and push your Rails code. Enable branch protection on main to require pull-request reviews before merges.

aws codecommit create-repository --repository-name my-rails-app
Enter fullscreen mode Exit fullscreen mode

Building with CodeBuild

Define a buildspec.yml that installs Ruby, runs bundle install, runs tests, and builds a Docker image.

version: 0.2
phases:
  install:
    runtime-versions:
      ruby: 3.2
  pre_build:
    commands:
      - bundle install --jobs=4 --retry=3
  build:
    commands:
      - bundle exec rspec
      - docker build -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION.
  post_build:
    commands:
      - docker push $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION
artifacts:
  files: []
Enter fullscreen mode Exit fullscreen mode

Orchestrating with CodePipeline

Create a pipeline with three stages: Source, Build, and Deploy. The Deploy stage invokes Elastic Beanstalk to roll out the new Docker image.

{
  "pipeline": {
    "name": "RailsDeployPipeline",
    "stages": [
      {"name": "Source", "actions": [{"name": "Source", "actionTypeId": {"category": "Source", "owner": "AWS", "provider": "CodeCommit", "version": "1"}, "outputArtifacts": [{"name": "SourceOutput"}]}]},
      {"name": "Build", "actions": [{"name": "Build", "actionTypeId": {"category": "Build", "owner": "AWS", "provider": "CodeBuild", "version": "1"}, "inputArtifacts": [{"name": "SourceOutput"}], "outputArtifacts": [{"name": "BuildOutput"}]}]},
      {"name": "Deploy", "actions": [{"name": "Deploy", "actionTypeId": {"category": "Deploy", "owner": "AWS", "provider": "ElasticBeanstalk", "version": "1"}, "inputArtifacts": [{"name": "BuildOutput"}]}]}
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Deploying with Elastic Beanstalk

Elastic Beanstalk handles environment provisioning, load balancing, and scaling. Use a Docker platform configuration and set environment variables for secrets.

aws elasticbeanstalk create-application-version \
  --application-name my-rails-app \
  --version-label $CODEBUILD_RESOLVED_SOURCE_VERSION \
  --source-bundle S3Bucket=my-bucket,S3Key=builds/$CODEBUILD_RESOLVED_SOURCE_VERSION.zip
Enter fullscreen mode Exit fullscreen mode

Monitoring and Rollback

Enable CloudWatch alarms on request latency and error rates. If a deployment degrades performance, Elastic Beanstalk can automatically roll back to the previous version.

aws cloudwatch put-metric-alarm --alarm-name HighLatency --metric-name Latency --namespace AWS/ElasticBeanstalk --statistic Average --period 60 --threshold 500 --comparison-operator GreaterThanThreshold --evaluation-periods 3 --alarm-actions arn:aws:sns:us-east-1:123456789012:OpsAlerts
Enter fullscreen mode Exit fullscreen mode

Conclusion

A CI/CD pipeline built on AWS services gives Rails teams fast feedback, reliable deployments, and easy scaling. By separating source control, build, and deployment concerns, you keep the process transparent and maintainable. The same patterns apply to other frameworks, making the approach reusable across projects.

Top comments (0)