components/terraform/lex/main.tf
6 KB - View raw file
# Amazon Lex V2 Resources (used in Lab 04: Conversational)
# Sets up a conversational AI chatbot with intents and slots.
locals {
enabled = var.enabled
name_prefix = "${var.namespace}-${var.lab}-${var.stage}-${var.name}"
}
# IAM Role for Lex Bot
resource "aws_iam_role" "lex" {
count = local.enabled ? 1 : 0
name = "${local.name_prefix}-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lexv2.amazonaws.com"
}
}
]
})
tags = merge(var.tags, {
Name = "${local.name_prefix}-role"
Lab = var.lab
})
}
# Policy for Lex to use Polly and CloudWatch
resource "aws_iam_role_policy" "lex" {
count = local.enabled ? 1 : 0
name = "${local.name_prefix}-policy"
role = aws_iam_role.lex[0].id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"polly:SynthesizeSpeech"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:*:*:log-group:/aws/lex/*"
}
]
})
}
# Lex V2 Bot
resource "aws_lexv2models_bot" "this" {
count = local.enabled ? 1 : 0
name = replace(local.name_prefix, "-", "_")
description = var.bot_description
role_arn = aws_iam_role.lex[0].arn
data_privacy {
child_directed = var.data_privacy.child_directed
}
idle_session_ttl_in_seconds = var.idle_session_ttl_in_seconds
tags = merge(var.tags, {
Name = local.name_prefix
Lab = var.lab
})
}
# Bot Locale
resource "aws_lexv2models_bot_locale" "this" {
count = local.enabled ? 1 : 0
bot_id = aws_lexv2models_bot.this[0].id
bot_version = "DRAFT"
locale_id = var.locale_id
n_lu_intent_confidence_threshold = var.nlu_confidence_threshold
voice_settings {
voice_id = var.voice_id
engine = var.voice_engine
}
}
# Greeting Intent
resource "aws_lexv2models_intent" "greeting" {
count = local.enabled && var.create_sample_intents ? 1 : 0
bot_id = aws_lexv2models_bot.this[0].id
bot_version = aws_lexv2models_bot_locale.this[0].bot_version
locale_id = aws_lexv2models_bot_locale.this[0].locale_id
name = "Greeting"
description = "Handles user greetings"
sample_utterance {
utterance = "Hello"
}
sample_utterance {
utterance = "Hi"
}
sample_utterance {
utterance = "Hey there"
}
sample_utterance {
utterance = "Good morning"
}
sample_utterance {
utterance = "Good afternoon"
}
fulfillment_code_hook {
enabled = false
}
closing_setting {
active = true
closing_response {
message_group {
message {
plain_text_message {
value = "Hello! Welcome to the AI Lab assistant. How can I help you today?"
}
}
}
}
}
}
# Help Intent
resource "aws_lexv2models_intent" "help" {
count = local.enabled && var.create_sample_intents ? 1 : 0
bot_id = aws_lexv2models_bot.this[0].id
bot_version = aws_lexv2models_bot_locale.this[0].bot_version
locale_id = aws_lexv2models_bot_locale.this[0].locale_id
name = "Help"
description = "Provides help information"
sample_utterance {
utterance = "Help"
}
sample_utterance {
utterance = "I need help"
}
sample_utterance {
utterance = "What can you do"
}
sample_utterance {
utterance = "What are your capabilities"
}
sample_utterance {
utterance = "Help me"
}
fulfillment_code_hook {
enabled = false
}
closing_setting {
active = true
closing_response {
message_group {
message {
plain_text_message {
value = "I can help you learn about AWS AI services. Try asking about Rekognition, Textract, Comprehend, Translate, Transcribe, Polly, or Bedrock!"
}
}
}
}
}
}
# Service Info Intent. It has no slots: the utterances are plain phrases, so
# the bot builds without a custom slot type. Adding a ServiceName slot is an
# optional exercise in the Lab 04 guide.
resource "aws_lexv2models_intent" "service_info" {
count = local.enabled && var.create_sample_intents ? 1 : 0
bot_id = aws_lexv2models_bot.this[0].id
bot_version = aws_lexv2models_bot_locale.this[0].bot_version
locale_id = aws_lexv2models_bot_locale.this[0].locale_id
name = "ServiceInfo"
description = "Provides information about AWS AI services"
sample_utterance {
utterance = "Tell me about AWS AI services"
}
sample_utterance {
utterance = "What AI services does AWS offer"
}
sample_utterance {
utterance = "Explain the AWS AI services"
}
sample_utterance {
utterance = "Which AWS AI service should I use"
}
sample_utterance {
utterance = "I want to learn about AI on AWS"
}
fulfillment_code_hook {
enabled = false
}
closing_setting {
active = true
closing_response {
message_group {
message {
plain_text_message {
value = "AWS offers vision (Rekognition, Textract), language (Comprehend, Translate), speech (Transcribe, Polly), chatbots (Lex), and generative AI (Bedrock). The hands-on labs in this project cover each one."
}
}
}
}
}
}
# FallbackIntent is automatically created by Lex
# CloudWatch Log Group for conversation logs
resource "aws_cloudwatch_log_group" "lex" {
count = local.enabled ? 1 : 0
name = "/aws/lex/${replace(local.name_prefix, "-", "_")}"
retention_in_days = 7
tags = merge(var.tags, {
Name = "${local.name_prefix}-logs"
Lab = var.lab
})
}