# Amazon Comprehend Resources (used in Lab 02: Language) # Sets up IAM roles and optional custom classifiers for NLP. locals { enabled = var.enabled name_prefix = "${var.namespace}-${var.lab}-${var.stage}-${var.name}" } # IAM Role for Comprehend to access S3 (for batch jobs and training) resource "aws_iam_role" "comprehend" { count = local.enabled ? 1 : 0 name = "${local.name_prefix}-comprehend-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "comprehend.amazonaws.com" } } ] }) tags = merge(var.tags, { Name = "${local.name_prefix}-comprehend-role" Lab = var.lab }) } # Policy for Comprehend to access S3 resource "aws_iam_role_policy" "comprehend_s3" { count = local.enabled && var.s3_bucket_arn != null ? 1 : 0 name = "${local.name_prefix}-s3-access" role = aws_iam_role.comprehend[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 Document Classifier (optional - requires training data) resource "aws_comprehend_document_classifier" "this" { count = local.enabled && var.create_document_classifier ? 1 : 0 name = "${local.name_prefix}-classifier" data_access_role_arn = aws_iam_role.comprehend[0].arn language_code = var.language_code mode = var.classifier_mode input_data_config { s3_uri = var.classifier_training_data_s3_uri dynamic "augmented_manifests" { for_each = var.classifier_augmented_manifests != null ? var.classifier_augmented_manifests : [] content { attribute_names = augmented_manifests.value.attribute_names s3_uri = augmented_manifests.value.s3_uri } } } dynamic "output_data_config" { for_each = var.classifier_output_s3_uri != null ? [1] : [] content { s3_uri = var.classifier_output_s3_uri } } tags = merge(var.tags, { Name = "${local.name_prefix}-classifier" Lab = var.lab }) depends_on = [aws_iam_role_policy.comprehend_s3] } # Custom Entity Recognizer (optional - requires training data) resource "aws_comprehend_entity_recognizer" "this" { count = local.enabled && var.create_entity_recognizer ? 1 : 0 name = "${local.name_prefix}-entity-recognizer" data_access_role_arn = aws_iam_role.comprehend[0].arn language_code = var.language_code input_data_config { dynamic "entity_types" { for_each = var.entity_types content { type = entity_types.value } } documents { s3_uri = var.entity_recognizer_documents_s3_uri } dynamic "entity_list" { for_each = var.entity_recognizer_entity_list_s3_uri != null ? [1] : [] content { s3_uri = var.entity_recognizer_entity_list_s3_uri } } } tags = merge(var.tags, { Name = "${local.name_prefix}-entity-recognizer" Lab = var.lab }) lifecycle { precondition { condition = length(var.entity_types) > 0 && var.entity_recognizer_documents_s3_uri != null error_message = "create_entity_recognizer = true needs at least one entry in entity_types and entity_recognizer_documents_s3_uri." } } depends_on = [aws_iam_role_policy.comprehend_s3] }