使用Terraform创建ElastiCache Redis实例遇CacheSecurityGroup参数错误
解决Terraform创建AWS ElastiCache Redis实例时的CacheSecurityGroup错误
问题根源
你碰到的InvalidParameterValue: Use of cache security groups is not permitted in this API version for your account错误,核心原因是AWS已经全面淘汰了经典网络模式下的ElastiCache专属安全组(CacheSecurityGroup)。现在新账户或使用较新API版本的场景,必须采用VPC部署模式的ElastiCache实例,搭配标准VPC安全组来管控访问权限,而非旧的CacheSecurityGroup。
具体修复步骤
替换旧的CacheSecurityGroup资源
删除原来的aws_elasticache_security_group资源,改用标准的aws_security_group来定义Redis实例的访问规则,示例配置如下:resource "aws_security_group" "redis" { name = "redis-access-sg" description = "管控Redis实例的访问权限" vpc_id = var.your_vpc_id # 替换为你的VPC ID # 允许指定IP段访问Redis默认端口6379 ingress { from_port = 6379 to_port = 6379 protocol = "tcp" cidr_blocks = ["10.0.0.0/16"] # 替换为实际需要访问的IP段 } # 默认放行所有出站流量(可按需调整) egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }配置ElastiCache子网组
必须创建关联VPC私有子网的ElastiCache子网组,确保实例部署在VPC内:resource "aws_elasticache_subnet_group" "redis" { name = "redis-subnet-group" subnet_ids = var.your_private_subnet_ids # 替换为你的私有子网ID列表 }更新ElastiCache实例配置
在你的aws_elasticache_replication_group(单节点可用aws_elasticache_cluster)资源中,移除cache_security_group_name参数,改用security_group_ids引用上面创建的VPC安全组,同时指定subnet_group_name:resource "aws_elasticache_replication_group" "redis" { replication_group_id = "prod-redis-cluster" replication_group_description = "生产环境Redis集群" node_type = var.redis_node_type number_cache_clusters = 1 # 按需调整节点数量 subnet_group_name = aws_elasticache_subnet_group.redis.name security_group_ids = [aws_security_group.redis.id] # 其他自定义配置(如密码、备份策略等)... }
额外提示
- 目前AWS不再支持创建经典网络模式的ElastiCache实例,所有新实例必须部署在VPC内,这是硬性要求。
- 建议升级你的Terraform AWS Provider到最新版本,旧版本可能仍在调用已废弃的API端点,容易引发此类兼容性问题。
内容的提问来源于stack exchange,提问作者Jeremy




