Skip to content

Commit

Permalink
Modularise Terraform Vars Key Vault (#335)
Browse files Browse the repository at this point in the history
* Added Event Hub deployment option
* Modularised TF Vars Key Vault
  • Loading branch information
DrizzlyOwl authored Mar 13, 2023
1 parent 7a9dde8 commit 1bfb434
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 72 deletions.
16 changes: 5 additions & 11 deletions terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,18 @@ If everything looks good, answer `yes` and wait for the new infrastructure to be

## Providers

| Name | Version |
|------|---------|
| <a name="provider_azuread"></a> [azuread](#provider\_azuread) | 2.36.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.46.0 |
No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_azure_container_apps_hosting"></a> [azure\_container\_apps\_hosting](#module\_azure\_container\_apps\_hosting) | github.com/DFE-Digital/terraform-azurerm-container-apps-hosting | v0.14.3 |
| <a name="module_azure_container_apps_hosting"></a> [azure\_container\_apps\_hosting](#module\_azure\_container\_apps\_hosting) | github.com/DFE-Digital/terraform-azurerm-container-apps-hosting | v0.14.6 |
| <a name="module_azurerm_key_vault"></a> [azurerm\_key\_vault](#module\_azurerm\_key\_vault) | github.com/DFE-Digital/terraform-azurerm-key-vault-tfvars | v0.1.1 |

## Resources

| Name | Type |
|------|------|
| [azurerm_key_vault.tfvars](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault) | resource |
| [azurerm_key_vault_secret.tfvars](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) | resource |
| [azuread_user.key_vault_access](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/data-sources/user) | data source |
| [azurerm_client_config.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config) | data source |
No resources.

## Inputs

Expand All @@ -167,6 +160,7 @@ If everything looks good, answer `yes` and wait for the new infrastructure to be
| <a name="input_enable_container_app_blob_storage"></a> [enable\_container\_app\_blob\_storage](#input\_enable\_container\_app\_blob\_storage) | Create an Azure Storage Account and Storage Container to be accessed by the Container App | `bool` | n/a | yes |
| <a name="input_enable_container_registry"></a> [enable\_container\_registry](#input\_enable\_container\_registry) | Set to true to create a container registry | `bool` | n/a | yes |
| <a name="input_enable_dns_zone"></a> [enable\_dns\_zone](#input\_enable\_dns\_zone) | Conditionally create a DNS zone | `bool` | n/a | yes |
| <a name="input_enable_event_hub"></a> [enable\_event\_hub](#input\_enable\_event\_hub) | Send Azure Container App logs to an Event Hub sink | `bool` | n/a | yes |
| <a name="input_enable_monitoring"></a> [enable\_monitoring](#input\_enable\_monitoring) | Create an App Insights instance and notification group for the Container App | `bool` | n/a | yes |
| <a name="input_environment"></a> [environment](#input\_environment) | Environment name. Will be used along with `project_name` as a prefix for all resources. | `string` | n/a | yes |
| <a name="input_existing_network_watcher_name"></a> [existing\_network\_watcher\_name](#input\_existing\_network\_watcher\_name) | Use an existing network watcher to add flow logs. | `string` | n/a | yes |
Expand Down
4 changes: 3 additions & 1 deletion terraform/container-apps-hosting.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module "azure_container_apps_hosting" {
source = "github.com/DFE-Digital/terraform-azurerm-container-apps-hosting?ref=v0.14.3"
source = "github.com/DFE-Digital/terraform-azurerm-container-apps-hosting?ref=v0.14.6"

environment = local.environment
project_name = local.project_name
Expand All @@ -14,6 +14,8 @@ module "azure_container_apps_hosting" {
container_command = local.container_command
container_secret_environment_variables = local.container_secret_environment_variables

enable_event_hub = local.enable_event_hub

enable_cdn_frontdoor = local.enable_cdn_frontdoor
enable_dns_zone = local.enable_dns_zone
dns_zone_domain_name = local.dns_zone_domain_name
Expand Down
7 changes: 0 additions & 7 deletions terraform/data.tf

This file was deleted.

65 changes: 12 additions & 53 deletions terraform/key-vault-tfvars-secrets.tf
Original file line number Diff line number Diff line change
@@ -1,54 +1,13 @@
resource "azurerm_key_vault" "tfvars" {
name = "${local.environment}${local.project_name}-tfvars"
location = module.azure_container_apps_hosting.azurerm_resource_group_default.location
resource_group_name = module.azure_container_apps_hosting.azurerm_resource_group_default.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
soft_delete_retention_days = 7
enable_rbac_authorization = false

dynamic "access_policy" {
for_each = data.azuread_user.key_vault_access

content {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = access_policy.value["object_id"]

key_permissions = [
"Create",
"Get",
]

secret_permissions = [
"Set",
"Get",
"Delete",
"Purge",
"Recover",
"List",
]
}
}

# It won't be possible to add/manage a network acl for this
# vault, as it will need to be accessable for multiple people.
# tfsec:ignore:azure-keyvault-specify-network-acl
network_acls {
bypass = "None"
default_action = "Allow"
}

purge_protection_enabled = true

tags = local.tags
}

# Expiry doesn't need to be set, as this is just used as a way to
# store and share the tfvars
# tfsec:ignore:azure-keyvault-ensure-secret-expiry
resource "azurerm_key_vault_secret" "tfvars" {
name = "${local.environment}${local.project_name}-tfvars"
value = base64encode(file(local.tfvars_filename))
key_vault_id = azurerm_key_vault.tfvars.id
content_type = "text/plain+base64"
module "azurerm_key_vault" {
source = "github.com/DFE-Digital/terraform-azurerm-key-vault-tfvars?ref=v0.1.1"

environment = local.environment
project_name = local.project_name
resource_group_name = module.azure_container_apps_hosting.azurerm_resource_group_default.name
azure_location = local.azure_location
key_vault_access_users = local.key_vault_access_users
tfvars_filename = local.tfvars_filename
diagnostic_log_analytics_workspace_id = module.azure_container_apps_hosting.azurerm_log_analytics_workspace_container_app.id
diagnostic_eventhub_name = local.enable_event_hub ? module.azure_container_apps_hosting.azurerm_eventhub_container_app.name : ""
tags = local.tags
}
1 change: 1 addition & 0 deletions terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ locals {
container_command = var.container_command
container_secret_environment_variables = var.container_secret_environment_variables
enable_cdn_frontdoor = var.enable_cdn_frontdoor
enable_event_hub = var.enable_event_hub
enable_dns_zone = var.enable_dns_zone
dns_zone_domain_name = var.dns_zone_domain_name
restrict_container_apps_to_cdn_inbound_only = var.restrict_container_apps_to_cdn_inbound_only
Expand Down
5 changes: 5 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ variable "container_secret_environment_variables" {
sensitive = true
}

variable "enable_event_hub" {
description = "Send Azure Container App logs to an Event Hub sink"
type = bool
}

variable "enable_cdn_frontdoor" {
description = "Enable Azure CDN Front Door. This will use the Container Apps endpoint as the origin."
type = bool
Expand Down

0 comments on commit 1bfb434

Please sign in to comment.