Skip to content

Commit

Permalink
Add module for Container App Environment with tests
Browse files Browse the repository at this point in the history
Introduce a Terraform module for creating a Container App Environment, including support for optional features like subnet configuration and zone redundancy. Accompanying the module are test configurations, outputs, and documentation for ease of use and validation.
  • Loading branch information
ffppa committed Jan 27, 2025
1 parent 1b12c57 commit c9c3212
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 0 deletions.
46 changes: 46 additions & 0 deletions container_app_environment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Container app environment

This resource allow the creation of a Container App Environment as Consumption plan (Workload profiles are not supported by this module).

Deploying the Container app environment in a custom subnet, unlocks other features such as zone redundancy and internal load balancing.

<!-- markdownlint-disable -->
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | ~> 4 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [azurerm_container_app_environment.container_app_environment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/container_app_environment) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_internal_load_balancer"></a> [internal\_load\_balancer](#input\_internal\_load\_balancer) | Internal Load Balancing Mode. Can be true only if a subnet\_id is provided | `bool` | `false` | no |
| <a name="input_location"></a> [location](#input\_location) | Resource location. | `string` | n/a | yes |
| <a name="input_log_analytics_workspace_id"></a> [log\_analytics\_workspace\_id](#input\_log\_analytics\_workspace\_id) | Log Analytics Workspace resource id | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | (Required) Resource name | `string` | n/a | yes |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Resource group name | `string` | n/a | yes |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | (Optional) Subnet id if the environment is in a custom virtual network | `string` | `null` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(any)` | n/a | yes |
| <a name="input_zone_redundant"></a> [zone\_redundant](#input\_zone\_redundant) | Deploy multi zone environment. Can be true only if a subnet\_id is provided | `bool` | `false` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_id"></a> [id](#output\_id) | n/a |
| <a name="output_name"></a> [name](#output\_name) | n/a |
| <a name="output_resource_group_name"></a> [resource\_group\_name](#output\_resource\_group\_name) | n/a |
<!-- END_TF_DOCS -->
13 changes: 13 additions & 0 deletions container_app_environment/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "azurerm_container_app_environment" "container_app_environment" {
name = var.name
location = var.location
resource_group_name = var.resource_group_name

log_analytics_workspace_id = var.log_analytics_workspace_id

infrastructure_subnet_id = var.subnet_id == null ? null : var.subnet_id
zone_redundancy_enabled = var.subnet_id == null ? null : var.zone_redundant
internal_load_balancer_enabled = var.subnet_id == null ? null : var.internal_load_balancer

tags = var.tags
}
11 changes: 11 additions & 0 deletions container_app_environment/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "id" {
value = azurerm_container_app_environment.container_app_environment.id
}

output "resource_group_name" {
value = azurerm_container_app_environment.container_app_environment.resource_group_name
}

output "name" {
value = azurerm_container_app_environment.container_app_environment.name
}
1 change: 1 addition & 0 deletions container_app_environment/tests/backend.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subscription=DevOpsLab
28 changes: 28 additions & 0 deletions container_app_environment/tests/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
terraform {
required_version = ">= 1.5.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "<= 3.94.0"
}
}
}

provider "azurerm" {
features {
key_vault {
purge_soft_delete_on_destroy = false
}
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}

data "azurerm_client_config" "current" {
}

resource "random_id" "unique" {
byte_length = 3
}
7 changes: 7 additions & 0 deletions container_app_environment/tests/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "random_id" {
value = random_id.unique.hex
}

output "cae_id" {
value = module.container_app_environment.id
}
26 changes: 26 additions & 0 deletions container_app_environment/tests/resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "azurerm_resource_group" "rg" {
name = "${var.resource_group_name}-${random_id.unique.hex}"
location = var.location

tags = var.tags
}

resource "azurerm_log_analytics_workspace" "law" {
name = var.law_name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Free"
retention_in_days = 30
}

module "container_app_environment" {
source = "../" # change me with module URI

resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
name = "${var.prefix}-cae"
internal_load_balancer = false
log_analytics_workspace_id = azurerm_log_analytics_workspace.law.id

tags = var.tags
}
34 changes: 34 additions & 0 deletions container_app_environment/tests/terraform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -e

action=$1
shift 1
other=$@

subscription="MOCK_VALUE"

case $action in
"init" | "apply" | "plan" | "destroy" )
# shellcheck source=/dev/null
if [ -e "./backend.ini" ]; then
source ./backend.ini
else
echo "Error: no backend.ini found!"
exit 1
fi

az account set -s "${subscription}"

terraform init
terraform "$action" $other
;;
"clean" )
rm -rf .terraform* terraform.tfstate*
echo "cleaned..."
;;
* )
echo "Missed action: init, apply, plan, destroy clean"
exit 1
;;
esac
29 changes: 29 additions & 0 deletions container_app_environment/tests/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
variable "location" {
type = string
default = "westeurope"
}

variable "prefix" {
type = string
default = "azrmtest"
}

variable "resource_group_name" {
type = string
default = "azrmtest-rg"
}

variable "tags" {
type = map(string)
description = "List of tags"
default = {
CreatedBy = "Terraform"
Source = "https://github.com/pagopa/terraform-azurerm-v3"
}
}

variable "law_name" {
type = string

default = "azrmtest-law"
}
42 changes: 42 additions & 0 deletions container_app_environment/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "name" {
type = string
description = "(Required) Resource name"
}

variable "resource_group_name" {
type = string
description = "Resource group name"
}

variable "location" {
type = string
description = "Resource location."
}

variable "subnet_id" {
type = string
description = "(Optional) Subnet id if the environment is in a custom virtual network"

default = null
}

variable "zone_redundant" {
type = bool
description = "Deploy multi zone environment. Can be true only if a subnet_id is provided"
default = false
}

variable "internal_load_balancer" {
type = bool
description = "Internal Load Balancing Mode. Can be true only if a subnet_id is provided"
default = false
}

variable "log_analytics_workspace_id" {
type = string
description = "Log Analytics Workspace resource id"
}

variable "tags" {
type = map(any)
}
10 changes: 10 additions & 0 deletions container_app_environment/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.9.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4"
}
}
}

0 comments on commit c9c3212

Please sign in to comment.