github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/examples/azure/terraform-azure-example/README.md (about)

     1  # Terraform Azure Example
     2  
     3  This folder contains a simple Terraform module that deploys resources in [Azure](https://azure.microsoft.com/) to demonstrate
     4  how you can use Terratest to write automated tests for your Azure Terraform code. This module deploys a [Virtual
     5  Machine](https://azure.microsoft.com/en-us/services/virtual-machines/) and gives that VM a `Name` tag with the value specified in the
     6  `vm_name` variable.
     7  
     8  Check out [test/azure/terraform_azure_example_test.go](/test/azure/terraform_azure_example_test.go) to see how you can write
     9  automated tests for this module.
    10  
    11  Note that the Virtual Machine in this module doesn't actually do anything; it just runs a Vanilla Ubuntu 16.04 image for
    12  demonstration purposes. For slightly more complicated, real-world examples of Terraform modules, see
    13  [terraform-http-example](/examples/terraform-http-example) and [terraform-ssh-example](/examples/terraform-ssh-example).
    14  
    15  **WARNING**: This module and the automated tests for it deploy real resources into your Azure account which can cost you
    16  money. The resources are all part of the [Azure Free Account](https://azure.microsoft.com/en-us/free/), so if you haven't used that up,
    17  it should be free, but you are completely responsible for all Azure charges.
    18  
    19  ## Running this module manually
    20  
    21  1. Sign up for [Azure](https://azure.microsoft.com/).
    22  1. Configure your Azure credentials using one of the [supported methods for Azure CLI
    23     tools](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest).
    24  1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`.
    25  1. Run `terraform init`.
    26  1. Run `terraform apply`.
    27  1. When you're done, run `terraform destroy`.
    28  
    29  ## Running automated tests against this module
    30  
    31  1. Sign up for [Azure](https://azure.microsoft.com/).
    32  1. Configure your Azure credentials using one of the [supported methods for Azure CLI
    33     tools](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest).
    34  1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`.
    35  1. [Review environment variables](#review-environment-variables).
    36  1. Install [Golang](https://golang.org/) and make sure this code is checked out into your `GOPATH`.
    37  1. `cd test`
    38  1. Make sure [the azure-sdk-for-go versions match](#check-go-dependencies) in [/test/go.mod](/test/go.mod) and in [test/azure/terraform_azure_example_test.go](/test/azure/terraform_azure_example_test.go).
    39  1. `go build terraform_azure_example_test.go`
    40  1. `go test -v -run TestTerraformAzureExample`
    41  
    42  ## Check Go Dependencies
    43  
    44  Check that the `github.com/Azure/azure-sdk-for-go` version in your generated `go.mod` for this test matches the version in the terratest [go.mod](https://github.com/gruntwork-io/terratest/blob/master/go.mod) file.
    45  
    46  > This was tested with **go1.14.1**.
    47  
    48  ### Check Azure-sdk-for-go version
    49  
    50  Let's make sure [go.mod](https://github.com/gruntwork-io/terratest/blob/master/go.mod) includes the appropriate [azure-sdk-for-go version](https://github.com/Azure/azure-sdk-for-go/releases/tag/v38.1.0):
    51  
    52  ```go
    53  require (
    54      ...
    55      github.com/Azure/azure-sdk-for-go v38.1.0+incompatible
    56      ...
    57  )
    58  ```
    59  
    60  We should check that [test/azure/terraform_azure_example_test.go](/test/azure/terraform_azure_example_test.go) includes the corresponding [azure-sdk-for-go package](https://github.com/Azure/azure-sdk-for-go/tree/master/services/compute/mgmt/2019-07-01/compute):
    61  
    62  ```go
    63  import (
    64      "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
    65      ...
    66  )
    67  ```
    68  
    69  If we make changes to either the **go.mod** or the **go test file**, we should make sure that the go build command works still.
    70  
    71  ```powershell
    72  go build terraform_azure_example_test.go
    73  ```
    74  
    75  ## Review Environment Variables
    76  
    77  As part of configuring terraform for Azure, we'll want to check that we have set the appropriate [credentials](https://docs.microsoft.com/en-us/azure/terraform/terraform-install-configure?toc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fterraform%2Ftoc.json&bc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fbread%2Ftoc.json#set-up-terraform-access-to-azure) and also that we set the [environment variables](https://docs.microsoft.com/en-us/azure/terraform/terraform-install-configure?toc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fterraform%2Ftoc.json&bc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fbread%2Ftoc.json#configure-terraform-environment-variables) on the testing host.
    78  
    79  ```bash
    80  export ARM_CLIENT_ID=your_app_id
    81  export ARM_CLIENT_SECRET=your_password
    82  export ARM_SUBSCRIPTION_ID=your_subscription_id
    83  export ARM_TENANT_ID=your_tenant_id
    84  ```
    85  
    86  Note, in a Windows environment, these should be set as **system environment variables**.  We can use a PowerShell console with administrative rights to update these environment variables:
    87  
    88  ```powershell
    89  [System.Environment]::SetEnvironmentVariable("ARM_CLIENT_ID",$your_app_id,[System.EnvironmentVariableTarget]::Machine)
    90  [System.Environment]::SetEnvironmentVariable("ARM_CLIENT_SECRET",$your_password,[System.EnvironmentVariableTarget]::Machine)
    91  [System.Environment]::SetEnvironmentVariable("ARM_SUBSCRIPTION_ID",$your_subscription_id,[System.EnvironmentVariableTarget]::Machine)
    92  [System.Environment]::SetEnvironmentVariable("ARM_TENANT_ID",$your_tenant_id,[System.EnvironmentVariableTarget]::Machine)
    93  ```