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.
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.)
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.
This completes the creation of VPC, subnet, and security group.
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
Now let's check the URL in the EB console.
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.
#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 ...
RAILS_ENV=production bundle exec rake assets:precompile
config.assets.compile = true
thank you for your hard work. This completes the deployment.
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).
I will add some things to touch after deployment and how to deal with errors.
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)
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.
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