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.
  • 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

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named main.tf and 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)
    }
    
  3. Create a file named variables.tf to 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_file field as needed to point to your PEM file.

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 plan command 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 -out parameter allows you to specify an output file for the plan. Using the -out parameter 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 apply command above assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, simply call terraform apply without any parameters.

6. Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

    Key points:

    • The terraform plan command 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 -out parameter allows you to specify an output file for the plan. Using the -out parameter 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.
  2. 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

Next steps