Let's update brew. We need to install aws-elasticbeanstalk. Let's create a new Rails 5.1 API only backend app. We don't need spring or action cable. We will use Postgresql as the database. The health controller renders status ok json. Let's start the server. We can hit the health endpoint using curl. The browser displays the JSON. We need to run eb init from the root folder of the Rails project. I will use the region 1, us-east-1. We can provide an application name. We are using Ruby. Let's use the option 1 for the platform version. We don't need CodeCommit. We can host a private repo for free on bitbucket. We need SSH access to our instance. We can provide a key pair name. It is better not to use generic name so that we know which keypair is used on which server. We need to provide a new passphrase. This generates the key pair. We see that the eb tool created some entries in the gitignore file. We can now create a production environment for our Rails app. This creates a zip file of our app and uploads the zip file to S3. It provides the details about the environment such as the application name, region, deployed version, environment id, platform, tier and so on. The environment is starting now. It automatically created a load balancer and security group for us.
We can check the status by using the eb status command. It is launching. Let's update the awcebcli tool. We get a permission denied error. We can use sudo to run the upgrade command. The environment is still in launching state. While we wait, let's see if we can set the secret key base environment variable. We cannot set any environment variable while the instance is still in launching state. We have to wait for some time. Now the instance is ready but is in red state. Let's set the secret key base environment variable. It is now updating the environment. After some time, it is in ready state but the health is yellow.
We can deploy our Rails app by using the eb deploy. Let's hit this URL in the browser. We get a 502 bad gateway error. The health check URL also gives the same error. Let's go to the AWS console. The health is in yellow state. We see the recent events and it's details. If we go to the causes screen, we see the warning. We can go to the logs tab and request the full logs. It will take some time to generate the log files. It will then appear as the download link. I looked at the eb-activity-log and found the PG connection bad error. The Rails app was not able to connect to Postgres database.
We need to add the configuration for the production database in the database.yml file. We can also run eb logs command to retrieve logs. It seems to hang and there is no feedback. So I downloaded the log file from the AWS console using the browser. We can create RDS instance using the AWS CLI command. This generates a small database instance with 1 GB storage for Postgres database. You need to provide the master user name and the master password.
We can get help for eb command by running eb. The eb open still gives the same error. I created a .ebextensions directory in the root folder of the Rails project and created a packages.config with the these contents. This will install the postgresql93-devel package on our EC2 instance. We need to add this file to the git and commit it. We can now deploy the changes. We did not change the code but provided a package that needs to be installed for the postgres database connection problem. It is in updating status. It is now in updating green status. We can see that it is still updating. It is now complete. We can now see the response of the health endpoint in the browser. We don't have anything to display in the home page.