# Amazon Transcribe Resources (used in Lab 03: Speech) # Sets up custom vocabularies and language models for speech-to-text. locals { enabled = var.enabled name_prefix = "${var.namespace}-${var.lab}-${var.stage}-${var.name}" } # IAM Role for Transcribe to access S3 resource "aws_iam_role" "transcribe" { count = local.enabled ? 1 : 0 name = "${local.name_prefix}-transcribe-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "transcribe.amazonaws.com" } } ] }) tags = merge(var.tags, { Name = "${local.name_prefix}-transcribe-role" Lab = var.lab }) } # Policy for Transcribe to access S3 resource "aws_iam_role_policy" "transcribe_s3" { count = local.enabled && var.s3_bucket_arn != null ? 1 : 0 name = "${local.name_prefix}-s3-access" role = aws_iam_role.transcribe[0].id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Action = [ "s3:GetObject", "s3:ListBucket", "s3:PutObject" ] Resource = [ var.s3_bucket_arn, "${var.s3_bucket_arn}/*" ] } ] }) } # Custom Vocabulary for domain-specific terms resource "aws_transcribe_vocabulary" "this" { count = local.enabled && var.create_custom_vocabulary ? 1 : 0 vocabulary_name = "${local.name_prefix}-vocabulary" language_code = var.language_code # Use either phrases or vocabulary_file_uri phrases = var.vocabulary_phrases vocabulary_file_uri = var.vocabulary_file_uri tags = merge(var.tags, { Name = "${local.name_prefix}-vocabulary" Lab = var.lab }) } # Custom Vocabulary Filter for filtering unwanted words resource "aws_transcribe_vocabulary_filter" "this" { count = local.enabled && var.create_vocabulary_filter ? 1 : 0 vocabulary_filter_name = "${local.name_prefix}-filter" language_code = var.language_code # Use either words or vocabulary_filter_file_uri words = var.filter_words vocabulary_filter_file_uri = var.filter_file_uri tags = merge(var.tags, { Name = "${local.name_prefix}-filter" Lab = var.lab }) } # Custom Language Model (optional - requires training data) resource "aws_transcribe_language_model" "this" { count = local.enabled && var.create_language_model ? 1 : 0 model_name = "${local.name_prefix}-language-model" base_model_name = var.base_model_name language_code = var.language_code input_data_config { data_access_role_arn = aws_iam_role.transcribe[0].arn s3_uri = var.language_model_training_data_s3_uri tuning_data_s3_uri = var.language_model_tuning_data_s3_uri } tags = merge(var.tags, { Name = "${local.name_prefix}-language-model" Lab = var.lab }) depends_on = [aws_iam_role_policy.transcribe_s3] }