Giter Club home page Giter Club logo

iac-terraform's Introduction

Introduction

Build Status

Infrastructure as Code using Terraform - Module Development

Prerequisites

  • Azure CLI installed.

    Assumes CLI Version = azure-cli (2.0.75)

  • HashiCorp Terraform installed.

    export VER="0.12.18"
    wget https://releases.hashicorp.com/terraform/${VER}/terraform_${VER}_linux_amd64.zip
    unzip terraform_${VER}_linux_amd64.zip
    sudo mv terraform /usr/local/bin/

Setup Terraform Environment Variables

Generate Azure client id and secret.

After creating a Service Principal you MUST add API access for Windows Azure Active Directory and enable the following permissions

  • Read and write all applications
  • Sign in and read user profile
# Create a Service Principal
Subscription=$(az account show --query id -otsv)
az ad sp create-for-rbac --name "Terraform-Principal" --role="Owner" --scopes="/subscriptions/$Subscription"

# Expected Result
{
  "appId": "00000000-0000-0000-0000-000000000000",
  "displayName": "Terraform-Principal",
  "name": "http://Terraform-Principal",
  "password": "0000-0000-0000-0000-000000000000",
  "tenant": "00000000-0000-0000-0000-000000000000"
}

appId -> Client id. password -> Client secret. tenant -> Tenant id.

Export environment variables to configure the Azure Terraform provider.

A great tool to do this automatically with is direnv.

export ARM_SUBSCRIPTION_ID="SUBSCRIPTION_ID"
export ARM_TENANT_ID="TENANT_ID"
export ARM_CLIENT_ID="CLIENT_ID"
export ARM_CLIENT_SECRET="CLIENT_SECRET"
export TF_VAR_client_id=${ARM_CLIENT_ID}
export TF_VAR_client_secret=${ARM_CLIENT_SECRET}

TF_VAR_remote_state_account="STORAGE_ACCOUNT"
TF_VAR_remote_state_container="remote-state-container"

Setup Terraform Azure Backend State

Terraform requires state to be stored in some location. This state can be stored in Azure Blob if desired. (optional)

Execute the init-backend-state.sh script to create the required Azure Resources.

  • Resource Group (or existing) to hold resources
  • Storage Account (or existing) for blob storage
  • Storage Container (or existing) for state blob
  • Key Vault (or existing) for storage account key
ResourceGroup="tfstate"
StorageAccount="tfstatestorage"  # Must be unique
KeyVault="tfstate"
./scripts/init-backend-state.sh $ResourceGroup $StorageAccount $KeyVault

# Expected Result
Creating Terraform Backend Store
------------------------------------
Logging in and setting subscription...
Creating the Resource Group...
Creating the Storage Account...
Retrieving the Storage Account Key...
Creating the Storage Account Container...
Creating the Key Vault...
Adding Storage Key to Vault...
------------------------------------

TF_VAR_remote_state_account=tfstate
TF_VAR_remote_state_container=remote-state-container

Run the following command to initialize Terraform to store its state into Azure Storage:
terraform init \
  -backend-config="storage_account_name=tfstate" \
  -backend-config="container_name=remote-state-container" \
  -backend-config="access_key=$(az keyvault secret show --name tfstate-storage-key --vault-name tfstate --query value -o tsv)" \
  -backend-config="key=terraform-ref-architecture-tfstate"

Getting Started

  1. Testing with Task Runner (mage)
iac-terraform task runner.

Targets:
  all              A build step that runs all tests.
  check            Validate both Terraform code and Go code.
  clean            Remove temporary build and test files.
  lintGO           Lint check Go and fail if files are not not formatted properly.
  lintTF           Lint check Terraform and fail if files are not formatted properly.
  test             Execute Module Tests and fail if a test fails.
  testSimpleWeb    Execute Integration Tests for the simpleweb sample.
  testWebData      Execute Integration Tests for the webdata sample.
  1. Deploying Sample Templates
  • simpleweb - This template deploys a simple Web App with Containers
  • webdata - This template deploys a Web App with Containers with Cosmos DB Integration
# Change directory to Samples
cd samples/{sample}

# Initialize the Modules
terraform init

# Test the plan
terraform plan

# Apply the Plan
terraform apply

This repo was created for personal learning objectives. Concepts and terraform modules have been modified from the following projects.

iac-terraform's People

Contributors

danielscholl avatar

Watchers

James Cloos avatar

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.