AWS ECR 存储库的跨区域复制

Cross region replication of AWS ECR repository

我正在尝试使用 Terraform 将我的 AWS ECR 存储库复制到同一账户内的多个区域。我从 AWS 控制台手动尝试它工作正常但从 terraform,我无法找到解决方案。 我尝试了什么: 我试图为名为 replicate_region 的区域创建一个单独的变量,并试图在列表中提供该区域,但它一直给我一个名为

的错误

Inappropriate value for attribute "region": string required.

变量代码如下:

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

这是我的 ecr 复制代码:

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

谁能帮帮我?

谢谢,

您的 replicate_region 应该是字符串 ,而不是字符串列表。应该是,例如:

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

更新

迭代使用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"
      }
  }
}}}

更简单的方法:

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