| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # ============================================================
- # 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
- }
- }
- })
- }
|