Wednesday, February 19, 2020

Getting started with terraform

Install terraform:
brew install terraform

Terraform - infrastructure as code.
- To build, manage and modify the infrastructure in a safe and repeatable way

Why terraform?
- to manage environments using configuration language
- here it uses HCL - HashiCorp Configuration Language


Infrastructure as a code?
- Instead of using UI to create resources, we use a file/files to mange infrastructure
- Resource: Any piece of infrastructure (Ex: Virtual machine, security group, network interface)'
- Provider - AWS, GCP, GitHub, Docker
- automates the creation of resources at the time of apply


Advantages of IAC:
- Easily repeatable
- easily readable
- operational certainty with "terraform plan"
- standardized environment builds
- quickly provisioned development environments
- disaster recovery

provider "aws" {
      access_key = "ACCESS_KEY",
      secret_key = "SECRET_KEY",
      region = "us-east-1"
}

# specific type of resource that we want to create, using ID - here example
resource "aws_instance" "example" {
       ami = "ami-8347957"
      instance_type = "t2.micro"
}



Terraform workflow to deploy:
1. scope - confirm what resources need to be created for a given project
2. author - create the configuration file in HCL based on the scoped parameters
3. initialize - run terraform init in the project directory with the configuration files, this will download the correct provider plug-ins for the project
4. plan and apply:
        - terraform plan -> to verify creation process
        - terraform apply -> to create real resources as well as state file that compares future changes in our configuration files to what actually exists in the development environment

create a terraform template (.tf)

provider "aws" {
 profile = "default"
 region = "us-east-1"
}

module "sns-topics" {
 source = "devops-workflow/sns-topics/aws"
 names = ["mt_teraform_example"]
 environment = "dev"

}


initialize the terraform template:

(base) HML-1612:terraform ladu$ terraform init


now apply the template

(base) HML-1612:terraform ladu$ terraform apply


now, to destroy the terraform template:


(base) HML-1612:terraform ladu$ terraform destroy

No comments:

Post a Comment