Deploy to Ruby on Rails Elastic beanstalk (EB deploy)

Overview

Deploy an application created with Ruby on Rails Use Elastic Beanstalk (EB) to build an environment in the shortest possible time. It is divided into IAM authority change, environment construction, and EB deployment. We will focus on deploying quickly, so please be aware of this if you need strict control.

table of contents

IAM authority change Environment construction EB Deployment

Deploy to EB

  1. First, make the initial settings. eb init (eb init -i for resetting)

myapp


Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
・ ・ ・
22) af-south-1 : Africa (Cape Town)
(default is 3): 9 #Select Tokyo
Enter Application Name
(default is "myapp"): 
Application myapp has been created.

It appears you are using Ruby. Is this correct?
(Y/n): 
Select a platform branch.
1) Ruby 2.7 running on 64bit Amazon Linux 2
2) Ruby 2.6 running on 64bit Amazon Linux 2
3) Ruby 2.5 running on 64bit Amazon Linux 2
4) Puma with Ruby 2.6 running on 64bit Amazon Linux
5) Puma with Ruby 2.5 running on 64bit Amazon Linux
6) Puma with Ruby 2.4 running on 64bit Amazon Linux
7) Passenger with Ruby 2.6 running on 64bit Amazon Linux
8) Passenger with Ruby 2.5 running on 64bit Amazon Linux
9) Passenger with Ruby 2.4 running on 64bit Amazon Linux
・ ・ ・
17) Passenger with Ruby 2.0 running on 64bit Amazon Linux (Deprecated)
18) Passenger with Ruby 1.9.3 running on 64bit Amazon Linux (Deprecated)
(default is 1): 4 
#Since it is cloud9, it is Amazon Linux, but what is important is Puma and Ruby 2.Select 6 (this version)

Do you wish to continue with CodeCommit? (y/N) (default is n): <OK with Enter>
Do you want to set up SSH for your instances?
(Y/n): <OK with Enter>

Select a keypair.
1) cloudformation
2) [ Create new KeyPair ]
(default is 1): <You are free to create a key pair for this or use a pre-existing key.>

The initial setting is now OK. Let's create around EC2 necessary for deployment. (It would be helpful if you could do something good here as well.)

  1. Create the required VPC, subnet, security group 2-1. VPC creation Select VPC from the AWS console menu. Click Create VPC. Enter / select as follows Name tag: myapp-vpc (any name) IPv4 CIDR block: 10.0.0.0/16 (specify any network) Click Create.

2-2. Creating a subnet (Subnet) Select Subnet from the left menu. Create the first Subnet. Click the Create Subnet button. Enter as follows. Name tag: myapp-subnet-1a (any Subnet name) VPC: Select myapp-vpc. Availability Zone: ap-northeast-1a (choose one from the regions where you are creating your VPC) IPv4 CIDR block: 10.0.0.0/24 (specified by VPC CIDR range for each AZ) Create a second Subnet. Click the Create Subnet button again. Enter as follows. Name tag: myapp-subnet-1c (any Subnet name) VPC: Select myapp-vpc. Availability Zone: ap-northeast-1c (choose one from the regions where you are creating your VPC) IPv4 CIDR block: 10.0.1.0/24 (specified by VPC CIDR range for each AZ)

2-3. Creating an Internet Gateway (IGW) Select from the left menu. Click the Create IGW button. Name tag: myapp-gateway Click the create button. Select the gateway myapp-gateway from the list and select Attach to VPC from the action. VPC: Select myapp-vpc Click Attach. Click the route table from the left menu. Select the route table linked to myapp-vpc by referring to the VPC ID. Select Routes from the tabs at the bottom of the page and click Edit routes. Click Add route and enter Destination: 0.0.0.0/0 Select Internet Gateway from Target, then select myapp-gateway. Click Save routes to save.

2-4. Create Security Group (Create two for two regions and one for RDS.) Select a security group from the left menu Click Create security group and enter: Security group name: myapp-security-group Description: (Anything is fine) VPC: Select myapp-vpc. Click Create to create a security group. Select the security group you just created from the list and click Inbound Rules from the tab at the bottom of the screen. Click Edit rules. Click Add Rule and enter: Type: HTTP Description: HTTP Click Save rules to save the rules. Repeat this again to create another one.

The security group for RDS will allow access from two of the security groups you just created. Allow Inbound Rules to inbound (access) from security groups. Select the two security groups you mentioned earlier and add them. 3.jpg

This completes the creation of VPC, subnet, and security group.

  1. Deploy to EB It's a little more. Let's deploy. Go back to the terminal and use the following command to configure the deployment settings (including RDS creation and ALB settings).
eb create <EB environment name> --instance_type <EC2 specs> --database.engine <DB type> --database.username <DBusername> --elb-type application --vpc
#Example) eb create rails-app --instance_type t2.medium --database.engine mysql --database.username admin --elb-type application --vpc
Enter an RDS DB master password: 
Retype password to confirm: 

Enter the VPC ID: <ID of the created VPC>
Do you want to associate a public IP address? (Y/n): <Enter>
Enter a comma-separated list of Amazon EC2 subnets: <IDs of the two Subnets you created>
Enter a comma-separated list of Amazon ELB subnets: <IDs of the two Subnets you created>
Do you want the load balancer to be public? (Select no for internal) (Y/n): 
Enter a comma-separated list of Amazon VPC security groups:<IDs of the two security groups you created>

This will start building the deployment environment on the EB console screen. In addition, add the settings of the created RDS. (The parameters to be set can be confirmed on the RDS console → each RDS screen.)

eb setenv RDS_DB_NAME=◎◎ RDS_USERNAME=◎◎ RDS_PASSWORD=◎◎ RDS_HOSTNAME=◎◎ RDS_PORT=◎◎
Example) eb setenv RDS_DB_NAME=myapp RDS_USERNAME=admin RDS_PASSWORD=123456 RDS_HOSTNAME=database-1.cfwrsdfe64.ap-northeast-1.rds.amazonaws.com RDS_PORT=3306

Let's deploy.

eb deploy

Now let's check the URL in the EB console.

4.jpg

An unhandled lowlevel error occurred. The application logs may have details. Was displayed. This is because Ruby has not created or set a secret key for the production environment. So, let's set the production environment of Ruby here.

  1. Setting ruby environment variables and secret keys to output EB
#Secret key output
rails credential:edit
<Secret key>
#Reflect the settings in EB
eb setenv SECRET_KEY_BASE=<Secret key>

Because CSS precompile in the production environment is required ...

python


RAILS_ENV=production bundle exec rake assets:precompile

myapp/config/environments/production.rb


config.assets.compile = true
  1. Deploy again
eb deploy

thank you for your hard work. This completes the deployment.

Summary

This time, I deployed a Ruby application using AWS elastic beanstalk. I felt that it was a very good service to be able to easily create a redundant configuration including resources such as ALB and Autoscalling, and to be able to manage all resources in the elastic beanstalk environment. On the other hand, I found it inconvenient that RDS is not created by default and S3 remains when deleting the environment (due to bucket policy).

Other

I will add some things to touch after deployment and how to deal with errors.

  1. DB is created / migrated every time by initial setting Elastic Beanstalk> Environment> myapp-env> Settings RAILS_SKIP_ASSET_COMPILATION(false → true) RAILS_SKIP_MIGRATIONS(false → true)

  2. About the error If an error occurs, use the following command to find out the current error content, so take action. eb logs

For other eb commands, click here [https://qiita.com/yoshito410kam/items/712e96be87477aafdc89) Was helpful.

Reference article

I referred to the following article. Until you deploy your Rails application to Elastic Beanstalk https://qiita.com/[email protected]/items/c9dcdb9c648d7e8eae7f#rails%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E4%BD%9C%E6%88%90%E3%81%A8%E8%A8%AD%E5%AE%9A%E7%AD%89

How to install by specifying the Rails version https://qiita.com/tanakayo/items/7b85261924eca1a5a3d6

Set secret_token in production environment (rails) https://qiita.com/takusemba/items/2ad25d3d0a007757c194

What I got into when starting Rails 5 in production https://qiita.com/qqhann/items/7cd01f4b5cff4a31e053

aws EB CLI official ref https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb3-cmd-commands.html

Summary of commands used in Elastic Beanstalk https://qiita.com/yoshito410kam/items/712e96be87477aafdc89

Recommended Posts

Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
Deploy to Heroku [Ruby on Rails] Beginner
How to deploy jQuery on Rails
How to use Ruby on Rails
How to deploy Bootstrap on Rails
Deploy Rails on Docker to heroku
[Ruby on Rails] How to use CarrierWave
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
Deploy RAILS on EC2
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Deploy a Spring Boot application on Elastic Beanstalk
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
[Ruby On Rails] How to reset DB in Heroku
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
How to deploy on heroku
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Apply CSS to a specific View in Ruby on Rails
How to deploy a Rails application on AWS (article summary)
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
How to deploy Laravel on CentOS 7
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary