[RAILS] [AWS] Health check of EC2 instance (target group) fails by all means in ALB. ..

The Rails app I'm currently developing is finally complete, and I've taken on the challenge of deploying it to AWS. (Using ECR x ECS x CircleCI, if you push from git to the master branch, CI will be built, and if the test passes, it will be automatically deployed.)

At that time, I was at a loss because the health check for the EC2 instance did not pass, so I finally learned programming by myself? I used MENTA, which is the trump card of.

Then, the problem I had been worried about for a week or two was solved in just 10 minutes, and it ended without a hitch. .. I couldn't find this solution after searching (probably a problem of how to find me), so I will leave it here as a memorandum so as not to waste as much time as I do!

** Mentor who consulted this time ** Rubycon @ Cloud (AWS) Engineer Rubicon's Twitter

Even though it was a one-time use, he kindly and politely taught me questions that are not related to the main subject. (Moreover, we had you serve for about 15 minutes.) If this article doesn't help, please feel free to contact us! I was also the one who would definitely like to use it if I was in trouble: smile:

What is a health check in the first place?

A health check is a way to check with a service on a particular server to see if it can do its job successfully. The load balancer asks each server this question on a regular basis to determine which servers are safe to forward traffic.

[Excerpt from Amazon's official page](https://aws.amazon.com/jp/builders-library/implementing-health-checks/#:~:text=%E3%83%98%E3%83%AB % E3% 82% B9% E3% 83% 81% E3% 82% A7% E3% 83% 83% E3% 82% AF% E3% 81% AF% E3% 80% 81% E7% 89% B9% E5 % AE% 9A% E3% 81% AE,% E3% 82% B5% E3% 83% BC% E3% 83% 90% E3% 83% BC% E3% 82% 92% E5% 88% A4% E6% 96% AD% E3% 81% 97% E3% 81% BE% E3% 81% 99% E3% 80% 82)

The image is like ** periodically sending a request from ALB (load balancer) to the target group (EC2 instance) and checking the returned HTTP status **.

The task was forcibly terminated due to the failure of this health check, and a new task was started, which was repeated at a pace of about 20 minutes. (I got the error `` `health checks failed with these codes: [301] ```) As a result, the page was displayed and a 50x error was returned depending on the timing of access.

Solutions

In conclusion, if I allowed the HTTP status code 301 in the target group settings, it was cured.

As mentioned above, `` `health checks failed with these codes: [301] ``` means that the page has been redirected. The reason for being redirected is that the Rails app had the following settings.

production.rb


config.force_ssl = true

Since it was SSL-enabled, I had set it to redirect from http to https. Therefore, it is natural that 301 is returned with the HTTP status code when performing a health check.

However, since the target group setting only allows 200, it becomes unhealthy and the task ends.

Setting method

First, select EC2 from the services and click on the target group. Then, the following screen will appear. Click Edit on the Group details tab there. スクリーンショット 2020-11-08 222221.png

Then click on the red circle. スクリーンショット 2020-11-08 222406.png

I think that 200 is written in Success codes, so add 301 here to allow it. (Please separate them with commas!) After writing, save the changes and you're done. スクリーンショット 2020-11-08 222449.png

This should turn healthy in a few minutes! (If the settings such as ALB and security group are correct)

スクリーンショット 2020-11-08 222057.png

If the health check still fails, there may be a problem with the security group, ALB settings, health check grace period, etc., so please review it!

If that doesn't work, please contact Rubycon @ Cloud (AWS) Engineer: thumbs up:

Thank you for reading to the end!

We output what we have learned every day! !! If you have any suggestions, I would appreciate it if you could comment.

Recommended Posts

[AWS] Health check of EC2 instance (target group) fails by all means in ALB. ..
Install docker-compose on a Graviton 2 instance of AWS EC2