Configure an Azure Attestation provider using Terraform
This article shows example Terraform code for creating an Attestation provider on Azure.
In this article, you learn how to:
- Configure an Azure Attestatoin provider
1. Configure your environment
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:
- Policy Signing Certificate: A PEM file defines a set of trusted signing keys. As there are many scenarios in which to have a PEM file, this article assumes you have access to one. For example, you can download a PEM during the process of creating a virtual machine in the Azure portal.
2. Implement the Terraform code
Create a directory in which to test the sample Terraform code and make it the current directory.
Create a file named
main.tfand insert the following code:terraform { required_version = ">=0.12" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } } } provider "azurerm" { features {} } resource "azurerm_resource_group" "rg" { name = var.resource_group_name location = var.resource_group_location } resource "azurerm_attestation_provider" "corpAttestation" { name = var.attestation_provider_name resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location policy_signing_certificate_data = file(var.policy_file) }Create a file named
variables.tfto contain the project variables and insert the following code:variable "resource_group_name" { default = "myResourceGroup" } variable "resource_group_location" { default = "eastus" } variable "policy_file" { default = "~/.certs/cert.pem" } variable "attestation_provider_name" { default = "attestationprovider007" }Key points:
- Adjust the
policy_filefield as needed to point to your PEM file.
- Adjust the
3. Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure modules required to manage your Azure resources.
terraform init
4. Create a Terraform execution plan
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Key points:
- The
terraform plancommand creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-outparameter allows you to specify an output file for the plan. Using the-outparameter ensures that the plan you reviewed is exactly what is applied. - To read more about persisting execution plans and security, see the security warning section.
5. Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The
terraform applycommand above assumes you previously ranterraform plan -out main.tfplan. - If you specified a different filename for the
-outparameter, use that same filename in the call toterraform apply. - If you didn't use the
-outparameter, simply callterraform applywithout any parameters.
6. Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroyflag.terraform plan -destroy -out main.destroy.tfplanKey points:
- The
terraform plancommand creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-outparameter allows you to specify an output file for the plan. Using the-outparameter ensures that the plan you reviewed is exactly what is applied. - To read more about persisting execution plans and security, see the security warning section.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure