-
Notifications
You must be signed in to change notification settings - Fork 0
Home
- Team Members
- Introduction & Purpose
- Amazon Web Services S3 Bucket
- Initializing the Ruby on Rails (RoR) Application
- Running on Local Machine (RubyMine IDE)
- Containerizing the Ruby on Rails Application
- Building Docker Image and Running on Local Machine
- Elastic Cloud 2 Instance Creation
- Deploying the Docker on Newly Created Instance
- Running the Docker Image on AWS EC2
- Final Application
- Project YouTube URL
- Issue 1 Solution - Exporting Instance Image
- Issue 2 Solution - Allocation of Static IP
- Image Reference
- This Application is build on Rails Framework of Ruby Programming language. We've used the docker container to host this application on AWS EC2 (Amazon Web Services Elastic Cloud) Instance. All the images used in this project are hosted on the AWS S3 Bucket with public access. This application mainly contains two web pages (Views in Rails) which are given below :-
- User List
- This shows the list of users registered on the application. Their names will be href links. By clicking the link users can see the photos uploaded by specific user.
- User Photo List
- This shows the list of photos uploaded by the user. This will have a comment section also under the images uploaded by user. In comment section there are three Values
- User Comment
- User Picture
- Comment Date
- By clicking the username (who created the comment), The photo list page will be opened for that user.
YouTube URL Link - https://youtu.be/Wn6upLW8vS8
GitHub Code Link - https://github.com/WebSystemProject/R_AWS
GitHub Wiki Link - https://github.com/WebSystemProject/R_AWS/wiki
- By following this documentation you'll learn following concepts:-
- Ruby on Rails Framework
- Database Model in Rails
- Docker Containers
- Hosting Docker on EC2
- AWS S3 Bucket Configuration as well as Uploading Content
- DockerHub Repository Commands
- Go to AWS Console and Select S3 Bucket.
- Now Click on Create bucket.
.png)
- Click Upload Button and Upload all the files.
.png)
- To give public access to content select image and click make public.
.png)
-
Now We'll generate Bucket Policy. Select Bucket -> Permissions -> Bucket Policy -> Policy Generator
-
But before generation policy We need IAM Role created for our EC2 instance.
- Go to services -> IAM -> Roles -> Create Role
.png)
- Select EC2 Services.
.png)
- Then Select AmazonS3ReadOnlyAccess / AmazonS3FullAccess
.png)
- Now Fill Role Name and Click Create Role
.png)
- Now Go to S3 Instance and attach this IAM role to that instance. Instance -> Actions -> Security -> Modify IAM Roles
.png)
- Now Click Save
.png)
-
Now fill the details in following fields
- Select Policy type: S3 bucket Policy.
- Effect: Allow
- Actions: GetObject
- Paste bucket ARN and add /* after this so the access is applied to all photos .
- Click Add Statement.
.png)
- Click Generate Policy
.png)
- Copy this JSON To Policy and Click Save.
-
Create a new RoR Project.
New Project -> Rails -> application -> Select Rails Version. For database select postgres. Click Create. -
Now We'll create the Controller and Views for Application
-
Photo
-
Comment
-
ListUser
Select Tools -> Run Rails Generator -> Type Controller followed by Model Names.
It will generate the model as well as the views for the application
.png)
.png)
.png)
-
Now We'll create the Model in Database for Application
Select Tools -> Run Rails Generator -> Type Model Names followed by Model Names.
.png)
- Usercomment
- Userphoto
- Userlist
Now It will generate Model Files which will be used to fetch the data from database and then render the views to the user.
Now in the models we'll define the relation between models.
- Comments belongs to Photo as well as User
.png)
- User is parent to Comment
.png)
- Photo is parent to Comment
.png)
-
Now We'll create some migration files for our database relations.
Tools-> Run Rails Generator -> Migration File name followed by Migration.
- Adding Foreign Key to User Comment
- Adding Photo to Comment
- Adding User to Comment
This will create following migrations.
.png)
-
Now Run the migration. Run the following command:
rails db:migrate -
The model and controller are generated Now We'll write Code in Views. Views are the HTML+CSS+JS which will be rendered to user in frontend.
-
User List View
.png)
- Photo View
.png)
- Click Shift+F10 Or Run Button
.png)
- It will Start a Rails web server in the application and will be listening the incoming connection on port 3000.
.png)
What is Docker
When we run the application local machine, we install dependencies related to application to make it running on local machine. but when we deploy this application on cloud we need to again install the dependencies there. To Solve this issue we create a image file on local machine which is having all the dependencies installed. Then we deploy this image to Cloud. Now we don't need to install anything on the cloud. We just have to run this image. This image is called docker image.
We'll create this image and will upload this image on docker hub (Which is the official repository hub of docker images). Then We'll pull this image on our EC2 instance and run it over there.
Section 2) Building the docker image and hosting this image on docker hub. Then running this application through docker.
- Select Create Repository and fill the details like repository name and description. Select the repository visibility to public.
.png)
- Repository Description Like Pull commands.
.png)
To Build the docker image we need two files.
- Dockerfile (Main File) Create the Dockerfile and fill the details according to project requirement. This file is mainly responsible for all the dependencies as well image building.
.png)
- docker-compose.yml (Services File) This file is responsible for services running on application like database and server
.png)
-
Now Go to terminal and run the following command
docker build -t project:docker .
.png)
-
It will build a docker images which we'll check with following command
docker images
.png)
-
Now We'll run this image on local system
docker run -p 3000:3000 -it project:docker
.png)
Server Starts in listening mode on port 3000 through docker container. Wow We've come so far !
- To View to home page visit http://localhost:3000/users/index. This will show the list of users and then we can click on the user name to see all the photos uploaded by them.
.png)
-
Now we'll tag this image to docker hub and then will push this image to dockerhub.
docker tag project:docker manoharsingh1025/raws -
Now hit the push command to host this image on dockerhub
docker push manoharsingh1025/raws
.png)
-
Login to Amazon Web Services and Search for EC2
-
Now Select Launch Instance
.png)
- It will ask you to select Image Select Ubuntu Server (20.04)
.png)
- Now Select the instance tier t2 -> t2.micro (Free Tier Available) and Click Review and Launch It will have all the setting by default like security group and storage etc.
.png)
- Now Create a Key-Pair to access your instance through ssh (SSH -i Keypair.pem Instanceurl)
.png)
-
Now Click Launch Instance
-
It will take around 2 to 5 minutes to initializing an instance for you.
-
After the instance is created. Click on the connect box and click SSH Client And copy the string and paste it in your machine's command line
.png)
- Paste the string in cmd (Windows). Press yes for first time and congratulations you're successfully logged in to your instance.
.png)
-
After you're logged in to your instance. Run the following command in the terminal to update the advance packet manager repositories.
sudo apt-get update
.png)
-
Then We'll run the following command to install the required packages on EC2 instance.
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
.png)
-
Then Run the curl command to get the PGP public key and add this key to your EC2 Instance. This PGP(GPG) key is used to validate the images on the repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - -
Now We'll add the fingerprint by running the following command
sudo apt-key fingerprint 0EBFCD88 -
To add the repository we'll run following command
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" -
Finally run this command to update the repository
sudo apt-get update -
Now the repositories are updated, we can install the docker.
sudo apt-get install docker-ce docker-ce-cli containerd.io
.png)
-
This command will give you options to select which docker version you want to install on the instance.
apt-cache madison docker-ce
.png)
-
Now Select the Version and add this to following command to install that version.
sudo apt-get install docker-ce=5:20.10.4~3-0~ubuntu-focal docker-ce-cli=5:20.10.4~3-0~ubuntu-focal containerd.io
-
To start docker service on EC2 instance
sudo service docker start -
Pull the docker image on EC2 instance
sudo docker pull manoharsingh1025/raws -
To install and run docker image of Application
sudo docker run -p 3000:3000 -it manoharsingh1025/raws
This will start the server in listening mode on port 3000. But Still the web page is not accessible because we haven't setup the EC2 instance to accept request on port 3000
.png)
To accept connection on port 3000. We'll add new rule to accept connection on port 3000.
Instance -> Security -> Security Groups -> Inbound Rule
.png)
Now From the terminal run the following command
sudo docker run -p 3000:3000 -it manoharsingh1025/raws
Now Visit the PUBLICIP:3000/users/index to visit the home page of application. Wow ! The application is running smoothly and successfully on newly created EC2 instance. Now anyone can see our application.
.png)
When this happens the data of instance may be lost.
We can create an image of our existing EC2 instance so if this happens we can create another instance using this copy.
First Go to EC2 Instance and select the instance, for which you want to crate the image copy.
Actions -> Image and Templates -> Create Image
.png)
Now Enter the Image name and fill the description and click Create Image
.png)
Now our image is being built and we'll receive an email from AWS when it's done. Now We can create a new instance by replicating this image. So in case our EC2 stops working we can create new one in a jiffy.
.png)
The IP Address AWS assigns to the instance was selected from dynamic pool. So it changes automatically once the server restarts.
AWS managers their pool of Elastic IP addresses. Which can be assigned to Instances and other running services on AWS Cloud. I sometimes prefer calling Elastic IP as Static IP because it is not changing without admin changes.
- To Allocate new elastic Ip Address to our instance Go to Services -> Elastic IP -> and Click Allocate Elastic IP Address
.png)
- Leave the settings as default (Auto Pool of Amazon) and Click Allocate.
.png)
- Now Select Allocate Elastic IP address
.png)
- Select the reallocation check box for future instance allocation and Click Associate. Congratulations Our instance is running on Static/Elastic IP Address Now.