Terraform Tutorial: Docker & Azure

Le Vuong - Feb 1 - - Dev Community

Terraform with Docker

Create main.tf Terraform config file

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "tutorial"

  ports {
    internal = 80
    external = 8000
  }
}

Enter fullscreen mode Exit fullscreen mode
# Initialize the project, which downloads a plugin called a provider that lets Terraform interact with Docker.
terraform init

# Provision the NGINX server container with apply. Type 'yes' to confirm.
terraform apply

$ docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
425d5ee58619        e791337790a6              "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds       0.0.0.0:8000->80/tcp     tutorial

# To stop the container, run terraform destroy.
terraform destroy
Enter fullscreen mode Exit fullscreen mode

Terraform with Azure

Get Started - Azure

# Login to azure
az login

# set az subscription
az account set --subscription "35akss-subscription-id"

# create service principal
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<SUBSCRIPTION_ID>"

# Set your environment variables

export ARM_CLIENT_ID="<APPID_VALUE>"
export ARM_CLIENT_SECRET="<PASSWORD_VALUE>"
export ARM_SUBSCRIPTION_ID="<SUBSCRIPTION_ID>"
export ARM_TENANT_ID="<TENANT_VALUE>"

# Write configuration
mkdir learn-terraform-azure; cd learn-terraform-azure
Enter fullscreen mode Exit fullscreen mode
# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
  }

  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myTFResourceGroup"
  location = "westus2"
}
Enter fullscreen mode Exit fullscreen mode

Explanation

Terraform Block

The terraform {} block contains Terraform settings, including the required providers Terraform will use to provision your infrastructure.

Providers Block

The providerblock configures the specified provider, in this case azurerm. A provider is a plugin that Terraform uses to create and manage your resources.

Resource Block

Use resource blocks to define components of your infrastructure. A resource might be a physical component such as a server, or it can be a logical resource such as a Heroku application.
Resource blocks have two strings before the block: the resource type (azurerm_resource_group) and the resource name (rg). azurerm is the prefix to specify provider. azurerm_resource_group.rg is the full resource ID.

Format and validate the configuration

# Format terreform config file
terraform fmt

# validate the config
terraform validate

# apply config. Type 'yes' to confirm the changes
terraform apply

# Inspect your state
# When you apply your configuration, Terraform writes data into a file called terraform.tfstate.
# This file contains the IDs and properties of the resources Terraform created
# Your state file contains **sensitive values in plaintext**
terraform show

> resource "azurerm_resource_group" "rg" {
>     id       = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup"
>     location = "westus2"
>     name     = "myTFResourceGroup"
> }

Enter fullscreen mode Exit fullscreen mode

To review the information in your state file, use the state command.

$ terraform state list

$ terraform state

$ terraform destroy

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player