apim-landing-zone-acceleratorreference-implementationsAppGW-IAPIM-Functerraform at main · Azureapim-landing-zone-accelerator

rw-book-cover

Open in github.dev Open in a new github.dev tab Open in codespace

API Management - Terraform Implementation Guide

Table of Contents

Pre-requisites

  1. Terraform
  2. Azure CLI
  3. Azure Subscription

Overview

Folder Structure
.
└──reference-implementations/AppGW-IAPIM-Func/terraform
    ├── modules
    │   ├── backend
    │   ├── shared
    │   ├── networking
    │   ├── apim
    │   └── gateway
    ├── provider.tf
    ├── main.tf
    ├── variables.tf
    └── outputs.tf

Deployment Files
Modules

Each module has a module.md document that aims to give a quick overview of the module arguments, and terraform resources that are being leveraged when the module is being deployed. This document is automatically generated based upon the configuration found in the *.tf files in the module directory.

Naming convention

This project leverages the service-suffix module to standardize and construct the resource_suffix to enforce naming standards across deployments.

resource_suffix is constructed based on terraform input variables as follows:

resource_suffix = ${workloadName}-${environment}-${location}-${resource_suffix}

Examples:

ResourceGroupName = rg-${module}-${resource_suffix} [e.g. rg-shared-apidemo-dev-eastus-001]
APIMName = apim-${resource_suffix} [e.g. apim-apidemo-dev-eastus-001]
AppInsightsName = appi-${resource_suffix} [e.g. appi-apidemo-dev-eastus-001]

🚀 Getting started

Setting up your environment
Configure Terraform

If you haven't already done so, configure Terraform using one of the following options:

Configure Remote Storage Account

Before you use Azure Storage as a backend, you must create a storage account. Run the following commands or configuration to create an Azure storage account and container:

Powershell

$RESOURCE_GROUP_NAME='tfstate'
$STORAGE_ACCOUNT_NAME="tfstate$(Get-Random)"
$CONTAINER_NAME='tfstate'

# Create resource group
New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location eastus

# Create storage account
$storageAccount = New-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME -SkuName Standard_LRS -Location eastus -AllowBlobPublicAccess $true

# Create blob container
New-AzStorageContainer -Name $CONTAINER_NAME -Context $storageAccount.context -Permission blob

Alternatively, the Terraform Dependencies actions workflow can provision the Terraform remote state storage account and container. Customize the deployment through setting the following GITHUB_SECRETS for your own repository's action workflows:

For additional reading around remote state:

Deploy the API Management Landing Zone
Configure Terraform Remote State

To configure your Terraform deployment to use the newly provisioned storage account and container, edit the ./provider.tf file at lines 3-7 as below:

  backend "azurerm" {
    storage_account_name = "apimlztfbackend "
    container_name       = "terraform-state"
    key                  = "terraform.tfstate"
  }
Provide Parameters Required for Deployment

As you configured the backend remote state with your live Azure infrastructure resource values, you must also provide them for your deployment.

  1. Review the available variables with their descriptions and default values in the variables.tf file.
  2. Provide any custom values to the defined variables by creating a terraform.tfvars file in this directory (reference-implementations/AppGW-IAPIM-Func/terraform/terraform.tfvars)
Deploy
  1. Navigate to the Terraform directory reference-implementations/AppGW-IAPIM-Func/terraform

  2. Initialize Terraform to install required_providers specified within the backend.tf and to initialize the backend remote state

    • to run locally without the remote state, comment out the backend "azurerm" block in backend.tf (lines 8-13)
terraform init
  1. See the planned Terraform deployment and verify resource values
terraform plan
  1. Deploy
terraform apply