sqs.tf 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. # checkov:skip=CKV_AWS_109: see tfsec aws-iam-no-policy-wildcard ignore comment
  45. # checkov:skip=CKV_AWS_111: see tfsec aws-iam-no-policy-wildcard ignore comment
  46. statement {
  47. sid = "AllowServices"
  48. effect = "Allow"
  49. principals {
  50. identifiers = ["cloudwatch.amazonaws.com", "sqs.amazonaws.com", "lambda.amazonaws.com"]
  51. type = "Service"
  52. }
  53. actions = [
  54. "kms:GenerateDataKey",
  55. "kms:Decrypt"
  56. ]
  57. # tfsec:ignore:aws-iam-no-policy-wildcards This is read-only access
  58. resources = ["*"]
  59. }
  60. # allow account to modify/manage key
  61. statement {
  62. sid = "AllowThisAccount"
  63. effect = "Allow"
  64. principals {
  65. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  66. type = "AWS"
  67. }
  68. actions = [
  69. "kms:*"
  70. ]
  71. resources = ["*"]
  72. }
  73. }
  74. resource "aws_kms_alias" "sqs_key_alias" {
  75. name = "alias/portal-scheduler-key"
  76. target_key_id = aws_kms_key.sqs_key.key_id
  77. }