Home   devops  

Terraform examples for aws, gcp, Azure

Below are simple examples of using Terraform to create resources in AWS, Azure, and GCP. These examples demonstrate creating a virtual machine (EC2 instance in AWS, VM in Azure, and Compute Engine instance in GCP).

AWS Example

1. Create a directory for your Terraform configuration files:

mkdir terraform-aws
cd terraform-aws

2. Create a main.tf file with the following content:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

3. Initialize Terraform and apply the configuration:

terraform init
terraform apply

Azure Example

1. Create a directory for your Terraform configuration files:

mkdir terraform-azure
cd terraform-azure

2. Create a main.tf file with the following content:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West US"
}

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                 = "example-subnet"
  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_B1s"
  admin_username      = "adminuser"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = "<your-public-ssh-key>"
  }

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

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

3. Initialize Terraform and apply the configuration:

terraform init
terraform apply

GCP Example

1. Create a directory for your Terraform configuration files:

mkdir terraform-gcp
cd terraform-gcp

2. Create a main.tf file with the following content:

provider "google" {
  project = "<your-gcp-project-id>"
  region  = "us-central1"
  zone    = "us-central1-a"
}

resource "google_compute_instance" "example" {
  name         = "example-instance"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"
    access_config {
    }
  }

  tags = ["web", "dev"]
}

3. Initialize Terraform and apply the configuration:

terraform init
terraform apply
Published on: Jul 08, 2024, 05:00 AM  
 

Comments

Add your comment