Multicloud Infrastructure as Code with Terraform: Setting up AWS and Azure Providers for Seamless Cloud Management – A Hands-On Guide

How to install azure cli on amazon ec2.

  1. Launch ec2 instance.
  • Login to linux machine.
sudo su               // become root user
  • Copy these below line to your CLI

sudo rpm - import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'

yum install azure-cli -y

Install terraform

sudo yum install -y yum-utils

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

sudo yum -y install terraform
mkdir newfolder

Create Manifest File

vi main.tf

# Configure the Microsoft Azure Provider

provider "azurerm" {

  features {}

  subscription_id=””

}

for subscription id go to azure.

Terraform init

If you face this issue then run

Please run ‘az login’ to setup account

az login

Open –  https://microsoft.com/devicelogin

Give code which is displayed in cli

Now setup –

az account set -s subscription_id

Create resource group

resource "azurerm_resource_group" "example" {
  name     = "example"
  location = "east us"
}
Terraform validate

Create azure linux vm

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }

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

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
}

Where my ssh key store

sudo ls ~/.ssh/

Run terraform validate.

If you got success then start plan

Terraform plan

Entier plan will display it take 2 min.

Setup AWS Instance

Now create aws ec2 instance.

Add this line to main.tf.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "ap-south-1"
}

Now configure AWS by running below commands

 aws configure
Access_key – your_access_key
Secret_key – your_secret_key
Terraform init
Terraform plan

Add below line to manifest file

resource "aws_instance" "web" {
  ami           = ami-0103f211a154d64a6
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}
Select AMI carefull by going to your ec2 dashboard engine.

Terraform validate.

Terraform plan

Terraform apply --auto-approve

Concluded file

provider "azurerm" {

  features {}

  subscription_id= ""

}

provider "aws" {

  region = "us-east-2"

}

resource "azurerm_resource_group" "example" {

  name     = "example"

  location = "east us"

}

resource "aws_instance" "web" {

  ami           = "ami-0103f211a154d64a6"

  instance_type = "t2.micro"

  tags = {

    Name = "HelloWorld"

  }

}

resource "azurerm_virtual_network" "example" {

  name                = "example-network"

  address_space       = ["10.0.0.0/16"]

  location            = azurerm_resource_group.example.location

  resource_group_name = azurerm_resource_group.example.name

}

resource "azurerm_subnet" "example" {

  name                 = "internal"

  resource_group_name  = azurerm_resource_group.example.name

  virtual_network_name = azurerm_virtual_network.example.name

  address_prefixes     = ["10.0.2.0/24"]

}

resource "azurerm_network_interface" "example" {

  name                = "example-nic"

  location            = azurerm_resource_group.example.location

  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {

    name                          = "internal"

    subnet_id                     = azurerm_subnet.example.id

    private_ip_address_allocation = "Dynamic"

  }

}

resource "azurerm_linux_virtual_machine" "example" {

  name                = "example-machine"

  resource_group_name = azurerm_resource_group.example.name

  location            = azurerm_resource_group.example.location

  size                = "Standard_D2_v2"

  admin_username      = "adminuser"

  network_interface_ids = [

    azurerm_network_interface.example.id,

  ]

  admin_ssh_key {

    username   = "adminuser"

    public_key = file("~/.ssh/id_rsa.pub")

  }

  os_disk {

    caching              = "ReadWrite"

    storage_account_type = "Standard_LRS"

  }

  source_image_reference {

    publisher = "Canonical"

    offer     = "UbuntuServer"

    sku       = "16.04-LTS"

    version   = "latest"

  }

}

Hope you successfully done multicloud setup.
Waiting for your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *