Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remote state #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A collection of Azure resources ready to be used with [Humanitec](https://humani
The following resources are included:

* [azure-blob/basic](./humanitec-resource-defs/azure-blob/basic): Basic Azure Storage Blob Container.
* This example also show-cases how to configure a [Terraform Backend](https://developer.humanitec.com/integration-and-extensions/drivers/generic-drivers/terraform/).
* [azure-blob/delegator](./humanitec-resource-defs/azure-blob/delegator): Echo Azure Storage Blob Container output and co-provision role definition.
* [azure-federated-identity/basic](./humanitec-resource-defs/azure-federated-identity/basic): Basic Azure Federated Identity for bounding K8s Service Account with Azure Managed Entity.
* [azure-managed-identity/basic](./humanitec-resource-defs/azure-managed-identity/basic): Basic Azure managed identity.
Expand Down
5 changes: 5 additions & 0 deletions examples/blob-storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The workload service account will automatically be assigned the necessary Azure
| azuread | ~> 2.47 |
| azurerm | ~> 3.91 |
| humanitec | ~> 1.0 |
| random | ~> 3.6 |

## Providers

Expand All @@ -36,6 +37,7 @@ The workload service account will automatically be assigned the necessary Azure
| azuread | ~> 2.47 |
| azurerm | ~> 3.91 |
| humanitec | ~> 1.0 |
| random | ~> 3.6 |

## Modules

Expand All @@ -60,6 +62,8 @@ The workload service account will automatically be assigned the necessary Azure
| [azuread_service_principal.humanitec_provisioner](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal) | resource |
| [azuread_service_principal_password.humanitec_provisioner](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal_password) | resource |
| [azurerm_role_assignment.resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
| [azurerm_storage_account.tfstate](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account) | resource |
| [azurerm_storage_container.tfstate](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_container) | resource |
| [humanitec_application.example](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/application) | resource |
| [humanitec_resource_account.humanitec_provisioner](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_account) | resource |
| [humanitec_resource_definition_criteria.blob_storage](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
Expand All @@ -72,6 +76,7 @@ The workload service account will automatically be assigned the necessary Azure
| [humanitec_resource_definition_criteria.role_definition_admin](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.role_definition_reader](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [humanitec_resource_definition_criteria.workload](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |
| [random_string.storage_account_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
| [azurerm_resource_group.main](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |

## Inputs
Expand Down
31 changes: 30 additions & 1 deletion examples/blob-storage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "azuread_service_principal_password" "humanitec_provisioner" {

resource "azurerm_role_assignment" "resource_group" {
scope = data.azurerm_resource_group.main.id
role_definition_name = "Contributor"
role_definition_name = "Owner"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required as this example creates a new role for the workload itself, which requires the Owner role.

principal_id = azuread_service_principal.humanitec_provisioner.object_id
}

Expand All @@ -32,6 +32,11 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group
]
}

# Example application and resource definition criteria
Expand Down Expand Up @@ -64,6 +69,7 @@ module "blob_storage" {
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
resource_group_name = var.resource_group_name
Expand All @@ -78,6 +84,8 @@ resource "humanitec_resource_definition_criteria" "blob_storage" {
resource_definition_id = module.blob_storage.id
app_id = humanitec_application.example.id
class = local.blob_storage_basic_class

force_delete = true
}

// Admin shared
Expand All @@ -95,6 +103,8 @@ resource "humanitec_resource_definition_criteria" "blob_storage_admin" {
resource_definition_id = module.blob_storage_admin.id
app_id = humanitec_application.example.id
class = local.blob_storage_admin_class

force_delete = true
}

module "role_definition_admin" {
Expand All @@ -110,6 +120,8 @@ resource "humanitec_resource_definition_criteria" "role_definition_admin" {
resource_definition_id = module.role_definition_admin.id
app_id = humanitec_application.example.id
class = local.blob_storage_admin_policy_class

force_delete = true
}

// Reader shared
Expand All @@ -127,6 +139,8 @@ resource "humanitec_resource_definition_criteria" "blob_storage_reader" {
resource_definition_id = module.blob_storage_reader.id
app_id = humanitec_application.example.id
class = local.blob_storage_reader_class

force_delete = true
}

module "role_definition_reader" {
Expand All @@ -142,6 +156,8 @@ resource "humanitec_resource_definition_criteria" "role_definition_reader" {
resource_definition_id = module.role_definition_reader.id
app_id = humanitec_application.example.id
class = local.blob_storage_reader_policy_class

force_delete = true
}

// Workload based
Expand All @@ -155,6 +171,8 @@ module "workload" {
resource "humanitec_resource_definition_criteria" "workload" {
resource_definition_id = module.workload.id
app_id = humanitec_application.example.id

force_delete = true
}

module "k8s_service_account" {
Expand All @@ -166,6 +184,8 @@ module "k8s_service_account" {
resource "humanitec_resource_definition_criteria" "k8s_service_account" {
resource_definition_id = module.k8s_service_account.id
app_id = humanitec_application.example.id

force_delete = true
}

module "federated_identity" {
Expand All @@ -174,6 +194,7 @@ module "federated_identity" {
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id

Expand All @@ -189,6 +210,8 @@ module "federated_identity" {
resource "humanitec_resource_definition_criteria" "federated_identity" {
resource_definition_id = module.federated_identity.id
app_id = humanitec_application.example.id

force_delete = true
}

module "managed_identity" {
Expand All @@ -197,6 +220,7 @@ module "managed_identity" {
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id

Expand All @@ -207,6 +231,8 @@ module "managed_identity" {
resource "humanitec_resource_definition_criteria" "managed_identity" {
resource_definition_id = module.managed_identity.id
app_id = humanitec_application.example.id

force_delete = true
}

module "role_assignment" {
Expand All @@ -215,6 +241,7 @@ module "role_assignment" {
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id

Expand All @@ -227,4 +254,6 @@ module "role_assignment" {
resource "humanitec_resource_definition_criteria" "role_assignment" {
resource_definition_id = module.role_assignment.id
app_id = humanitec_application.example.id

force_delete = true
}
4 changes: 4 additions & 0 deletions examples/blob-storage/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ terraform {
source = "humanitec/humanitec"
version = "~> 1.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}

required_version = ">= 1.3.0"
Expand Down
33 changes: 33 additions & 0 deletions examples/blob-storage/state.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Resources required for the terraform backend
# More details https://developer.humanitec.com/integration-and-extensions/drivers/generic-drivers/terraform/

resource "random_string" "storage_account_suffix" {
length = 12
special = false
upper = false
}

resource "azurerm_storage_account" "tfstate" {
name = "humrp${random_string.storage_account_suffix.result}tfstate"
resource_group_name = data.azurerm_resource_group.main.name
location = data.azurerm_resource_group.main.location
account_tier = "Standard"
account_replication_type = "ZRS"
allow_nested_items_to_be_public = false
}

resource "azurerm_storage_container" "tfstate" {
name = var.name
storage_account_name = azurerm_storage_account.tfstate.name
container_access_type = "private"
}

locals {
terraform_state = {
subscription_id = var.subscription_id
resource_group_name = data.azurerm_resource_group.main.name
storage_account_name = azurerm_storage_account.tfstate.name
container_name = azurerm_storage_container.tfstate.name
# key_prefix is set by the respective resource definition
}
}
5 changes: 5 additions & 0 deletions examples/dns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group
]
}

# Example application and resource definition criteria
Expand Down
5 changes: 5 additions & 0 deletions examples/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group
]
}

# Example application and resource definition criteria
Expand Down
18 changes: 6 additions & 12 deletions examples/postgres/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group_resource,
azurerm_role_assignment.resource_group_workload
]
}

# Example application and resource definition criteria
Expand Down Expand Up @@ -72,12 +78,6 @@ module "postgres_instance" {
virtual_network_name = var.virtual_network_name
subnet_name = var.subnet_name
workload_resource_group_name = data.azurerm_resource_group.workload.name

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group_resource,
azurerm_role_assignment.resource_group_workload
]
}

resource "humanitec_resource_definition_criteria" "postgres_instance" {
Expand All @@ -99,12 +99,6 @@ module "postgres" {
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
instance_resource = "postgres-instance.${local.postgres_instance_class}#${local.postgres_instance_res_id}"

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group_resource,
azurerm_role_assignment.resource_group_workload
]
}

resource "humanitec_resource_definition_criteria" "postgres" {
Expand Down
5 changes: 5 additions & 0 deletions examples/redis/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group_workload
]
}

# Example application and resource definition criteria
Expand Down
7 changes: 6 additions & 1 deletion examples/service-bus/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "azuread_service_principal_password" "humanitec_provisioner" {

resource "azurerm_role_assignment" "resource_group_workload" {
scope = data.azurerm_resource_group.main.id
role_definition_name = "Contributor"
role_definition_name = "Owner"
principal_id = azuread_service_principal.humanitec_provisioner.object_id
}

Expand All @@ -32,6 +32,11 @@ resource "humanitec_resource_account" "humanitec_provisioner" {
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})

depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group_workload
]
}

# Example application and resource definition criteria
Expand Down
1 change: 1 addition & 0 deletions humanitec-resource-defs/azure-blob/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
| prefix | Specifies the prefix used in default name for created resources. | `string` | `"hum-rp-blob-storage-ex-"` | no |
| resource\_packs\_azure\_rev | Azure Resource Pack git branch. | `string` | `"refs/heads/main"` | no |
| resource\_packs\_azure\_url | Azure Resource Pack git url. | `string` | `"https://github.com/humanitec-architecture/resource-packs-azure.git"` | no |
| terraform\_state | Use terraform remote state. | <pre>object({<br> subscription_id = string<br> resource_group_name = string<br> storage_account_name = string<br> container_name = string<br> key_prefix = optional(string)<br> })</pre> | `null` | no |

## Outputs

Expand Down
26 changes: 24 additions & 2 deletions humanitec-resource-defs/azure-blob/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
locals {
def_id = "${var.prefix}azure-blob-storage-basic"
remote_backend = <<EOT
terraform {
required_version = ">= 1.0.0"

%{if var.terraform_state != null}backend "azurerm" {
subscription_id = "${var.terraform_state.subscription_id}"
delca85 marked this conversation as resolved.
Show resolved Hide resolved
resource_group_name = "${var.terraform_state.resource_group_name}"
storage_account_name = "${var.terraform_state.storage_account_name}"
container_name = "${var.terraform_state.container_name}"
key = "${coalesce(var.terraform_state.key_prefix, local.def_id)}/$${context.app.id}/$${context.env.id}/$${context.res.id}.tfstate"
}%{endif}
}
EOT
files = var.terraform_state != null ? {
"backend.tf" = local.remote_backend
} : {}
}

resource "humanitec_resource_definition" "main" {
driver_type = "humanitec/terraform"
id = "${var.prefix}azure-blob-storage-basic"
name = "${var.prefix}azure-blob-storage-basic"
id = local.def_id
name = local.def_id
type = "azure-blob"

driver_account = var.driver_account
Expand All @@ -23,6 +43,8 @@ resource "humanitec_resource_definition" "main" {
}
}

files = local.files

variables = {
res_id = "$${context.res.id}"
app_id = "$${context.app.id}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ resource_packs_azure_rev = "refs/heads/main"
resource_packs_azure_url = "https://github.com/humanitec-architecture/resource-packs-azure.git"

# The Subscription ID which should be used.
subscription_id = ""
subscription_id = ""

# Use terraform remote state.
terraform_state = ""
12 changes: 12 additions & 0 deletions humanitec-resource-defs/azure-blob/basic/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ variable "container_access_type" {
type = string
default = "private"
}

variable "terraform_state" {
description = "Use terraform remote state."
type = object({
subscription_id = string
resource_group_name = string
storage_account_name = string
container_name = string
key_prefix = optional(string)
})
default = null
}
Loading
Loading