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.
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
- 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
- Container Orchestration
- Terraform, CloudFormation (Specific to AWS), OpenStack Heat
Add alt text
- Declarative vs Imperative
- Immutable infrastructure vs Mutable Infrastructure
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.
Add alt text
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 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 !
#GCP #Terraform #Service Template #Learning