diff --git a/quickstart/101-aci-linuxcontainer-public-ip/main.tf b/quickstart/101-aci-linuxcontainer-public-ip/main.tf new file mode 100644 index 00000000..0bfb3efd --- /dev/null +++ b/quickstart/101-aci-linuxcontainer-public-ip/main.tf @@ -0,0 +1,36 @@ +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + name = random_pet.rg_name.id + location = var.resource_group_location +} + +resource "random_string" "container_name" { + length = 25 + lower = true + upper = false + special = false +} + +resource "azurerm_container_group" "container" { + name = "${var.container_group_name_prefix}-${random_string.container_name.result}" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + ip_address_type = "Public" + os_type = "Linux" + restart_policy = var.restart_policy + + container { + name = "${var.container_name_prefix}-${random_string.container_name.result}" + image = var.image + cpu = var.cpu_cores + memory = var.memory_in_gb + + ports { + port = var.port + protocol = "TCP" + } + } +} diff --git a/quickstart/101-aci-linuxcontainer-public-ip/outputs.tf b/quickstart/101-aci-linuxcontainer-public-ip/outputs.tf new file mode 100644 index 00000000..5a07d7b8 --- /dev/null +++ b/quickstart/101-aci-linuxcontainer-public-ip/outputs.tf @@ -0,0 +1,3 @@ +output "container_ipv4_address" { + value = azurerm_container_group.container.ip_address +} diff --git a/quickstart/101-aci-linuxcontainer-public-ip/providers.tf b/quickstart/101-aci-linuxcontainer-public-ip/providers.tf new file mode 100644 index 00000000..74326fc6 --- /dev/null +++ b/quickstart/101-aci-linuxcontainer-public-ip/providers.tf @@ -0,0 +1,16 @@ +terraform { + required_version = ">=1.0" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.4" + } + random = { + source = "hashicorp/random" + version = "~>3.4" + } + } +} +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-aci-linuxcontainer-public-ip/readme.md b/quickstart/101-aci-linuxcontainer-public-ip/readme.md new file mode 100644 index 00000000..b90bb757 --- /dev/null +++ b/quickstart/101-aci-linuxcontainer-public-ip/readme.md @@ -0,0 +1,26 @@ +# Azure Container Instance with public IP + +This template deploys an Azure Container Instance with a public IP address. + +## Terraform resource types + +- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [azurerm_container_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/container_group) + +## Variables + +| Name | Description | Default | +|-|-|-| +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | +| `name` | Name for the container group | acilinuxpublicipcontainergroup | +| `image` | Container image to deploy. | mcr.microsoft.com/azuredocs/aci-helloworld" | +| `port` | Port to open on the container and the public IP address. | 80 | +| `cpu_cores` | Quantity of CPU cores to allocate to the container. | 1 | +| `memory_in_gb` | Amount of memory to allocate to the container in gigabytes. | 2 | +| `restart_policy` | Behavior of Azure runtime if container has stopped. | Always | + +## Example + +To see how to run this example, see [Create an Azure resource group using Terraform](https://docs.microsoft.com/azure/developer/terraform/create-resource-group). \ No newline at end of file diff --git a/quickstart/101-aci-linuxcontainer-public-ip/variables.tf b/quickstart/101-aci-linuxcontainer-public-ip/variables.tf new file mode 100644 index 00000000..86ef8b5c --- /dev/null +++ b/quickstart/101-aci-linuxcontainer-public-ip/variables.tf @@ -0,0 +1,57 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location for all resources." +} + +variable "resource_group_name_prefix" { + type = string + default = "rg" + description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription." +} + +variable "container_group_name_prefix" { + type = string + description = "Prefix of the container group name that's combined with a random value so name is unique in your Azure subscription." + default = "acigroup" +} + +variable "container_name_prefix" { + type = string + description = "Prefix of the container name that's combined with a random value so name is unique in your Azure subscription." + default = "aci" +} + +variable "image" { + type = string + description = "Container image to deploy. Should be of the form repoName/imagename:tag for images stored in public Docker Hub, or a fully qualified URI for other registries. Images from private registries require additional registry credentials." + default = "mcr.microsoft.com/azuredocs/aci-helloworld" +} + +variable "port" { + type = number + description = "Port to open on the container and the public IP address." + default = 80 +} + +variable "cpu_cores" { + type = number + description = "The number of CPU cores to allocate to the container." + default = 1 +} + +variable "memory_in_gb" { + type = number + description = "The amount of memory to allocate to the container in gigabytes." + default = 2 +} + +variable "restart_policy" { + type = string + description = "The behavior of Azure runtime if container has stopped." + default = "Always" + validation { + condition = contains(["Always", "Never", "OnFailure"], var.restart_policy) + error_message = "The restart_policy must be one of the following: Always, Never, OnFailure." + } +}