Terraform-learning

This project was developed during the learning process of the terraform by Joshua-Jebaraj

Terraform Digital Ocean GCP Ansible Docker

Why this project ? 🤔

When I started to learning Terraform I want to create simple projects around it but when I google it I couldn't able to find one what I am ;looking for ,so I created one.

Below are the List of projects

Some of the projects ideas were taken from the Michael Crilly Youtube videos on Terraform Tutorial - The Best Project Ideas (Terraform How To and I tweaked little bit

Contribution

If you want to contribute to the project here are a few ways that you can contribute

  • Sharing the word with your friends about this Project
  • Create more task for the project
  • Improve the existing solution and the Documentation

Task-1

  • For this Task you have to create the simple droplet in the Digital-Ocean using Terraform
  • Install the Nginx on the created droplet
  • Replace the default Nginx-website with the new-website

🏆 Bonus Task

  • Try to automate the last two steps using the configuration management tool like Ansible

Solution

Task-2

  • For this Task you have to create two droplets in the Digital-Ocean using Terraform
  • Install the Nginx on the created droplet
  • Replace the default nginx-website with the new-website
  • Add the Load balancer in front of the created droplets

The Final Architecture should be like the below diagram

drawing

🏆 Bonus Task

  • Map the IP address of the Load balancer to the Custom Domain

Solution

Task-3

Note this task is a little bit more focused on ansible rather than terraform

  • For this Task You have to create the Simple droplet in the Digital-Ocean using terraform
  • Install and Setup Gitea using the configuration management

🏆 Bonus Task

  • Install the Gitea using the custom ansible role

Solution

Task-4

  • Create the Custom VPC using Terraform
  • Add the firewall rules to allow traffic at port-80 at the above-created VPC
  • Create the compute instance in the Google-cloud at inside the created VPC
  • Install the Nginx on the created instance

The final Architecture should be like the below diagram

drawing

I have used GCP to do this task you can use AWS,Azure to achieve the same output

Solution

Task-5

Extending Terraform

This is an Extra-mile task

Terraform has a lot of official and community-based modules and providers already exist, but there are few chances where you may want to extend certain features for the resources or create the resources for the provider that doesn't exist.

  • Create new custom resources to create docker images using the Null provider and python

Note In the production environment You should use official or well-tested resources but for the learning sake we are going to create the custom resources

Solution-1

The code for this task can be found here

Pre-Task

First create the private and public key by typing the below command in the terminal

ssh-keygen -m PEM -f do_key -N ""

Add the relevant permission to the created key using the below command

chmod 600  do_key

Create the digital ocean api key If you don't have one, create it following this tutorial

Note : Make Sure you kept those keys safe and don't put share it online

Create the new file in the same directory with the name terraform.tfvars

Add the below content to the created file Replace token-value with the api-key that you have created in the previous step

do_token = "<token-value>"

Task

Now type the below command in the terminal in order to create the resources terraform

terraform init
terraform plan -out state.plan
terraform apply "state.plan"

Once everything is done you will see the IP address of the droplet as the terraform output. Navigate to the ip address using the browser

On hitting the ip-address you will see

Welcome to <IP-ADDRESS>

Post-task

Once everything is done make sure to tear down the infrastructure that we have created during the task using the below command

terraform destroy -auto-approve

References

  1. https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management
  2. https://www.digitalocean.com/community/tutorials/how-to-deploy-a-static-html-website-with-ansible-on-ubuntu-20-04-nginx

Solution-2

The code for this task can be found here

Pre-Task

First create the private and public key by typing the below command in the terminal

ssh-keygen -m PEM -f do_key -N ""

Add the relevant permission to the created key using the below command

chmod 600  do_key

Create the digital ocean api key If you don't have one, create it following this tutorial

Note : Make Sure you kept those keys safe and don't put share it online

Create the new file in the same directory with the name terraform.tfvars

Add the below content to the created file Replace token-value with the api-key that you have created in the previous step

do_token = "<token-value>"

Task

Now type the below command in the terminal in order to create the resources using terraform

terraform init
terraform plan -out state.plan
terraform apply "state.plan"

Once everything is done you will see the Ip address of the loadbalancer as the terraform output. Navigate to the Ip address using the browser

On Hitting the IP address of the Loadbalancer

Welcome to <SERVER1-IP>

If you refresh again for sometimes you will see

Welcome to <SERVER2-IP>

Post-task

Once everything is done make sure to tear down the Infrastructure that we have created during the task using the below command

terraform destroy -auto-approve

References

  1. https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management
  2. https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/loadbalancer

Solution-3

The code for this task can be found here

Pre-Task

First create the private and public key by typing the below command in the terminal

ssh-keygen -m PEM -f do_key -N ""

Add the relevant permission to the created key using the below command

chmod 600  do_key
  • Create the digital ocean api key If you don't have one, create it following this tutorial

Note : Make Sure you kept those keys safe and don't put share it online

Create the new file in the same directory with the name terraform.tfvars

Add the below content to the created file Replace token-value with the api-key that you have created in the previous step

do_token = "<token-value>"

Now Install the ansible-role by typing the below command in the terminal

ansible-galaxy install joshuajebaraj.ansible_role_gitea

Task

Now type the below command in the terminal in order to create the resources terraform

terraform init
terraform plan -out state.plan
terraform apply "state.plan"

Once everything is done you will see the ip address of the droplet as the terraform output. Navigate to the ip address using the browser

On hitting the ip-address you will see the Gitea Initial Welcome Page

Post-task

Once everything is done make sure to tear down the Infrastructure that we have created during the task using the below command

terraform destroy -auto-approve

References

  1. https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management
  2. https://galaxy.ansible.com/joshuajebaraj/ansible_role_gitea

Solution-4

The code for this task can be found here

Pre-Task

Setup the Terraform and GCP following this Guide

Create the new file in the same directory with the name terraform.tfvars

Add the below content to the created file Replace project-name with your google cloud project name

region = "us-central1"
project = "<project-name>"
zone = "us-central1-a"

Task

Now type the below command in the terminal in order to create the resources terraform

terraform init
terraform plan -out state.plan
terraform apply "state.plan"

Once everything is done you will see the IP address of the compute instance as the terraform output. Navigate to the IP address using the browser

On hitting the IP-address you will see the Nginx Welcome Page

Post-task

Once everything is done make sure to tear down the Infrastructure that we have created during the task using the below command

terraform destroy -auto-approve

References

  1. https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management
  2. https://galaxy.ansible.com/joshuajebaraj/ansible_role_gitea

Solution-5

The code for this task can be found here

For this task, you need to have Docker installed on your system If you don't have docker Install it by following this guide

Task

Now type the below command in the terminal in order to create the resources terraform

terraform init
terraform apply  -var name="alpine-git" -var tag="0.1" --auto-approve

create

Tear down

To remove the image that we have created using the terraform type the below command in the terminal

terraform destroy -var name="alpine-git" -var tag="0.1" --auto-approve

Delete

Reference

  1. https://dev.to/jmarhee/upgrading-terraform-destroy-time-provisioners-50p7