# ============================================================ # Step Functions State Machine # ============================================================ resource "aws_iam_role" "sfn" { name = "${local.prefix}-sfn-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [{ Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "states.amazonaws.com" } }] }) } resource "aws_iam_role_policy" "sfn" { name = "${local.prefix}-sfn-policy" role = aws_iam_role.sfn.id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = "lambda:InvokeFunction" Resource = [ aws_lambda_function.download.arn, aws_lambda_function.transcribe.arn, aws_lambda_function.check.arn, aws_lambda_function.summarize.arn, aws_lambda_function.notify.arn, ] }] }) } resource "aws_sfn_state_machine" "pipeline" { name = "${local.prefix}-pipeline" role_arn = aws_iam_role.sfn.arn definition = jsonencode({ Comment = "SP Stream Transcribe Pipeline" StartAt = "Download" States = { Download = { Type = "Task" Resource = aws_lambda_function.download.arn Next = "StartTranscribe" Retry = [{ ErrorEquals = ["States.ALL"], MaxAttempts = 2, IntervalSeconds = 10 }] Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }] } StartTranscribe = { Type = "Task" Resource = aws_lambda_function.transcribe.arn Next = "WaitForTranscribe" Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }] } WaitForTranscribe = { Type = "Wait" Seconds = 30 Next = "CheckTranscribe" } CheckTranscribe = { Type = "Task" Resource = aws_lambda_function.check.arn Next = "IsTranscribeComplete" Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }] } IsTranscribeComplete = { Type = "Choice" Choices = [{ Variable = "$.transcribe_status" StringEquals = "COMPLETED" Next = "Summarize" }, { Variable = "$.transcribe_status" StringEquals = "FAILED" Next = "NotifyError" }] Default = "WaitForTranscribe" } Summarize = { Type = "Task" Resource = aws_lambda_function.summarize.arn Next = "Notify" Retry = [{ ErrorEquals = ["States.ALL"], MaxAttempts = 3, IntervalSeconds = 30, BackoffRate = 2 }] Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }] } Notify = { Type = "Task" Resource = aws_lambda_function.notify.arn End = true } NotifyError = { Type = "Task" Resource = aws_lambda_function.notify.arn End = true } } }) }