Skip to content

Deploying Webserver to AWS

Let's recap what we have done so far. We have

  1. created Copilot app in AWS application account
  2. created production environment in AWS production account
  3. created staging environment in AWS staging account
  4. created webserver and worker manifests

We're ready to deploy our Rails app to AWS.

Configure Rails

Add following to application.rb. AWS Load Balancer will generate a domain, but we don't know what it is yet. So we will allow any domain for now.

config.hosts = [
  /.*/,
]

Add Environment Variables to AWS

copilot secret init
What would you like to name this secret? [? for help] RAILS_MASTER_KEY
What is the value of secret RAILS_MASTER_KEY in environment prod? # paste the master key from config/master.key to here
What is the value of secret RAILS_MASTER_KEY in environment staging? # paste the master key from config/master.key to here

After secrets are added, add following to webserver manifests so the containers can use these environment variables.

webserver/manifest

secrets:
    RAILS_MASTER_KEY: /copilot/${COPILOT_APPLICATION_NAME}/${COPILOT_ENVIRONMENT_NAME}/secrets/RAILS_MASTER_KEY

Deploy Webserver

Now we're ready to deploy webserver to AWS. Run

copilot svc deploy --app rails70 --env staging --name webserver # push to staging
copilot svc deploy --app rails70 --env prod --name webserver # push to production

Info

You need to have Docker running on your local. Copilot will build a docker image, deploy it to ECR and run the image in ECS.

Info

We only need to build the image manually for the first time. Later we will move the building/deployment process to Github Actions.

Successful Deployment

A Successful Deployment

You will see an URL at the end of the deployment. That's the URL of the Load Balancer which Copilot has provisioned for us. Open up the URL: Screen Shot 2023-05-06 at 7 46 15 pm

Congratulations, you have deployed your first container to AWS 🎉

Info

When Copilot is deploying the services, you can check the log on AWS to see if there's any error Screen Shot 2023-05-06 at 7 39 51 pm

If there is, don't waste time waiting for Copilot to return error. Go to CloudFormation, delete the task, fix the error and redeploy (copilot svc deploy). Screen Shot 2023-05-06 at 7 40 15 pm