Terraform 04-July day-01 By G Kameswar Rao
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
Assignment – 1
List of Components of Terraform and explain each components with 1 image
1.Terraform Code (HCL)
Terraform code is expressed as a declarative code which contains declaration of resources, providers and necessary code for infrastructure deployment and management acting as single input source file for terraform executable.
2. Terraform Executable
Terraform Executable provides command line interface tool that will allow developer to run / perform all the terraform commands on terraform project/code.
Following are mostly used Terraform commands:
- terraform init : This command allows to add/download necessary providers in the terraform project based on the declaration in terraform code. It is necessary to execute “INIT” command in the beginning of any project.
- terraform validate: Validate command will check the necessary syntax and blocks are in place for successful execution by performing local scan.
- terraform plan: To ensure the configuration file will work as expected. And also allow developer to preview the necessary changes going to take place.
- terraform apply: It will execute the actions proposed in terraform plan.
- terraform destroy: It will destroy all the remote resources declared in terraform configuration file.
3. Terraform Provider
Providers allows to interact with different platforms. It acts as a mediator to provide connectivity from terraform code to the platform(AzureRM) to access its API. The azurerm provider helps terraform code on how to interact with azurerm API.
Below is the screenshot showing declaration of Provider for “azurerm” in terraform code.
4.Terraform State :
It is the terraform state file generated to capture the current state of the remote resources as a response to the create, update and destroy commands. This will allow terraform to know the current state of the resources define in the terraform code compared against the one in remote service. It contains all the necessary details like IP address, Secrets, and Keys from remote resources so it need to be properly guarded.
Assignment – 2
Write a terraform file which must create 1 azure resources group + 1 aws instance and 1 github repo. All the code must be into 1 file including providers.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.12.0"
}
aws = {
source = "hashicorp/aws"
version = "4.21.0"
}
github = {
source = "integrations/github"
version = "4.26.1"
}
}
}
provider "azurerm" {
features{}
subscription_id = "test-00000000-0000-0000-0000-000000000000"
client_id = "test-00000000-0000-0000-0000-000000000000"
client_secret = "test-afdfafsefgwrgwrwye"
tenant_id = "test-00000000-0000-0000-0000-000000000000"
}
provider "aws" {
access_key = "test-AKIAQ6GAIA5XIHHM2GJM"
secret_key = "test-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"
region = "eu-west-3"
}
provider "github" {
token = "testghp_Ii7t3DexYVxBGwqHOlgnLXOS2BTZ1s"
}
resource "azurerm_resource_group" "asgnmt2_rg" {
name = "assignment02_Rg"
location = "East US"
}
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}
resource "github_repository" "repo" {
name = "assignment02_repo"
description = "This repository created as a result by executing terraform script using github Provider"
visibility = "public"
}