Search code examples
amazon-web-servicesterraformamazon-ecrterragruntelastic-container-registry

Cross region replication of AWS ECR repository


I am trying to replicate my AWS ECR repository to multiple regions within the same account using terraform. I tried manually from the AWS console it works fine but from terraform, I am not able to find the solution. What I tried: I tried to make a separate variable for the region called replicate_region and tried to provide the region in the list but it keeps on giving me an error called

Inappropriate value for attribute "region": string required.

Here is the variable code:

variable "replicate_region" {
 description = "value"
 type = list(string)
}

Here is my code for ecr replication:

resource "aws_ecr_replication_configuration" "replication" {
 replication_configuration {
  rule {
   destination {
     region      = var.replicate_region
     registry_id = "xxxxxxxx"
  }
}}}

Can anyone please help me out?

Thanks,


Solution

  • Your replicate_region should be string, not a list of strings. It should be, e.g.:

    variable "replicate_region" {
     description = "value"
     type = string
     default = "us-east-1"
    }
    

    Update

    Iteration using dynamic block.

    variable "replicate_region" {
     description = "value"
     type = list(string)
     default = ["us-east-1", "ap-southeast-1", "ap-south-1"]
    }
    
    resource "aws_ecr_replication_configuration" "replication" {
    
     replication_configuration {
      rule {
    
       dynamic "destination" {
    
           for_each = toset(var.replicate_region) 
    
           content {
             region      = destination.key
             registry_id = "xxxxxxxx"
          }
      }
    }}}