|
| 1 | +--- |
| 2 | +title: Use Terraform as an infrastructure as code tool for Azure Developer CLI (azd) Preview |
| 3 | +description: How to use Terraform as an infrastructure as code tool for Azure Developer CLI (azd) Preview. |
| 4 | +author: hhunter-ms |
| 5 | +ms.author: hannahhunter |
| 6 | +ms.date: 09/14/2022 |
| 7 | +ms.service: azure-dev-cli |
| 8 | +ms.topic: conceptual |
| 9 | +ms.custom: devx-track-azdevcli |
| 10 | +--- |
| 11 | + |
| 12 | +# Use Terraform as an infrastructure as code tool for Azure Developer CLI (azd) Preview |
| 13 | + |
| 14 | +Azure Developer CLI (azd) Preview supports multiple infrastructure as code (IaC) providers, including: |
| 15 | +- [Bicep](/azure/azure-resource-manager/bicep/overview?tabs=bicep) |
| 16 | +- [Terraform](/azure/developer/terraform/overview) |
| 17 | + |
| 18 | +By default, `azd` assumes Bicep as the IaC provider. Refer to the [Comparing Terraform and Bicep](/azure/developer/terraform/comparing-terraform-and-bicep?tabs=comparing-bicep-terraform-integration-features) article for help deciding which IaC provider is best for your project. |
| 19 | + |
| 20 | +## Configure Terraform as the IaC provider |
| 21 | + |
| 22 | +1. If you haven't already, [install and configure Terraform](/azure/developer/terraform/quickstart-configure?source=recommendations). |
| 23 | +1. Open the `azure.yaml` file found in the root of your project and make sure you have the following lines to override the default, which is Bicep: |
| 24 | + |
| 25 | + ```yaml |
| 26 | + infra: |
| 27 | + provider: terraform |
| 28 | + ``` |
| 29 | +
|
| 30 | +1. Add all your `.tf` files to the `infra` directory found in the root of your project. |
| 31 | +1. Run `azd up`. |
| 32 | + |
| 33 | +> [!NOTE] |
| 34 | +> Check out these two azd templates with Terraform as IaC Provider: [Node.js and Terraform](https://github.com/Azure-Samples/todo-nodejs-mongo-terraform) and [Python and Terraform](https://github.com/Azure-Samples/todo-python-mongo-terraform). |
| 35 | + |
| 36 | +## azd pipeline config for Terraform |
| 37 | + |
| 38 | +Terraform stores state about your managed infrastructure and configuration. Because of this state file, you need to enable remote state **before** you run `azd pipeline config` to set up your deployment pipeline in GitHub. |
| 39 | + |
| 40 | +By default, `azd` assumes the use of local state file. If you ran `azd up` before enabling remote state, you need to run `azd down` and switch to remote state file. |
| 41 | + |
| 42 | +## Local vs remote state |
| 43 | + |
| 44 | +Terraform uses persisted [state](https://www.terraform.io/language/state) data to keep track of the resources it manages. |
| 45 | + |
| 46 | +Scenarios for enabling remote state: |
| 47 | + |
| 48 | +- To allow shared access to the state data, and allow multiple people work together on that collection of infrastructure resources |
| 49 | +- To avoid exposing sensitive information included in state file |
| 50 | +- To decrease the chance of inadvertent deletion because of storing state locally |
| 51 | + |
| 52 | +## Enable remote state |
| 53 | + |
| 54 | +1. Make sure you [configure a remote state storage account](/azure/developer/terraform/store-state-in-azure-storage). |
| 55 | +1. Add a new file called `provider.conf.json` in the `infra` folder. |
| 56 | + |
| 57 | + ```json |
| 58 | + { |
| 59 | + "storage_account_name": "${RS_STORAGE_ACCOUNT}", |
| 60 | + "container_name": "${RS_CONTAINER_NAME}", |
| 61 | + "key": "azd/azdremotetest.tfstate", |
| 62 | + "resource_group_name": "${RS_RESOURCE_GROUP}" |
| 63 | + } |
| 64 | + ``` |
| 65 | + |
| 66 | +1. Update `provider.tf` found in the `infra` folder to set the backend to be remote |
| 67 | + |
| 68 | + ```console |
| 69 | + # Configure the Azure Provider |
| 70 | + terraform { |
| 71 | + required_version = ">= 1.1.7, < 2.0.0" |
| 72 | + backend "azurerm" { |
| 73 | + } |
| 74 | + ``` |
| 75 | + |
| 76 | +1. Run `azd env set <key> <value>` to add configuration in the `.env` file. |
| 77 | +For example: |
| 78 | + |
| 79 | + ```bash |
| 80 | + azd env set RS_STORAGE_ACCOUNT your_storage_account_name |
| 81 | + azd env set RS_CONTAINER_NAME your_terraform_container_name |
| 82 | + azd env set RS_RESOURCE_GROUP your_storage_account_resource_group |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Run the next `azd` command as per your usual workflow. When remote state is detected, `azd` initializes Terraform with the configured backend configuration. |
| 86 | + |
| 87 | +1. To share the environment with teammates, make sure they run `azd env refresh -e <environmentName>` to refresh environment settings in the local system, and perform Step 4 to add configuration in the `.env` file. |
| 88 | + |
| 89 | +## See also |
| 90 | + |
| 91 | +- For more on remote state, see [store Terraform state in Azure Storage](/azure/developer/terraform/store-state-in-azure-storage). |
| 92 | +- Template: [Todo Application with Node.js and Terraform](https://github.com/Azure-Samples/todo-nodejs-mongo-terraform) |
| 93 | +- Template: [Todo Application with Python and Terraform](https://github.com/Azure-Samples/todo-python-mongo-terraform) |
| 94 | + |
| 95 | +## Next steps |
| 96 | + |
| 97 | +> [!div class="nextstepaction"] |
| 98 | +> [Azure Developer CLI FAQ](./faq.yml) |
0 commit comments