Terraform
Introduction:
Terraform are cloud agnostic unlike CloudFormation which is cloud native with AWS and AzureResourceManager which is native with Azure cloud provider. Terraform helps to maintain single codebase to maintain infrastructure related code to provision resources across multiple cloud providers.
Setup:
To install Terraform, need to download the binary from “https://www.terraform.io/downloads.html” and set it in PATH variable. Run ‘sudo mv terraform /usr/local/bin’.
To verify the installation, please run the command terraform — version
IAC ( Infrastructure as Code )
- Adhoc Scripts
- Configuration Management Tools
- Service Template Tools
- Orchestration Tools
- Service Provisioning Tools
AdHoc Scripts
- General purpose programming language to write scripts
- Good for small tasks and it’s not good for Complicated Tasks
- Code can be messy, if we need to manage more servers, loadbalancers, here comes the advantage of IAC tools which provides features like IdemPotency and API support
“sudo apt-get update && sudo apt-get install apache2 -y && echo ‘<!doctype html><html><body><h1>Adhoc Script to install Apache server !</h1></body></html>’ | sudo tee /var/www/html/index.html”
Configuration Management Tools
- Chef, Puppet, Ansible and SaltStack — Designed to install and manage softwares on existing servers
- Enforce Coding Conventions and support for Idempotence
- Good for managing large number of remote servers
Server Templating Tools
- Docker, Packer and Vagrant
- Create an image of a Server that captures everything ( OS, Software and files ) and use the Configuration Management tool like Ansible to install the image into multiple servers
- Shift to Immutable infrastructure component
Orchestration Tools
- Kubernetes,Nomad,Marathon/Mesos
- Container Orchestration
Provisioning Tools
- Terraform, CloudFormation (Specific to AWS), OpenStack Heat
Add alt text
Approaches
- Declarative vs Imperative
- Immutable infrastructure vs Mutable Infrastructure
Terraform Files:
To start with Terraform, we need to specify the providers for Infrastructure. It can be any cloud service providers GCP or AWS.
The file can be named as provider.tf and it may have the following content.
provider.tf
Add alt text
variables.tf:
Variables can be declared inside variables.tf file and can be refereed in resources or main.tf file. It can be created using keyword variable. It can have the default value and description key to it.
Add alt text
Terraform Commands:
- terraform init ( To initialize the terraform )
- terraform plan ( To plan or preview the changes )
- terraform deploy ( To deploy or install the changes )
- terraform destroy ( To destroy the resources once finished )
- terraform fmt ( To do formatting )
- terraform refresh ( to refresh the state )
- terraform show ( to view the terraform.tfstate file )
Happy Learning !
Bharathy Poovalingam
#GCP #Terraform #Service Template #Learning