Understanding and Practicing Terraform #3

Understanding and Practicing Terraform #3

In case you are here for the first time, this is a series about understanding the basis of terraform and how to put it to work for you. I defined terraform and wrote about the overview here and here.

As we know terraform belongs to hashiCorp. HashiCorp is a software company that provides open-source tools for developers to provision, run, and secure in the cloud.

Today, I am going to take us through one of the smallest tasks in terraform. Which is how to provision AWS EC2 instance using Terraform and how to spin up a virtual machine on Microsoft Azure.

For AWS, we will create an IAM user in the AWS console. This user will be given EC2 privilege. The credential will be used to configure aws cli on our terminal

we will create a main.tf file in our code editor or Ubuntu terminal, paste the below code in the file

terraform {

required_providers {

aws = {

source = "hashicorp/aws"

version = "~> 4.16"

} }

required_version = ">= 1.2.0" }

provider "aws" {

region = "us-west-2"

}

resource "aws_instance" "terraform-example" {

ami = "ami-830c94e3"

instance_type = "t2.micro"

tags = {

Name = "ExampleAppServerInstance"

} }

As I said in the previous article, we have to create a provider block first, this will enable Terraform to connect to the remote host.

In the code editor terminal, we will type terraform init, this helps to initialize the module of the resource that we have in our main.tf file. Then we will type terraform apply, this will provision the infrastructure.

For Azure, we will also create a main.tf file and paste the below code into the file.

provider   "azurerm"   { 
   version   =   "= 2.0.0" 
   features   {} 
 } 

 resource   "azurerm_resource_group"   "rg"   { 
   name   =   "my-first-terraform-rg" 
   location   =   "northeurope" 
 } 

 resource   "azurerm_virtual_network"   "new-app"   { 
   name   =   "new-app" 
   address_space   =   [ "10.0.0.0/16" ] 
   location   =   "northeurope" 
   resource_group_name   =   azurerm_resource_group.rg.name 
 } 

 resource   "azurerm_subnet"   "the-subnet"   { 
   name   =   "the-Subnet" 
   resource_group_name   =    azurerm_resource_group.rg.name 
   virtual_network_name   =   azurerm_virtual_network.myvnet.name 
   address_prefix   =   "10.0.1.0/24" 
 } 

 resource   "azurerm_public_ip"   "new-app-ipaddr"   { 
   name   =   "pip1" 
   location   =   "northeurope" 
   resource_group_name   =   azurerm_resource_group.rg.name 
   allocation_method   =   "Dynamic" 
   sku   =   "Basic" 
 } 

 resource   "azurerm_network_interface"   "new-app"   { 
   name   =   "new-app" 
   location   =   "northeurope" 
   resource_group_name   =   azurerm_resource_group.rg.name 

   ip_configuration   { 
     name   =   "ipconfig1" 
     subnet_id   =   azurerm_subnet.frontendsubnet.id 
     private_ip_address_allocation   =   "Dynamic" 
     public_ip_address_id   =   azurerm_public_ip.myvm 1 publicip.id 
   } 
 } 

 resource   "azurerm_windows_virtual_machine"   "example-app"   { 
   name                    =   "new-app"   
   location                =   "northeurope" 
   resource_group_name     =   azurerm_resource_group.rg.name 
   network_interface_ids   =   [ azurerm_network_interface.myvm 1 nic.id ] 
   size                    =   "Standard_B1s" 
   admin_username          =   "adminuser" 
   admin_password          =   "Password123!" 

   source_image_reference   { 
     publisher   =   "MicrosoftWindowsServer" 
     offer       =   "WindowsServer" 
     sku         =   "2019-Datacenter" 
     version     =   "latest" 
   } 

   os_disk   { 
     caching             =   "ReadWrite" 
     storage_account_type   =   "Standard_LRS" 
   } 
 }

In the terminal, we will type terraform init which will initialize terraform backend with the remote host. Then type terraform apply, this will provision our infrastructure in the remote host.