Asterix
Asterix

Reputation: 433

Second Container registry in Terraform

I have an existing terraform script where one container registry is defined. We are in the process of creating a new one with customer managed key and once its established, the old one will be deleted. So in the terraform script I copied the same module for container registry and gave a new name. But it gave an error saying multiple entries are seen. Then I gave count=2 in container registry module. That also gave an error.

I am unable to implement customer managed key as well due to this error. Is there a way to achieve two container registry in one terraform script?

Upvotes: 0

Views: 317

Answers (1)

kavya Saraboju
kavya Saraboju

Reputation: 10831

Initially i tried to reproduce the same and tried to change the name of the registry , then new one got created deleting the old registry .It was fine.

  • So then I jus copied the same module next to previous one and changed the name of reference only, without changing the registry name.Then it threw error as , it already exists.

Note:make sure you save changes every time you modify and then do terraform flow.

I even got error as containerregistry1 already exists even if acr01 is changed to acr03 .(as registry name is not changed here) enter image description here But when I changed registry name also.then I could successfully create other similar instances of the registry successfully.

Even tried using count=3 in container registry module which gave me the similar error.

provider "azurerm" {

  features {}

}


data "azurerm_resource_group" "example" {
  name     = "<myResourcegroup>"
  
}

resource "azurerm_container_registry" "acr" {

  count = 3
  name                = "containerkavyasarabojuRegistry01"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location
  sku                 = "Premium"
  admin_enabled       = false

  georeplications {
    location                = "East US"
    zone_redundancy_enabled = true
    tags                    = {}
  }

  ...
   ...

enter image description here

This is caused as the registry name must be unique too.Azure container registry has global scope( resources having global scope).So it must be unique globally.

  • So we must make sure registry name must be unique.

So along with count = x, even registry name must be varied or incremented like below:

count = 3
name = "containerkavyasarabojuRegistry01${format("%03d", count.index + 1)}"

example:

data "azurerm_resource_group" "example" {
  name     = "<myresourcegroup>"
  
}

resource "azurerm_container_registry" "acr" {
  count = 3
  name                = "containerkavyasarabojuRegistry01${format("%03d", count.index + 1)}" //check this
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location
  sku                 = "Premium"
  admin_enabled       = false
      
   ...
  georeplications {
    ...
  }

  ...

enter image description here

RESULT: we can see the registries created with unique names.

enter image description here

You can check if acr name already exists through az cli -az acr check name before actually starting to give a name:

az acr check-name -n doesthisnameexist

Reference: terraform-modules-and-multiple-instances

Upvotes: 2

Related Questions