Deploying Webserver to AWS
Let's recap what we have done so far. We have
- created Copilot app in AWS application account
- created production environment in AWS production account
- created staging environment in AWS staging account
- 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.
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:
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
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
).