[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)

This is the way to build a production environment on AWS by turning a Ruby on Rails application created as a portfolio into a Docker container. The portfolio itself is here. [[Portfolio] Overview of the portfolio created during job change activities (Tec camp)] (https://qiita.com/sho_U/items/058e590325ee6833abb0)

I was suffering a lot, so I hope it helps someone.

title
1 Docker containerization of Rails application in local environment
2 Create a VPC on AWS. Create a public subnet
3 Create a private subnet
4 Create an EC2 instance
5 Create an RDS
6 Upload Docker container to AWS

Create an EC2 instance

What is EC2

It's called Elastic Compute Cloud, and it's roughly a virtual computer. By placing it on AWS, we will build a system on the cloud.

IMG_718A6999530D-1.jpeg

Place the created EC2 instance on the public subnet and place the app itself.

Creating an EC2 instance

Search for EC2 from the search window and enter the console. image.png

Click Launch Instance. image.png

This time, we will use the quick start "AMAZON Linux 2" as the instance type. (Roughly) What type of computer to use. This time, select the one whose OS is linux.

image.png

For the type, select t2.micro, which is a free usage tier. (Roughly) It's about how much specs to make.

image.png

→ Click "Next Step: Advanced Instance Settings".

item Contents Explanation (roughly)
Number of instances 1 Number of instances to launch
Purchase options no check If you select Spot Instance, you can use it cheaply (when it does not start all the time)
network fitO2_vpc Place it in the VPC created earlier.
Subnet fitO2_public_subnet_1a Place it in the public subnet created earlier.
Auto-assigned public IP Use subnet settings (disabled) To allocate a fixed IP later
Placement group no check Settings to speed up communication between multiple instances
Capacity booking None Avoid being unable to start an instance when the resource limit is exceeded (charged)
IAM roll None A service that sets permissions that can be linked to AWS resources
CPU options None Options for CPU performance
Shutdown operation Stop Operation at shutdown
Stop-Hibernate operation None Add pause operation to stop operation
Enable termination protection None Prevents accidental termination
monitoring None Make monitoring every 5 minutes feel like 1 minute.
Tenancy share Whether to occupy the hard disk
Elastic Inference None Streamline machine learning
Credit specifications None There is no limit when the specified amount of communication arrives

Network interface

Primary IP 10.0.10.10 Set a private IP address for the subnet.

Click "Next Step: Add Storage"

item Contents Explanation (roughly)
size 8
Volume type General purpose SSD
Delete together check
encryption No encryption

Click "Add Tag"

item Contents Explanation (roughly)
name fitO2_web
Volume type General purpose SSD
instance check
volume check

Click "Security Group Settings"

Select an existing security group. Apply the (fitO2_SG) created earlier.

Click "Confirm and Create"

Click "Launch"

Creating a key pair

Create a new key pair (if you have already created a key pair, you can create an existing one)

Set the key pair name and download.

A (roughly) key pair is a key used when connecting to ssh. Connect using the key pair downloaded when connecting with ssh, and if it matches the key generated on the AWS side, the connection is permitted.

"Create Instance"

image.png

Once created, name the instance for clarity. You can edit the name by clicking on the name.

image.png

Move the downloaded key pair to a suitable position. (This time ~/.ssh)

Create an Elastic IP (fixed) address.

If you stop the current instance, the IP address will change, so allocate a fixed IP.

From the EC2 dashboard, select Elastic IP.

Click Assign New Address.

Network border group: ap-northease-1 Public IPv4 Address Pool: Amazon's IPv4 Address Pool

Click "Assign".

Give the created Elastic IP address a name so that it is easy to understand.

Associate an Elastic IP (fixed) address with an instance.

Check the created IP address and select "Address Association" from the action.

Resource: Instance Instance: Select the created instance Private IP address: 10.0.10.10

Click "Associate"

Try and confirm the ssh connection.

After confirming that the instance status is running

 sudo ssh -i ~/.ssh/fitO2_key.pem [email protected] IP address

~/.ssh/fitO2_key.pem is the location where the private key is stored/the name of the private key The static IP is the one that is displayed as Elastic IP by selecting it from the instance list and clicking the description tag.

Are you sure you want to continue connecting (yes/no/[fingerprint])?

Type yes to enter

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

If it is displayed as above, the connection is successful.

In the figure, the connection is made with the image below.

IMG_D7961C5FA634-1.jpeg

The security group (fitO2_SG) created earlier is applied to EC2.

image.png

For inbound (incoming communication), in the case of ssh connection, port 22 is used and all access sources (0.0.0.0/0) are allowed, so it was possible to connect with ssh. is.

I created an EC2 instance and was able to make an ssh connection.

Since port 80 is also open for http communication, the instance you just created is in a state that allows the following communication.

IMG_378691F05F41-1.jpeg

You have now set up a security group like the one above for your public subnet EC2 instance.

Continue to next time (5) Create RDS

Recommended Posts

[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
Build an environment with Docker on AWS
Build Metabase with Docker on Lightsail and make it https with nginx
Deploy to heroku with Docker (Rails 6, MySQL)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Ruby on Rails] Let's build an environment on mac
[Rails] How to build an environment with Docker
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
Rails6 I tried to introduce Docker to an existing application
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build WordPress environment with Docker (Local) and AWS (Production)
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Maybe it works! Create an image with Docker and share it!
[Docker] Build a site on Hugo and publish it on GitHub
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Java: Start WAS with Docker and deploy your own application
How to deploy a Rails application on AWS (article summary)
Deploy a Docker application with Greengrass
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
How to make an application with ruby on rails (assuming that the environment has been built)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build environment with vue.js + rails + docker
How to build an environment with Docker, which is the minimum required to start a Rails application
(Ruby on Rails6) Creating a database and displaying it in a view
[Environment construction with Docker] Rails 6 & MySQL 8
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Deploy Rails on Docker to heroku
Steps to build a Ruby on Rails development environment with Vagrant
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
I built a rails environment with docker and mysql, but I got stuck
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
[Ruby on Rails] Creating an inquiry form
Building Rails 6 and PostgreSQL environment with Docker
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Create Rails 6 + MySQL environment with Docker compose
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
Deploy to Heroku [Ruby on Rails] Beginner
Ruby on Rails application new creation command
[Ruby on Rails] Model test with RSpec
Deploy your application with VPC + EC2 + Docker.
Build an Ultra96v2 development environment on Docker 1
How to build Rails 6 environment with Docker
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
Build a development environment to create Ruby on Jets + React apps with Docker
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)