Code Monkey home page Code Monkey logo

terraform-azure's Introduction

Terraform Azure

Purpose

Test secure Azure provisioning using Terraform, utilising a Remote Backend and a Key Vault in Azure.

HashiCorp Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

Preparation

Before you can securely use Terraform with Azure, you will need to action the following steps:

Install Azure Dependencies / Log in to Azure

  1. Install the Azure PowerShell module.
  2. Ensure you are logged in to Azure (eg. Connect-AzAccount)

Configure Azure for Secure Terraform Access

  1. Clone this repo: git clone [email protected]:adamrushuk/terraform-azure.git
  2. Open scripts\ConfigureAzureForSecureTerraformAccess.ps1 and update the $adminUserDisplayName variable to match your Admin User Display Name (eg. Joe Bloggs).
  3. Run scripts\ConfigureAzureForSecureTerraformAccess.ps1

The ConfigureAzureForSecureTerraformAccess.ps1 script does the following:

  1. Creates an Azure Service Principle for Terraform.
  2. Creates a new Resource Group.
  3. Creates a new Storage Account.
  4. Creates a new Storage Container.
  5. Creates a new Key Vault.
  6. Configures Key Vault Access Policies.
  7. Creates Key Vault Secrets for these sensitive Terraform login details:
    • ARM_SUBSCRIPTION_ID
    • ARM_CLIENT_ID
    • ARM_CLIENT_SECRET
    • ARM_TENANT_ID
    • ARM_ACCESS_KEY

Load Azure Key Vault secrets into Terraform environment variables

Now that Azure has been configured for secure Terraform access, the Key Vault secrets need to be loaded into environment variables, but only for the current PowerShell session.

  1. Run scripts\LoadAzureTerraformSecretsToEnvVars.ps1

Install Terraform

Either download Terraform and add to your path , or use the Chocolatey method below:

  1. Install Chocolatey
  2. Install Terraform: choco install terraform

Provisioning

Now that Terraform is installed, the secure remote backend can be utilised whilst provisioning an Azure Resource Group and a Virtual Network:

  1. Navigate to the examples\remote-backend\ folder.
  2. Open main.tf and ensure you have updated the storage_account_name variable in the backend code block, to the new Storage Account Name created by the ConfigureAzureForSecureTerraformAccess.ps1 script.
  3. Initialise the Remote Backend and download plugins: terraform init
  4. Create an execution plan (see planned changes): terraform plan
  5. Apply the Terraform configuration: terraform apply
  6. Enter yes to confirm the planned actions.

Cleanup

You should now have a new Azure Resource Group (eg: backend-test-rg) with a Virtual Network (eg: test-vnet). To cleanup these Azure resources, you can also use Terraform to destroy what it created.

  1. If this is a new PowerShell session, you will have to run scripts\LoadAzureTerraformSecretsToEnvVars.ps1 again to reload the environment variables needed to Terraform to access Azure.
  2. Navigate to the examples\remote-backend\ folder.
  3. Remove the previously created Azure resources: terraform destroy
  4. Enter yes to confirm the planned actions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.