amazon-web-services - 如何将安全组应用于 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 的正确方法是什么???使用子网???如何 ???
解决方案
我做这样的事情,我相信这是分配所需配置的最佳方式:
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])
}
您的子网组基本上包括您的 VPC 中将在其中创建 elasticache 复制组的所有私有或公共子网。
通常,使用安全组 ID 而不是名称。
我编写了一个绝对有效的 terraform 模块,如果您有兴趣,可以在示例https://github.com/umotif-public/terraform-aws-elasticache-redis下找到它。
推荐阅读
- django - django_hosts - NoReverseMatch:找不到“家”的反向。'home' 不是有效的视图函数或模式名称
- javascript - Paperjs 组与 PointText
- python - 如何使用 API 密钥和 python 库正确连接到 Elasticsearch 云?
- javascript - 将 8 字节 (u64) 无符号整数转换为 javascript 的建议
- c++ - C ++在保存完成之前防止系统退出
- machine-learning - 在 neuraxle 管道中使用 fit_params
- emmeans - 测试结果中的对比对比(第一/第二差异)
- python - 鼠标抑制 pynput.mouse 中的不同输出
- angular - Angular 路由是否订阅了 RouteGuards 返回的 observable?
- r - 如何在r中按组选择最高值的行?