Step 3. Prepare your Azure environment¶
In this step we will set up the following in Azure:
- Storage account for Terraform states
- Storage account for function app code packages
- Registered providers needed for various services
This section will demonstrate how to set up Azure using Terraform.
1. Prepare¶
Login to your Azure subscription through the CLI.
$ az login
Make sure the subscription you want to work in is set to default. If it is not, you can run
$ az account set --subscription <name or id>
2. Register providers¶
Make sure the following providers are registered on the subscription.
$ az provider register --namespace Microsoft.Web
$ az provider register --namespace Microsoft.KeyVault
$ az provider register --namespace Microsoft.Storage
$ az provider register --namespace Microsoft.Insights
3. Terraform configuration¶
Setup your Terraform configuration.
In main.tf:
terraform {
required_version = ">= 0.14.0"
}
In modules.tf:
module "shared_infra" {
source = "git@github.com:labd/terraform-azure-mach-shared.git"
name_prefix = "mach-shared-we" # replace 'mach' with your desired prefix
region = "westeurope"
dns_zone_name = "example.com"
certificate_access_object_ids = [
... # User, group or principle IDs that should have access to the resources
]
}
More info about the shared infra module.
4. Apply Terraform¶
- Run the following commands:
$ terraform init $ terraform apply - For a new Terraform setup, initially it will store the Terraform state locally and should be named
terraform.tfstate.
We'll move this state to the Storage Account that has been created by the shared infra module.
To do this, add a backend setting to project like belowterraform { required_version = ">= 0.14.0" backend "azurerm" { } } - Now run:
Terraform will detect that you're trying to move your state into Azure and ask; "Do you want to copy existing state to the new backend?".
$ terraform init -reconfigure
Enter "yes".
Now the state is stored in the Storage Account. - Check if
terraform.tfstateis empty and remove it
Example¶
See the examples directory for an example of a Terraform setup
Manual setup¶
See instructions on how to set up Azure manually.