如何将安全组应用到 aws_elasticache_replication_group

how to apply security groups to aws_elasticache_replication_group

我的 terraform 脚本如下: VPC 中的所有内容

resource "aws_security_group" "cacheSecurityGroup" {
  name   = "${var.devname}-${var.namespace}-${var.stage}-RedisCache-SecurityGroup"
  vpc_id = var.vpc.vpc_id
  tags   = var.default_tags
  ingress {
    protocol         = "tcp"
    from_port        = 6379
    to_port          = 6379
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  egress {
    protocol         = "-1"
    from_port        = 0
    to_port          = 0
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }
}

resource "aws_elasticache_parameter_group" "usagemonitorCacheParameterGroup" {
  name    = "${var.devname}${var.namespace}${var.stage}-usagemonitor-cache-parameterGroup"
  family  = "redis6.x"
}

resource "aws_elasticache_subnet_group" "redis_subnet_group" {
  name       = "${var.devname}${var.namespace}${var.stage}-usagemonitor-cache-subnetGroup"
  subnet_ids = var.vpc.database_subnets
}

resource "aws_elasticache_replication_group" "replication_group_usagemonitor" {
  replication_group_id          = "${var.devname}${var.namespace}${var.stage}-usagemonitor-cache"
  replication_group_description = "Replication group for Usagemonitor"
  node_type                     = "cache.t2.micro"
  number_cache_clusters         = 2
  parameter_group_name          = aws_elasticache_parameter_group.usagemonitorCacheParameterGroup.name
  subnet_group_name             = aws_elasticache_subnet_group.redis_subnet_group.name
  #security_group_names          = [aws_elasticache_security_group.bar.name]
  automatic_failover_enabled    = true
  at_rest_encryption_enabled    = true
  port                          = 6379
}

如果我取消注释该行

#security_group_names          = [aws_elasticache_security_group.bar.name]

正在获取 我收到以下错误:

Error: Error creating Elasticache Replication Group: InvalidParameterCombination: Use of cache security groups is not permitted along with cache subnet group and/or security group Ids.
    status code: 400, request id: 4e70e86d-b868-45b3-a1d2-88ab652dc85e

我读到如果所有资源都在 VPC 中,我们不必使用 aws_elasticache_security_group。将安全组分配给 aws_elasticache_replication_group 的正确方法是什么??? usinf 子网???怎么样???

我这样做了,我相信这是分配所需配置的最佳方式:

resource "aws_security_group" "redis" {
  name_prefix = "${var.name_prefix}-redis-"
  vpc_id      = var.vpc_id

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_elasticache_replication_group" "redis" {
  ...
  engine = "redis"
  subnet_group_name    = aws_elasticache_subnet_group.redis.name
  security_group_ids   = concat(var.security_group_ids, [aws_security_group.redis.id])
}

您的子网组基本上包括所有私有或 public 来自将要创建 elasticache 复制组的 VPC 的子网。

一般情况下,使用安全组 ID 而不是名称。

我已经编写了一个绝对有效的 terraform 模块,如果您有兴趣,可以在示例 https://github.com/umotif-public/terraform-aws-elasticache-redis.

下找到它