Skip to content

atilasantos/iac-terraform-remessa-online-poc

Repository files navigation

Deploying a Nginx service with ECS

Overview

This repository allows you to provision a cluster (ECS on AWS) with:

  • Nginx image
  • Elastic load balancer
  • Auto scaling group

Obs: This README.md file was written focused to Linux users, may other OS systems will be covered in the future 😅

Pre-requesites ✔️

  • Have an account on AWS.
  • While creating your IAM, don't forget to download the .csv file with the generated key/secret pair, you'll need them in the further steps.
  • Install Terraform CLI v0.13.0:
    wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip ;
    unzip terraform_0.13.0_linux_amd64.zip;
    chmod +x terraform;
    sudo mv terraform /usr/local/bin
    
  • Create a S3 Bucket service on AWS (fix the name if necessary in backend.tf)
  • Create an ECR registry and push the nginx image to that as the following article.

Getting started 🚀

  1. Install the aws cli, try execute aws --version right after the instalation, something like the snippet below must be displayed:

    aws-cli/2.0.56 Python/3.7.3 Linux/5.4.0-51-generic exe/x86_64.ubuntu.18

  2. Associate AWS credentials to our local aws CLI so terraform would be able to identify what credentials to use while provisioning IAC on aws provider.

  3. In order to set the right key-pair to get logged into the EC2 instance, clone this repository and inside the root folder, execute:

ssh-keygen -f mykey -N '' -q;
key=`cat mykey.pub`;
sed -i "6d" mainvars.tf;
sed -i "6i \ " mainvars.tf;
sed -i "6i \ \ default = \"$key\"" mainvars.tf;

Terraform files

This repository is responsible for provisioning the aimed infrastructure, however it uses a modularized structure provided by another repository also written by me, in this section i'll go through the files which belongs to this repo.

  • backend.tf: Define a remote versioned backend using S3 bucket service. The S3 bucket must be created before initializing terraform.
  • main.tf: Define how to create the cluster by providing the required variables from all the necessary modules.
  • mainvars.tf: Define 'global' variables used by terraform.
  • output.tf: Define which information will be displayed when the apply is successfully executed. In our example, will display the elb_dns_name so we can check wether nginx is up or not.
  • provider.tf: Define AWS as provisioner and us-east-2 as region to provision our infrastructure.

Other files

  • Jenkinsfile: Define the stages to be executed when a job is started in Jenkins.
  • validate_availability.sh: Script to check wheter nginx is up or not, once it is, it'll open a Google Chrome tab with the needed URL to display the service.

Provisioning

Follow the steps bellow to provision the infrastructure without hadaches:

  1. Inside the repository root folder, execute:
terraform init
  1. Once the modules, backend and plugins were initialized, execute:
terraform plan
  1. Check if all the necessary resources are planned to be provisioned and then execute:
terraform apply -auto-approve

Validating the ELB_DNS_NAME

Once the apply is executed, and a ELB_DNS_NAME was provided, execute:

./validate_availability.sh ${ELB_DNS_NAME}

Where ELB_DNS_NAME is the name provided as output in the apply.

The script will keep running until nginx instance is up, once this condition is reached, the script will open a Chrome tab with the nginx front-end. Bellow the output example:

 ELB not resolved.. 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   621  100   621    0     0   1621      0 --:--:-- --:--:-- --:--:--  1621
 ELB finally resolved! 
Opening in existing browser session.

Destroying the provisioned infrastructure

Once the validation is ok, execute:

terraform destroy -auto-approve

Any questions?

Feel free to contact me via [email protected]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published