main.tf 5.1 KB

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