step_functions.tf 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # ============================================================
  2. # Step Functions State Machine
  3. # ============================================================
  4. resource "aws_iam_role" "sfn" {
  5. name = "${local.prefix}-sfn-role"
  6. assume_role_policy = jsonencode({
  7. Version = "2012-10-17"
  8. Statement = [{
  9. Action = "sts:AssumeRole"
  10. Effect = "Allow"
  11. Principal = { Service = "states.amazonaws.com" }
  12. }]
  13. })
  14. }
  15. resource "aws_iam_role_policy" "sfn" {
  16. name = "${local.prefix}-sfn-policy"
  17. role = aws_iam_role.sfn.id
  18. policy = jsonencode({
  19. Version = "2012-10-17"
  20. Statement = [{
  21. Effect = "Allow"
  22. Action = "lambda:InvokeFunction"
  23. Resource = [
  24. aws_lambda_function.download.arn,
  25. aws_lambda_function.transcribe.arn,
  26. aws_lambda_function.check.arn,
  27. aws_lambda_function.summarize.arn,
  28. aws_lambda_function.notify.arn,
  29. ]
  30. }]
  31. })
  32. }
  33. resource "aws_sfn_state_machine" "pipeline" {
  34. name = "${local.prefix}-pipeline"
  35. role_arn = aws_iam_role.sfn.arn
  36. definition = jsonencode({
  37. Comment = "SP Stream Transcribe Pipeline"
  38. StartAt = "Download"
  39. States = {
  40. Download = {
  41. Type = "Task"
  42. Resource = aws_lambda_function.download.arn
  43. Next = "StartTranscribe"
  44. Retry = [{ ErrorEquals = ["States.ALL"], MaxAttempts = 2, IntervalSeconds = 10 }]
  45. Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }]
  46. }
  47. StartTranscribe = {
  48. Type = "Task"
  49. Resource = aws_lambda_function.transcribe.arn
  50. Next = "WaitForTranscribe"
  51. Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }]
  52. }
  53. WaitForTranscribe = {
  54. Type = "Wait"
  55. Seconds = 30
  56. Next = "CheckTranscribe"
  57. }
  58. CheckTranscribe = {
  59. Type = "Task"
  60. Resource = aws_lambda_function.check.arn
  61. Next = "IsTranscribeComplete"
  62. Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }]
  63. }
  64. IsTranscribeComplete = {
  65. Type = "Choice"
  66. Choices = [{
  67. Variable = "$.transcribe_status"
  68. StringEquals = "COMPLETED"
  69. Next = "Summarize"
  70. }, {
  71. Variable = "$.transcribe_status"
  72. StringEquals = "FAILED"
  73. Next = "NotifyError"
  74. }]
  75. Default = "WaitForTranscribe"
  76. }
  77. Summarize = {
  78. Type = "Task"
  79. Resource = aws_lambda_function.summarize.arn
  80. Next = "Notify"
  81. Retry = [{ ErrorEquals = ["States.ALL"], MaxAttempts = 3, IntervalSeconds = 30, BackoffRate = 2 }]
  82. Catch = [{ ErrorEquals = ["States.ALL"], Next = "NotifyError", ResultPath = "$.error_info" }]
  83. }
  84. Notify = {
  85. Type = "Task"
  86. Resource = aws_lambda_function.notify.arn
  87. End = true
  88. }
  89. NotifyError = {
  90. Type = "Task"
  91. Resource = aws_lambda_function.notify.arn
  92. End = true
  93. }
  94. }
  95. })
  96. }