Terraform创建awscc_elasticache_serverless_cache失败排查与解决
解决Terraform创建ElastiCache Serverless时的名称过长错误
错误详情
执行terraform apply时触发以下错误:
Error: AWS SDK Go Service Operation Incomplete │ │ with awscc_elasticache_serverless_cache.redis_cluster, │ on main.tf line 281, in resource "awscc_elasticache_serverless_cache" "redis_cluster": │ 281: resource "awscc_elasticache_serverless_cache" "redis_cluster" { │ │ Waiting for Cloud Control API service CreateResource operation completion │ returned: waiter state transitioned to FAILED. StatusMessage: Serverless │ Cache Name cannot be longer than 40 characters. (Service: ElastiCache, Status │ Code: 400, Request ID: 6e1a2bf2-6fd9-46a4-88b6-f01b21e080c2). ErrorCode: │ InvalidRequest
核心原因:AWS ElastiCache Serverless要求缓存名称长度不能超过40字符,当前配置中serverless_cache_name直接复用var.name变量,若该变量值长度超出限制就会触发此报错。
解决方案
1. 给name变量添加长度验证(推荐)
在变量定义文件中为name变量增加校验规则,提前拦截不符合要求的输入:
variable "name" { description = "Name of the application" type = string default = "value" validation { condition = length(var.name) <= 40 error_message = "应用名称长度不能超过40字符,否则无法创建ElastiCache Serverless缓存。" } }
2. 自动截断过长的名称(无需手动修改变量值)
如果不想手动调整var.name的长度,可以在资源配置中自动截断名称至40字符以内:
resource "awscc_elasticache_serverless_cache" "this" { count = var.enabled && var.use_serverless ? 1 : 0 # 自动截断名称到40字符以内 serverless_cache_name = substr(var.name, 0, 40) description = "${substr(var.name, 0, 40)} ElastiCache Redis Serverless" engine = "redis" major_engine_version = var.engine_version security_group_ids = [aws_security_group.elasticache_serverless_sg.id] subnet_ids = var.subnets depends_on = [aws_security_group.elasticache_serverless_sg] }
3. 手动设置符合长度的名称
直接在myvars.tfvars中指定长度≤40的name值:
enabled = true create_elasticache_subnet_group = true use_serverless = true subnets = ["subnet-dce5d0bb","subnet-922a66db"] vpc_id = "vpc-41425a26" name = "your-short-app-name-here" # 确保长度≤40
额外建议
- 若需要保留完整名称标识,可将完整名称放在
description字段中,该字段无严格长度限制 - ElastiCache名称仅允许使用字母、数字、连字符和下划线,避免使用其他特殊字符
内容的提问来源于stack exchange,提问作者Woodsman
相关产品推荐
相关产品推荐

