lambda.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. resource "aws_lambda_event_source_mapping" "sqs_fair_queue" {
  2. event_source_arn = var.source_sqs_arn
  3. function_name = aws_lambda_function.sqs_fair_queue.arn
  4. batch_size = 100
  5. maximum_batching_window_in_seconds = 30 # How long to wait to gather a batch, max: 300
  6. }
  7. # To install prereqs:
  8. # pip install --target ./scripts jsonpath-ng
  9. data "archive_file" "sqs_fair_queue" {
  10. type = "zip"
  11. source_dir = "${path.module}/scripts/"
  12. output_path = "${path.module}/tmp/sqs_fair_queue.zip"
  13. }
  14. resource "aws_lambda_function" "sqs_fair_queue" {
  15. filename = data.archive_file.sqs_fair_queue.output_path
  16. function_name = "sqs_fair_queue_${var.sqs_prefix}"
  17. role = aws_iam_role.sqs_fair_queue.arn
  18. handler = "sqs_fair_queue.lambda_handler"
  19. timeout = var.lambda_timeout
  20. # NOTE: If it can't handle the batch in the time alloted, there is a chance for duplicates.
  21. source_code_hash = data.archive_file.sqs_fair_queue.output_base64sha256
  22. runtime = "python3.9"
  23. environment {
  24. variables = {
  25. #"SOURCE_SQS_ARN" = var.source_sqs_arn Not needed?
  26. "SOURCE_SQS_URL" = var.source_sqs_url
  27. "SQS_PREFIX" = var.sqs_prefix
  28. "NUM_QUEUES" = var.num_queues
  29. "HASH_JSONPATH" = var.hash_jsonpath
  30. }
  31. }
  32. # tfsec recommends tracing as a best practice
  33. tracing_config {
  34. mode = "Active"
  35. }
  36. tags = var.tags
  37. }
  38. resource "aws_lambda_permission" "sqs_fair_queue" {
  39. statement_id = "AllowExecutionFromSQS"
  40. action = "lambda:InvokeFunction"
  41. function_name = aws_lambda_function.sqs_fair_queue.function_name
  42. principal = "sqs.amazonaws.com"
  43. source_arn = var.source_sqs_arn
  44. }
  45. data "aws_iam_policy_document" "sqs_fair_queue" {
  46. statement {
  47. sid = "SQSIngest"
  48. effect = "Allow"
  49. resources = [var.source_sqs_arn]
  50. # tfsec:ignore:aws-iam-no-policy-wildcards Wildcards are fine and useful
  51. actions = ["sqs:*"] # TODO: Nail down
  52. # Probably:
  53. # "sqs:ReceiveMessage",
  54. # "sqs:SendMessage",
  55. # "sqs:GetQueueAttributes"
  56. # "sqs:GetQueueUrl"
  57. }
  58. statement {
  59. sid = "SQSPut"
  60. effect = "Allow"
  61. resources = tolist(aws_sqs_queue.queue[*].arn)
  62. # tfsec:ignore:aws-iam-no-policy-wildcards Wildcards are fine and useful
  63. actions = ["sqs:*"] # TODO: Nail down
  64. }
  65. }
  66. resource "aws_iam_policy" "sqs_fair_queue" {
  67. name = "sqs_fair_queue_${var.sqs_prefix}"
  68. path = "/sqs_fair_queue/"
  69. description = "SQS Fair Queueing Lambda Policy"
  70. policy = data.aws_iam_policy_document.sqs_fair_queue.json
  71. tags = var.tags
  72. }
  73. data "aws_iam_policy_document" "lambda_trust" {
  74. statement {
  75. sid = ""
  76. effect = "Allow"
  77. actions = ["sts:AssumeRole"]
  78. principals {
  79. type = "Service"
  80. identifiers = ["lambda.amazonaws.com"]
  81. }
  82. }
  83. }
  84. resource "aws_iam_role" "sqs_fair_queue" {
  85. name = "sqs_fair_queue_${var.sqs_prefix}"
  86. path = "/sqs_fair_queue/"
  87. assume_role_policy = data.aws_iam_policy_document.lambda_trust.json
  88. tags = var.tags
  89. }
  90. resource "aws_iam_role_policy_attachment" "sqs_fair_queue" {
  91. role = aws_iam_role.sqs_fair_queue.name
  92. policy_arn = aws_iam_policy.sqs_fair_queue.arn
  93. }
  94. resource "aws_iam_role_policy_attachment" "aws_managed_lambda" {
  95. role = aws_iam_role.sqs_fair_queue.name
  96. policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  97. }