Terraform -- IAC-1

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 https://www.youtube.com/watch?v=Ff0DoAmpv6w

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 





You would usually have a Test step in between this which we are not using in our DevOps workflow. 


The code will be pushed to GitHub and the GitHub will trigger the build on Azure DevOps  .  Azure DevOps is going to build a Docker Image , Then we are going to push the docker image to the docker Hub .

And then it is going to use Terraform to provision and deploy that image on Azure , and it is going to do in conjunction with Azure DevOps


And this deployment is going to be completely automated . Only manual step for the day would be to put up to the GitHub. 

Today's agenda is highlighted below. 


Some Benefits of IaC - Infrastructure as a Code .

 




All of them are free except the exception of Azure. 

Lets get coding our Infrastructure .

Firstly we will open up the Visual Code editor tool : SDK 

We created the ,net project , Build the docker file and . All the code has been added to staging and committed to local branch now we need to push it to the GitHub .

Created a New Repository on Github and left it as a Public repository .


$ git push -u origin master

What is Terraform  ?


Terraform works with all of these cloud provide and even many more.

Lets talk about automatically provisioning resources in chosen cloud provider platform  . He says 

her is more familiarized with Azure, and he does not have to use Terraform - Instead he can use ARM Templates to do this . And suddenly if you want to use AWS or GCP , then you would suddenly have to use something specify to their tools to do the provisioning on their platform. In the interest of saving time , in this scenario Terraform will add value to by bring IaC using its common framework

To define IaC in terraform we use something called HCL - Hasi Corp Language or you can actually use JSON as well though the HCL approach is the preferred approach

How does Terraform Work .

It makes use of the respective Cloud providers API 

 




Installing Terraform  : Google it "Terraform Dowbload" be it for any OS and it comes as as single executable file. If you want to make use of that from your command line more Globally you then you will have to update your Environment Variable and your path

Once installed type in terraform and it will disaplay some commands on how to use  it . And the other things that you need to follow along is Azure CLI

Go to google - Type in Azure CLI


There are number of ways you can use it you can use if from your command line . But i recommend using from your CLI

Now lets create our IaC file

He has installed this Visual Studio Code Plugin  . We go ahead and create a file you can name it anything . We will create the file in our root  But I will call it main.tf

The first thing that we put in out file is the provider that we want to work with

The Provider Tag 


This is the HCL - it looks a little bit like JSON 

azurerm is on the Terraform -- to represent Azure provide this is the keyword given on the terraform language to refer to the Aure Provider. -- azurerm

The version that we need to use 


From the Azure website -- I downloaded the provider block

 # We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.46.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}


The next thing that you need to specify the resources that you want to use . Those who have used Auzre should be familiar with resource groups . Before you create Virtual machines or containers you need to put them into Resource group .

We use the resource block to create a resource

resource "specify the type of resource we want to create"

example from terraform 


How do I know what is "azurerm_resource_group" -- This is all available in the Terraform website you need to take it from there.


This name  "tf_test" is not the name of the resource that you are going to create it is only for the terraform file to know.

These two combination that we choose must be unique "azurerm_resource_group" "tf_test" and this can be used to refer in some other part of the code if necessary.

  • We do need to name our resource on Azure, so we will name it below code
  • You need to mention the location as well where you want to provision your resource 


 Now let see if we can apply this to Azure , The way you will do that is using terraform command line .

The first command is "init" --

The terraform init command is used to initialize a working directory containing Terraform configuration files

Terraform plan command 

The terraform plan command creates an execution plan. By default, creating a plan consists of: Reading the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.

terraform apply command

The terraform apply command executes the actions proposed in a Terraform plan.

 terraform destroy command

The terraform destroy command terminates resources managed by your Terraform project.

 


You need to logged into Azure to run this code for or it fails

Login into terraform

$ az login


To execute the plan -- terraform apply

it will challenges us here , asking "are you sure you want to apply it here"

Will got to Auzre portal 








Comments

Popular posts from this blog

Terraform : AWS : Terraform on AWS with SRE & IaC DevOps | Section 2 : Terraform basics

Terraform : AWS : Terraform on AWS | Section 4: Terraform Input Variables and Datasources

Terraform VPC - on AWS : Three tier architecture design