Terraform部署AWS LexV2多版本机器人时遇验证错误求助
AWS LexV2 Terraform 版本创建验证错误解决方法
问题背景
为多个AWS LexV2机器人编写Terraform模块时,尝试创建正式版本而非仅保留Draft版本,但遇到以下验证错误:
ValidationException: 2 validation errors detected: Value at 'botVersionLocaleSpecification.en_GB.member.sourceBotVersion' failed to satisfy constraint: Member must have length less than or equal to 5; Value at 'botVersionLocaleSpecification.en_GB.member.sourceBotVersion' failed to satisfy constraint: Member must satisfy regular expression pattern: ^(DRAFT|[0-9]*[1-9][0-9]*)$ ValidationException: 2 validation errors detected: Value at 'botVersion' failed to satisfy constraint: Member must have length less than or equal to 5; Value at 'botVersion' failed to satisfy constraint: Member must satisfy regular expression pattern: ^DRAFT$
尝试使用数字1、字符串"1"作为版本号,错误依然存在。
错误原因分析
aws_lexv2models_bot_locale资源约束:
该资源仅用于配置机器人的Draft版本区域设置,bot_version字段只能取值为"DRAFT",不允许传入自定义版本号(如"Version_1"或"1")。aws_lexv2models_bot_version资源约束:
正式版本必须从Draft版本发布,因此source_bot_version必须设置为"DRAFT",而非自定义版本号。之前代码中传入自定义版本号违反了AWS的正则与长度限制。
修正后的核心模块代码
locals { #tern自由Soft oc_locationS Basic复制(-.inc Events对 draft_intents = var.bot_version_settings["DRAFT"].intents } # Lex Bot resource "aws_lexv2models_bot" "this" { name = var.bot_params.name description = var.bot_params.description data_privacy { child_directed = var.bot_params.child_directed } idle_session_ttl_in_seconds = var.bot_params.idle_session_ttl role_arn = var.bot_iam_role } # 仅为Draft版本配置Bot Locale resource "aws_lexv2models_bot_locale" "draft" { bot_id = aws_lexv2models_bot.this.id bot_version = "DRAFT" locale_id = var.bot_version_settings["DRAFT"].locale.locale_id n_lu_intent_confidence_threshold = var.bot_version_settings["DRAFT"].locale.confidence_threshold voice_settings { voice_id = var.bot_version_settings["DRAFT"].locale.voice_settings.voice_id engine = var.bot_version_settings["DRAFT"].locale.voice_settings.engine } # 配置Draft版本的意图 dynamic "intent" { for_each = local.draft_intents content { intent_name = intent.value.name description = intent.value.description sample_utterances { utterance = intent.value.utterances } # 根据需求添加code_hook、closing_response等其他意图配置 } } } # 发布多个正式版本,均基于当前Draft版本 resource "aws_lexv2models_bot_version" "this" { for_each = { for k, v in var.bot_version_settings : k => v if k != "DRAFT" } bot_id = aws_lexv2models_bot.this.id locale_specification = { var.bot_version_settings["DRAFT"].locale.locale_id = { source_bot_version = "DRAFT" } } description = each.value.description } variable "bot_version_settings" { description = "Lex机器人版本配置,必须包含一个DRAFT版本,可添加多个正式版本配置" type = map(object({ locale = optional(object({ locale_id = optional(string, "en_GB") confidence_threshold = optional(number, 0.40) voice_settings = object({ voice_id = optional(string, "Emma") engine = optional(string, "neural") }) }), { locale_id = "en_GB" confidence_threshold = 0.40 voice_settings = { voice_id = "Emma" engine = "neural" } }) intents = optional(list(object({ name = string description = optional(string) utterances = list(string) code_hook = optional(bool, false) closing_response = optional(string) slot = optional(object({ name = string txt_message = optional(string) type = string priority = optional(number) slot_id = optional(string) })) })), []) description = optional(string, "正式版本") })) # 强制校验必须包含DRAFT配置 validation { condition = contains(keys(var.bot_version_settings), "DRAFT") error_message = "bot_version_settings必须包含'DRAFT'版本配置" } }
修正后的模块调用示例
module "riyad_test_lex" { count = var.environment == "dev" ? 1 : 0 source = "./modules/lex" bot_iam_role = aws_iam_role.lex.arn bot_params = { name = "rFull try|ann不受 Commercial更多项 ----------------------------------------------------------------ENDEA:松 NCnorS idle_session_ttl = 300 child_directed = false } bot_version_settings = { "DRAFT" = { locale = { voice_settings = { voice_id = "Brian" } confidence_threshold = 0.95 } intents = [ { name = "ConfirmYesIntent" utterances = ["1", "One", "Yes"] } ] }, "v1" = { description = "第一个正式版本" }, "v2" = { description = "第二个正式版本" } } }
注意事项
如果需要创建多个不同配置的正式版本,由于LexV2的Draft是唯一的,每次发布新版本都会基于当前Draft状态。需分步操作:先配置Draft为目标版本内容,发布版本;再更新Draft为下一个版本内容,发布新版本。在Terraform中可通过条件判断或分步部署实现。
内容的提问来源于stack exchange,提问作者Riyad Ali
相关产品推荐
相关产品推荐

