Provision Server

This article will show the Terraform workflow for provisioning the server for Rails on EC2.

Step 1 : Install Terraform

You can install Terraform using brew on Mac.

Install Terraform
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Terraform Version
terraform --version

These steps are tested on Terraform v1.8.1.

Step 2 : Format the Template

Go to the terraform folder in hivegrid.dev project. Format the Terraform template:

Format Template
terraform fmt .

Step 3 : Validate the Template

Run:

Validate Template
terraform validate .

Step 4 : Provision the Instance

Run:

Run Terraform
terraform apply -auto-approve

The output will have the values for the static public IP address, AWS account ID and Rails secret key name. The IP is needed to deploy using Capistrano and map the IP address to domain name in your domain registrar for Caddy SSL playbook to work. The account ID is useful if you have multiple AWS accounts and you want to make sure the resources are created in the expected AWS account. In the next step, we will use the Rails secret key to download the PEM file.

Step 5 : Download PEM File

In the terminal, run:

export ROR_SECRET_KEY='set-this-to-ouput-of-terraform-apply'

Go to javascript folder and run:

Download Key
node keyDownload.js

You must have nodejs installed on your machine. This will output the command to use for SSH connection to the server.

Step 6 : Connect to the Server

SSH to EC2 Instance
ssh -i rails-server.pem -p 2222 ubuntu@54.188.245.219

Replace the IP address with the IP of the static IP address from output of Terraform apply. You must run ssh command from the javascript directory. The pem file is stored inside that directory when you run the key downloader javascript program.