Cross region replication of AWS ECR repository

Viewed 2011

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,

3 Answers

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"
      }
  }
}}}

More easy way:

resource "aws_ecr_replication_configuration" "replication" {
  replication_configuration {
    rule {
      destination {
        region      = "us-east-2"
        registry_id = "xxxxxxxx"
      }
      destination {
        region      = "ap-southeast-1"       
        registry_id = "xxxxxxxx"
      }
    }
  }
}
variable "replicas" {
  description = "ECR replicas region list"
  type        = list(string)
  default = [
    {
      region      = "aaa"
      registry_id = "11111111"
    },
    {
      region      = "bbb"
      registry_id = "22222222"
    }
  ]
}

resource "aws_ecr_replication_configuration" "replication" {
  count = length(var.replicas) != 0 ? 1 : 0

  replication_configuration {
    rule {
      dynamic "destination" {
        for_each = var.replicas
        content {
          region      = destination.value.region
          registry_id = destination.value.registry_id
        }
      }
      repository_filter {
        filter      = var.filter
        filter_type = "PREFIX_MATCH"
      }
    }
  }
}
Related