main.tf 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. locals {
  2. environment_vars = {
  3. "HTTP_PROXY" = "http://${var.proxy}"
  4. "HTTPS_PROXY" = "http://${var.proxy}"
  5. "NO_PROXY" = "${var.dns_info["legacy_private"]["zone"]},${var.dns_info["private"]["zone"]}"
  6. "VAULT_HOST" = "vault.${var.dns_info["private"]["zone"]}"
  7. "VAULT_PATH" = "portal/data/lambda_sync_env"
  8. "VERIFY_PORTAL_SSL" = "0"
  9. "PYTHONWARNINGS" = "ignore:Unverified HTTPS request"
  10. "SQS_URL" = "https://sqs.${var.aws_region}.amazonaws.com/${var.aws_account_id}/portal-scheduler.fifo"
  11. }
  12. }
  13. ####
  14. #
  15. #Security Group
  16. #
  17. ####
  18. data "aws_security_group" "typical-host" {
  19. name = "typical-host"
  20. vpc_id = var.vpc_id
  21. }
  22. resource "aws_security_group" "portal_lambda_splunk_sg" {
  23. vpc_id = var.vpc_id
  24. name = "portal-data-sync-lambda-splunk-sg"
  25. description = "Allow Lambda access to Moose"
  26. }
  27. resource "aws_security_group_rule" "portal_lambda_splunk_out" {
  28. type = "egress"
  29. from_port = 8089
  30. to_port = 8089
  31. protocol = "tcp"
  32. cidr_blocks = ["10.0.0.0/8"]
  33. description = "All Splunk SH"
  34. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  35. }
  36. resource "aws_security_group_rule" "portal_lambda_splunk_in" {
  37. type = "ingress"
  38. from_port = 8089
  39. to_port = 8089
  40. protocol = "tcp"
  41. description = "Moose SH"
  42. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  43. self = "true"
  44. }
  45. # Env variables for bootstrap only; true secrets should be in vault
  46. resource "aws_lambda_function" "portal_data_sync" {
  47. description = "Sync data between Splunk and Portal"
  48. filename = "code.zip"
  49. source_code_hash = filebase64sha256("code.zip")
  50. function_name = "portal_data_sync"
  51. role = aws_iam_role.portal_lambda_role.arn
  52. handler = "lambda_function.lambda_handler"
  53. runtime = "python3.7"
  54. timeout = "898"
  55. vpc_config {
  56. subnet_ids = var.subnets
  57. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  58. }
  59. environment {
  60. variables = merge(var.customer_vars, local.environment_vars)
  61. }
  62. tags = merge(var.standard_tags, var.tags)
  63. lifecycle {
  64. # Ignoring changes to the code of the function so that we won't
  65. # overlay changes to the function made outside of terraform. Installing
  66. # new versions of a lambda should not be a terraform-ish action we don't think
  67. ignore_changes = [
  68. last_modified,
  69. source_code_hash
  70. ]
  71. }
  72. }
  73. resource "aws_lambda_function" "portal_scheduler" {
  74. description = "Used to schedule Portal sync jobs"
  75. filename = var.environment == "test" ? "test_portal_data_sync_20211209135332.zip" : "prod_portal_data_sync_20211209135448.zip"
  76. source_code_hash = var.environment == "test" ? filebase64sha256("test_portal_data_sync_20211209135332.zip") : filebase64sha256("prod_portal_data_sync_20211209135448.zip")
  77. #s3_bucket = aws_s3_bucket.bucket.bucket
  78. #s3_key = "code.zip"
  79. function_name = "portal_scheduler"
  80. role = aws_iam_role.portal_lambda_role.arn
  81. handler = "lambda_function.scheduler"
  82. runtime = "python3.8"
  83. timeout = "180"
  84. vpc_config {
  85. subnet_ids = var.subnets
  86. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  87. }
  88. environment {
  89. variables = merge(var.customer_vars, local.environment_vars)
  90. }
  91. tags = merge(var.standard_tags, var.tags)
  92. lifecycle {
  93. # Ignoring changes to the code of the function so that we won't
  94. # overlay changes to the function made outside of terraform. Installing
  95. # new versions of a lambda should not be a terraform-ish action we don't think
  96. ignore_changes = [
  97. last_modified,
  98. source_code_hash
  99. ]
  100. }
  101. }
  102. resource "aws_lambda_function_event_invoke_config" "portal_scheduler" {
  103. function_name = aws_lambda_function.portal_scheduler.function_name
  104. maximum_retry_attempts = 0
  105. }
  106. resource "aws_lambda_function" "portal_customer_sync" {
  107. description = "Sync data between Splunk and Portal"
  108. filename = var.environment == "test" ? "test_portal_data_sync_20211209135332.zip" : "prod_portal_data_sync_20211209135448.zip"
  109. source_code_hash = var.environment == "test" ? filebase64sha256("test_portal_data_sync_20211209135332.zip") : filebase64sha256("prod_portal_data_sync_20211209135448.zip")
  110. function_name = "portal_customer_sync"
  111. role = aws_iam_role.portal_lambda_role.arn
  112. handler = "lambda_function.handler"
  113. runtime = "python3.8"
  114. timeout = "900"
  115. vpc_config {
  116. subnet_ids = var.subnets
  117. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  118. }
  119. environment {
  120. variables = merge(var.customer_vars, local.environment_vars)
  121. }
  122. tags = merge(var.standard_tags, var.tags)
  123. lifecycle {
  124. # Ignoring changes to the code of the function so that we won't
  125. # overlay changes to the function made outside of terraform. Installing
  126. # new versions of a lambda should not be a terraform-ish action we don't think
  127. ignore_changes = [
  128. last_modified,
  129. source_code_hash
  130. ]
  131. }
  132. }
  133. resource "aws_lambda_function_event_invoke_config" "portal_customer_sync" {
  134. function_name = aws_lambda_function.portal_customer_sync.function_name
  135. maximum_retry_attempts = 0
  136. }
  137. resource "aws_lambda_event_source_mapping" "portal_customer_sync" {
  138. event_source_arn = aws_sqs_queue.sqs_queue.arn
  139. function_name = aws_lambda_function.portal_customer_sync.arn
  140. batch_size = 1
  141. }