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.
$azlogin
Make sure the subscription you want to work in is set to default. If it is not, you can run
$azaccountset--subscription<nameorid>
2. Register providers¶
Make sure the following providers are registered on the subscription.
$azproviderregister--namespaceMicrosoft.Web$azproviderregister--namespaceMicrosoft.KeyVault$azproviderregister--namespaceMicrosoft.Storage$azproviderregister--namespaceMicrosoft.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 prefixregion="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:
$terraforminit$terraformapply
- 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?".
$terraforminit-reconfigure
Enter"yes".
Now the state is stored in the Storage Account. - Check if
terraform.tfstate
is empty and remove it
Example¶
See theexamples directory for an example of a Terraform setup
Manual setup¶
See instructions on how toset up Azure manually.