Deploying a rails application on Amazon ECS — DevOps Project 02
GH repo here
Hi everyone! This is me learning and building in public. The explanation in this post is limited, I hope the screenshots will cater to that, still if you have any questions, wanting to do it on your own, or any suggestions whatsoever please reach out to me on X.
Let’s first understand the difference between ECR and ECS:
- Elastic Container Registry: A registry for container images
- Elastic Container Service: Uses ECS to pull container images and host them, simplifies the process of deploying, managing and scaling containerized apps.
Now let’s begin with the project
The app: Containerd Rails is a simple rails application that is containerized using Docker. Read more about the project in this linked post.
The next step? Taking it online. Using Amazon ECS. Let’s begin!
Our approach will be as follows:
- Create a new IAM user for this task, it is a good practice to do so.
- Push the docker image to Amazon ECR
- ECS setup
- Create Cluster
- Define Task Definition - Deploy and Test
Creating IAM user
- Attach policies directly:
- AmazonEC2ContainerRegistryFullAccess
- AmazonECS_FullAccess
- IAMFullAccess
2. Use tags, good for filtering
3. Create access key to be able to push as this user from AWS CLI.
Now let’s configure AWS CLI as this user and push our image to ECR.
Pushing image to ECR
Steps to follow:
- Create a Repo
- View push commands
and push your docker image to ECR
ECS Setup
The next setp is to setup ECS. The steps are:
- Create cluster
Toggle on use container insights and use tags, for better monitoring and filtering
2. Create Task Definition (information about your container like the registry)
Provide your ECR url (mapping of port 5432 is not required for our application as we are not going with multi-container image)
ecsTaskExecutionRole gets created
Add Cloudwatch logging policy to this role
Now go to cluster
Services-> create
We go with service as our container will be running to serve the application, it is not a one time task
Task definition, the one we created gets used next:
Created a security group to allow access on port 3000 for our application
For production, we can go with the following options:
And we’re done, the next step is to check the application. Get the ip from ‘Task’ section and find public ip there, navigate to ip:3000 to see our application, up and running.
Check the logs to have real time updates about the container. We have used Amazon Fargate, giving us the features of serverless architecture, that is, we don’t have to cater to auto scaling, managing and keeping the server updated, it is all handled as per the need of the container.
Be sure to clean everything up
Delete
- Task definition
- ECR repo
- the roles
- the user
Questions and Suggestions are welcome!
Completed the second project, follow me as I learn and build in public: X @ bhavyansh001