Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you create an Azure container registry and a resource group using Terraform. Azure Container Registry is a managed Docker registry service used for storing private Docker container images. It's typically used with Azure Kubernetes Service (AKS), Azure App Service, and other Azure services to pull down container images. The registry is stored within a resource group, which is a logical container for resources deployed on Azure. These resources are created with unique names by combining a prefix with a random string, ensuring they are unique within your Azure subscription.
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.
- Specify the required version of Terraform and the required providers.
- Define the Azure provider with no additional features.
- Define a variable for the location of the resource group and with a default value of "chinaeast2".
- Define a variable for the prefix of the resource group name and with a default value of "rg".
- Generate a random pet name for the resource group.
- Create an Azure resource group with the generated name at a specified location.
- Generate a random string of five lowercase letters to be used as part of the container registry name.
- Create a container registry with the generated string as part of its name and in the same location and resource group as above.
- Output the names of the created resource group and container registry.
- Output the login server of the created container registry.
Create an Azure account with an active subscription. You can create a trial subscription.
Note
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.
See more articles and sample code showing how to use Terraform to manage Azure resources.
Create a directory in which to test and run the sample Terraform code, and make it the current directory.
Create a file named
main.tf
, and insert the following code:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id } resource "random_string" "acr_name" { length = 5 lower = true numeric = false special = false upper = false } resource "azurerm_container_registry" "example" { name = "${random_string.acr_name.result}registry" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location sku = "Standard" }
Create a file named
outputs.tf
, and insert the following code:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "container_registry_name" { value = azurerm_container_registry.example.name } output "container_registry_login_server" { value = azurerm_container_registry.example.login_server }
Create a file named
providers.tf
, and insert the following code:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
Create a file named
variables.tf
, and insert the following code:variable "resource_group_location" { type = string default = "chinaeast2" description = "Location of the resource group." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." }
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Key points:
- The
terraform plan
command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-out
parameter allows you to specify an output file for the plan. Using the-out
parameter ensures that the plan you reviewed is exactly what is applied.
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
Run az acr show to view the container registry.
az acr show --name <registry_name> --resource-group <resource_group_name>
Replace <registry_name>
with the name of your container registry and <resource_group_name>
with the name of your resource group.
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.terraform plan -destroy -out main.destroy.tfplan
Key points:
- The
terraform plan
command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-out
parameter allows you to specify an output file for the plan. Using the-out
parameter ensures that the plan you reviewed is exactly what is applied.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan