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

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 a VPC on AWS.

What is VPC (roughly)

It is called Virtual Private Cloud and is a virtual network created in AWS.

First, create a VPC and subbet with the following configuration.

A ** subnet ** is a further segmented network within your VPC. The app itself on the ** public subnet **. Place the database on a ** private subnet **. The public subnet is set to ** allow access from the outside **. Private subnets do not allow ** external access, only public subnets **. (The database does not need to be accessed from the outside, so to improve security)

In addition, there is no setting itself of "public subnet" and "private subnet", and the above network access restrictions will be set in the security group that will be set later.

IMG_A66BC051DFB8-1.jpeg

Since the cider notation of the private IP address of vpc is 16 bits, the 16 bits from the beginning, that is, 10.0 is the network part.

Both the public and private subnets have private addresses starting at 10.0., So they are in the network inside your VPC.

Also, since the cider notation of the public subnet is/24, the network part is for 24 bits, that is, up to 10.0.10. Since the cider notation of the private subnet is also 24, 24 bits, that is, up to 10.0.20. Is the network part, Public subnets and private subnets have different network spaces.

IMG_BD8661923AD3-1.jpeg

Creating a VPC

image.png

Log in to AWS, enter the VPC dashboard, and create a VPC.

Name: Any VPC name

IPv4 CIDR block: Private IP address for VPC (10.0.0.0/16 this time)

Click "Create VPC"

Create public subnet

Enter your VPC from the AWS console into your VPC, select your subnet and create a subnet.

Select the VPC you created earlier for your VPC ID.

Availability Zone: This time ap-northease-1a

Name: fitO2_public_subnet_1a

IPv4 CIDR block: 10.0.10.0/24

Click "Create Subnet"

Creating public subnet routing

Create an Internet Gateway (IGW) and attach it to your VPC.

IMG_174B7A21241B-1.jpeg

Roughly speaking, an IGW is like a window that connects a VPC to the Internet. VPC communication with the Internet through IGW. Attaching is the work of associating the created IGW with the VPC.

From the VPC console, select "Internet Gateway" and click "Create Internet Gateway". Decide a name and create it.

Select the IGW created from the list and click "Attach" to the VPC from the action on the upper right

image.png

Select the VPC you created earlier and click "Attach Internet Gateway"

image.png

Create a route table and link it to the public subnet

Create a routing table for the public subnet.

IMG_92E66ED01F79-1.jpeg

Of the access that came to the public subnet

Those destined for 10.0.0.0/16 will be sent to the VPC. Those destined for 10.0.10.0/24 will be sent to the public subnet (self). 0.0.0.0/0 (all destinations other than the above) will be sent to IGW.

From the VPC console, select Route Table, click Create Route Table, name it, and select the VPC you just created for your VPC.

image.png

If you select the route you created from the list and check the "Routes" tab at the bottom, You can confirm that the destination (target) of the destination "10.0.0.0/16" is VPC (local). Since the VPC to which you belong is always required for the route table, the VPC you selected earlier will be linked.

Click the Subnet Association tab, then click Edit Subnet Association.

image.png

Select a public subnet and click Save.

image.png

Create a route to IGW.

Select the "Routes tab" and click "Edit Route". image.png

Select "Add Route" and then "0.0.0.0/0". Select the IGW you just created.

Click "Save Route".

image.png

You have now created a routing table associated with your public subnet.

The following is a summary of how to make it.

IMG_AE0A928B8E61-1.jpeg

It's complicated because each method is different, but what we are doing is where to distribute the communication to the destination IP address.

Create a security group for the public subnet.

Click a security group on your EC2 dashboard, then click Create Security Group.

Click Add Rule.

image.png

Security group name: fitO2_SG VPC: The VPC you just created Inbound: Created as shown above

You have now created a security group that opens SSH communication (port 22) and http communication (port 80) to all access sources. Later, I plan to apply this security group to the EC2 instance I create. (The security group will be explained in a little more detail later.)

Continue to next time (3) Create Private Subnet (https://qiita.com/sho_U/items/95c25df53a7e1fd20efd)

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 (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[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 ~