Infrastructure as Code (IAC)
- The basic idea behind IAC is to write and execute the code to define, deploy, update and destroy your infrastructure
- We need to treat all aspects of operations as software (Event those aspects of setting up servers)
- Five Broad categories of IAC
- Adhoc Scripts:
- Examples: Shell Script, Powershell Script
- Configuration Management tools:
- Examples: Chef, Ansible, Puppet, Salt Stack
- Server Templating Tools:
- Examples: Packer, Vagrant, Docker
- Orchestration Tools:
- Examples: Kubernetes, Apache mesos etc
- Provisioning tools:
- Examples: Cloud formation, ARM Templates, Terraform
- Adhoc Scripts:
Terraform
- This is a tool for building, changing and versioning infrastructure safely and effeciently
- To express our Infrastructure as a code Terraform has its own Domain Specific Language (DSL)
- To understand how terraform works, we need to know two key terms
- Resource: This is piece of infrastructure which you want to create.
- Provider: This tells terraform where the infrastructure has to be built/created.
- Basic Terraform Workflow
- In Terraform, we configure Providers which specify where the infra has to be created
- We also configure resources, which specify what has to be created
- Terraform is tool which is written in a GO Language, which interacts with Provider api
- Terraform Language Refer Here
- Terraform supports many providers Refer Here
- Each Provider provides resources which we can use to write in a template
Hello-Terraform
- Lets create a very simple terraform template to create s3 bucket in AWS
- To do this we need to know
- How to create the infrastructure manually
- How to configure the provider to create resource (s3 bucket) in terraform configuration language
- In this case since we need to create in aws lets view the aws provider Refer Here and for documentation Refer Here
- Lets see the steps to create s3 bucket
- Login into aws console
- Navigate to s3
- Click on Create bucket
- Lets see how to create this from terraform
- create a new folder and create a file called as main.tf
- .tf is extension for terraform
- In Terraform Arguments are the inputs which you provide and Attributes are the outputs which terraform provides upon execution.
- Now lets try to setup provider and also provide authentication information Refer Here
- Creating Access Key and secret key in AWS
- official docs Refer Here
- for image demonstration Refer Here
- Lets write a terraform template to configure the resource and provider
- Now lets execute the terraform by executing following commands
terraform init
terraform validate --help
terraform validate .
terraform apply --help
terraform apply .
terraform destory .
- By default terraform is a single executable that is downloaded into your machine, if you want to execute terraform we need providers. When we execute
terraform init
your respective provider gets downloaded - Template developed in class
provider "aws" {
region = "ap-south-1"
access_key = "your access key"
secret_key = "your secret key"
}
resource "aws_s3_bucket" "mybucket" {
bucket = "qts3fromtffeb21"
}