components/terraform/polly/main.tf
5 KB - 元のファイルを表示
# Amazon Polly Resources (used in Lab 03: Speech)
# Sets up custom lexicons for text-to-speech pronunciation.
#
# Note: the AWS Terraform provider does NOT have an aws_polly_lexicon
# resource. Polly lexicons are managed via the AWS API only. We invoke the AWS CLI from
# a null_resource so the lexicon is created/destroyed alongside the rest of
# the lab. Requires the AWS CLI on the apply host (already a workshop
# prerequisite) plus the polly:PutLexicon / polly:DeleteLexicon permissions.
locals {
enabled = var.enabled
name_prefix = "${var.namespace}-${var.lab}-${var.stage}-${var.name}"
# Polly lexicon names must match [0-9A-Za-z]{1,20}: no dashes or
# underscores, at most 20 characters. For example "ai03speechtest".
lexicon_name = substr(replace("${var.namespace}${var.lab}${var.stage}", "/[^0-9A-Za-z]/", ""), 0, 20)
lexicon_content = var.lexicon_content != null ? var.lexicon_content : local.default_lexicon_content
}
# Default lexicon content for AI/tech terms
locals {
default_lexicon_content = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<lexicon version="1.0"
xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
alphabet="ipa"
xml:lang="en-US">
<lexeme>
<grapheme>AWS</grapheme>
<alias>Amazon Web Services</alias>
</lexeme>
<lexeme>
<grapheme>S3</grapheme>
<alias>S 3</alias>
</lexeme>
<lexeme>
<grapheme>EC2</grapheme>
<alias>E C 2</alias>
</lexeme>
<lexeme>
<grapheme>AI</grapheme>
<alias>A I</alias>
</lexeme>
<lexeme>
<grapheme>ML</grapheme>
<alias>M L</alias>
</lexeme>
<lexeme>
<grapheme>NLP</grapheme>
<alias>N L P</alias>
</lexeme>
<lexeme>
<grapheme>API</grapheme>
<alias>A P I</alias>
</lexeme>
</lexicon>
XML
}
# Drop the rendered XML to a build dir so the CLI can pass it via --content file://
resource "local_file" "lexicon" {
count = local.enabled && var.create_lexicon ? 1 : 0
filename = "${path.module}/build/${local.lexicon_name}.pls"
content = local.lexicon_content
}
resource "null_resource" "lexicon" {
count = local.enabled && var.create_lexicon ? 1 : 0
# Recreate when name or content changes.
triggers = {
name = local.lexicon_name
content_sha256 = sha256(local.lexicon_content)
region = var.region
}
provisioner "local-exec" {
when = create
command = "aws polly put-lexicon --name '${local.lexicon_name}' --content file://${local_file.lexicon[0].filename} --region ${var.region}"
}
provisioner "local-exec" {
when = destroy
command = "aws polly delete-lexicon --name '${self.triggers.name}' --region ${self.triggers.region} || true"
}
depends_on = [local_file.lexicon]
}
# S3 bucket for Polly output (async speech synthesis tasks)
resource "aws_s3_bucket" "polly_output" {
count = local.enabled && var.create_output_bucket ? 1 : 0
bucket = "${local.name_prefix}-output"
force_destroy = var.force_destroy_bucket
tags = merge(var.tags, {
Name = "${local.name_prefix}-output"
Lab = var.lab
})
}
resource "aws_s3_bucket_server_side_encryption_configuration" "polly_output" {
count = local.enabled && var.create_output_bucket ? 1 : 0
bucket = aws_s3_bucket.polly_output[0].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_public_access_block" "polly_output" {
count = local.enabled && var.create_output_bucket ? 1 : 0
bucket = aws_s3_bucket.polly_output[0].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# IAM Role for Polly speech synthesis tasks
resource "aws_iam_role" "polly" {
count = local.enabled && var.create_output_bucket ? 1 : 0
name = "${local.name_prefix}-polly-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "polly.amazonaws.com"
}
}
]
})
tags = merge(var.tags, {
Name = "${local.name_prefix}-polly-role"
Lab = var.lab
})
}
# Policy for Polly to write to S3
resource "aws_iam_role_policy" "polly_s3" {
count = local.enabled && var.create_output_bucket ? 1 : 0
name = "${local.name_prefix}-s3-write"
role = aws_iam_role.polly[0].id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:PutObject"
]
Resource = [
aws_s3_bucket.polly_output[0].arn,
"${aws_s3_bucket.polly_output[0].arn}/*"
]
}
]
})
}
# SNS Topic for async speech synthesis notifications
resource "aws_sns_topic" "polly" {
count = local.enabled && var.create_sns_topic ? 1 : 0
name = "${local.name_prefix}-notifications"
tags = merge(var.tags, {
Name = "${local.name_prefix}-notifications"
Lab = var.lab
})
}