sqs.tf 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. resource "aws_sqs_queue" "sqs_queue" {
  2. name = "portal-scheduler.fifo"
  3. visibility_timeout_seconds = 900 # wait 15 minutes; this should always be equal or greater than the lambda timeout or we can get duplicate messages
  4. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  5. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  6. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.sqs_queue_dlq.arn}\",\"maxReceiveCount\":1}"
  7. fifo_queue = true
  8. content_based_deduplication = true
  9. deduplication_scope = "queue"
  10. fifo_throughput_limit = "perQueue"
  11. tags = merge(local.standard_tags, var.tags)
  12. kms_master_key_id = aws_kms_key.sqs_key.id
  13. kms_data_key_reuse_period_seconds = 3600
  14. }
  15. # Dead Letter queue
  16. resource "aws_sqs_queue" "sqs_queue_dlq" {
  17. name = "portal-scheduler-dlq.fifo"
  18. fifo_queue = true
  19. tags = merge(local.standard_tags, var.tags)
  20. kms_master_key_id = aws_kms_key.sqs_key.id
  21. kms_data_key_reuse_period_seconds = 3600
  22. }
  23. data "aws_iam_policy_document" "sqs_policy" {
  24. statement {
  25. effect = "Allow"
  26. principals {
  27. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  28. type = "AWS"
  29. }
  30. actions = ["SQS:*"]
  31. resources = [aws_sqs_queue.sqs_queue.arn]
  32. }
  33. }
  34. resource "aws_sqs_queue_policy" "sqs_policy_attach" {
  35. policy = data.aws_iam_policy_document.sqs_policy.json
  36. queue_url = aws_sqs_queue.sqs_queue.id
  37. }
  38. resource "aws_kms_key" "sqs_key" {
  39. description = "Encryption of SQS queue for portal-scheduler"
  40. policy = data.aws_iam_policy_document.sqs_kms_policy.json
  41. enable_key_rotation = true
  42. }
  43. data "aws_iam_policy_document" "sqs_kms_policy" {
  44. statement {
  45. sid = "AllowServices"
  46. effect = "Allow"
  47. principals {
  48. identifiers = ["cloudwatch.amazonaws.com", "sqs.amazonaws.com", "lambda.amazonaws.com"]
  49. type = "Service"
  50. }
  51. actions = [
  52. "kms:GenerateDataKey",
  53. "kms:Decrypt"
  54. ]
  55. # tfsec:ignore:aws-iam-no-policy-wildcards This is read-only access
  56. resources = ["*"]
  57. }
  58. # allow account to modify/manage key
  59. statement {
  60. sid = "AllowThisAccount"
  61. effect = "Allow"
  62. principals {
  63. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  64. type = "AWS"
  65. }
  66. actions = [
  67. "kms:*"
  68. ]
  69. resources = ["*"]
  70. }
  71. }
  72. resource "aws_kms_alias" "sqs_key_alias" {
  73. name = "alias/portal-scheduler-key"
  74. target_key_id = aws_kms_key.sqs_key.key_id
  75. }