Skip to content

Commit 07a49ec

Browse files
authored
Merge pull request #2 from bronius/feature/provide-example
Adds example implementation to documentation
2 parents 3d35327 + c0d5b87 commit 07a49ec

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,51 @@ This is a terraform module for initializing a terraform state backend in Azure.
33

44
By default, it creates a resource group named `terraform-state`, a storage account with a unique name, and a container named "terraform-state",
55
<!-- BEGIN_TF_DOCS -->
6+
7+
## Example usage
8+
9+
A common pattern for using this is to create a folder within your terraform IaC project for setting up your environment, such as `/environments/{env_name}/setup`, containing a `main.tf` like:
10+
11+
```
12+
module "state_backend" {
13+
source = "github.com/tamu-edu/it-ae-tfmod-azure-state?ref=v0.1.2"
14+
15+
container_name = "tfstate"
16+
location = "southcentralus"
17+
resource_group_name = "your-project-tfstate-dev"
18+
# storage_account_name = "LeaveBlankToAutoGen"
19+
subscription_id = "f5358b4a-0a02-4485-8157-367fc107a27d"
20+
tenant_id = "68f381e3-46da-47b9-ba57-6f322b8f0da1"
21+
22+
remove_secrets_from_state = false
23+
}
24+
25+
output "container_name" {
26+
value = module.state_backend.container_name
27+
}
28+
output "resource_group_name" {
29+
value = module.state_backend.resource_group_name
30+
}
31+
output "storage_account_name" {
32+
value = module.state_backend.storage_account_name
33+
}
34+
```
35+
36+
To execute, first `az login` with an appropriately permissioned Azure account using the Azure CLI. Once logged in, run command `terraform init` within the new `terraform-state` folder. Then, run `terraform plan` to see what will be created. If satisfied with the results, run command `terraform apply`. This will create the appropriate Azure Blob Storage for holding state files for the main project. Azure Blobs are semaphore-locked from concurrent writes automatically. The state file for this remote state terraform script will be stored on the file system. Be sure to capture the results of the output (run `terraform output` to see it again) and copy it into your main Terraform stack variables. It is recommended to alter the name of the key to fit the granularity of separation of concerns that you require.
37+
38+
> [!CAUTION]
39+
> The terraform statefile with an Azure storage account resource will contain the initial storage account access keys. It is best practice to _disable_ access key access in favor of Entra ID authentication for your storage accounts. Do not commit the statefile until either the access keys are removed, rotated, or access keys disabled.
40+
41+
Consider adding the following to your parent terraform IaC project `.gitignore` file:
42+
```
43+
# .tfstate files
44+
*.tfstate
45+
*.tfstate.*
46+
!environments/*/setup/*.tfstate
47+
!environments/*/setup/*.tfstate.*
48+
```
49+
This will ignore the `.tfstate` file in your project, which will use remote storage, but retain the `.tfstate` for the remote tfstate infrastructure.
50+
651
## Requirements
752

853
| Name | Version |
@@ -34,11 +79,12 @@ No modules.
3479

3580
| Name | Description | Type | Default | Required |
3681
|------|-------------|------|---------|:--------:|
37-
| <a name="input_client_id"></a> [client\_id](#input\_client\_id) | The client ID to use for authenticating to Azure | `string` | `null` | no |
82+
| <a name="input_client_id"></a> [client\_id](#input\_client\_id) | The client ID to use for authenticating to Azure. Terraform authentication will overwrite this. | `string` | `null` | no |
3883
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | The name of the storage container to use for the Terraform state | `string` | `"terraform-state"` | no |
3984
| <a name="input_location"></a> [location](#input\_location) | The location to use for the Terraform state | `string` | `"centralus"` | no |
4085
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The name of the resource group to use for the Terraform state | `string` | `"terraform-state"` | no |
41-
| <a name="input_storage_account_name"></a> [storage\_account\_name](#input\_storage\_account\_name) | The name of the storage account to use for the Terraform state | `string` | `null` | no |
86+
| <a name="input_remove_secrets_from_state"></a> [remove\_secrets\_from\_state](#input\_remove\_secrets\_from\_state) | Whether to sanitize tfstate of access keys automatically created on created resources. Actual, assigned keys remain untouched on created assets. | `bool` | `true` | no |
87+
| <a name="input_storage_account_name"></a> [storage\_account\_name](#input\_storage\_account\_name) | The name of the storage account to use for the Terraform state. Leave blank to let Terraform manage a globally unique name to fit Azure constraints. | `string` | `null` | no |
4288
| <a name="input_subscription_id"></a> [subscription\_id](#input\_subscription\_id) | The subscription ID to use for the Terraform state | `string` | `null` | no |
4389
| <a name="input_tenant_id"></a> [tenant\_id](#input\_tenant\_id) | The tenant ID to use for the Terraform state | `string` | `null` | no |
4490

@@ -49,4 +95,7 @@ No modules.
4995
| <a name="output_container_name"></a> [container\_name](#output\_container\_name) | n/a |
5096
| <a name="output_resource_group_name"></a> [resource\_group\_name](#output\_resource\_group\_name) | n/a |
5197
| <a name="output_storage_account_name"></a> [storage\_account\_name](#output\_storage\_account\_name) | n/a |
52-
<!-- END_TF_DOCS -->
98+
99+
These output values will serve as your terraform IaC project inputs.
100+
101+
<!-- END_TF_DOCS -->

0 commit comments

Comments
 (0)