main.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #
  16. #Security Group
  17. #
  18. ####
  19. data "aws_security_group" "typical-host" {
  20. name = "typical-host"
  21. vpc_id = var.vpc_id
  22. }
  23. resource "aws_security_group" "portal_lambda_splunk_sg" {
  24. vpc_id = var.vpc_id
  25. name = "portal-data-sync-lambda-splunk-sg"
  26. description = "Allow Lambda network access"
  27. }
  28. resource "aws_security_group_rule" "portal_lambda_splunk_out" {
  29. type = "egress"
  30. from_port = 8089
  31. to_port = 8089
  32. protocol = "tcp"
  33. cidr_blocks = ["10.0.0.0/8"]
  34. description = "All Splunk SH"
  35. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  36. }
  37. resource "aws_security_group_rule" "portal_lambda_phantom_out" {
  38. type = "egress"
  39. from_port = 443
  40. to_port = 443
  41. protocol = "tcp"
  42. cidr_blocks = local.cidr_map["vpc-private-services"]
  43. description = "Allow Lambda to connect to all server APIs in private-services"
  44. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  45. }
  46. resource "aws_security_group_rule" "portal_lambda_splunk_in" {
  47. type = "ingress"
  48. from_port = 8089
  49. to_port = 8089
  50. protocol = "tcp"
  51. description = "Moose SH"
  52. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  53. self = "true"
  54. }
  55. resource "aws_lambda_function" "portal_scheduler" {
  56. description = "Used to schedule Portal sync jobs"
  57. filename = "code.zip"
  58. source_code_hash = filebase64sha256("code.zip")
  59. function_name = "portal_scheduler"
  60. role = aws_iam_role.portal_lambda_role.arn
  61. handler = "lambda_function.scheduler"
  62. runtime = "python3.8"
  63. timeout = "180"
  64. vpc_config {
  65. subnet_ids = var.subnets
  66. security_group_ids = [data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id]
  67. }
  68. environment {
  69. variables = merge(var.customer_vars, local.environment_vars)
  70. }
  71. tags = merge(local.standard_tags, var.tags)
  72. lifecycle {
  73. # Ignoring changes to the code of the function so that we won't
  74. # overlay changes to the function made outside of terraform. Installing
  75. # new versions of a lambda should not be a terraform-ish action we don't think
  76. ignore_changes = [
  77. last_modified,
  78. source_code_hash
  79. ]
  80. }
  81. }
  82. resource "aws_lambda_function_event_invoke_config" "portal_scheduler" {
  83. function_name = aws_lambda_function.portal_scheduler.function_name
  84. maximum_retry_attempts = 0
  85. }
  86. resource "aws_lambda_function" "portal_customer_sync" {
  87. description = "Sync data between Splunk and Portal"
  88. filename = "code.zip"
  89. source_code_hash = filebase64sha256("code.zip")
  90. function_name = "portal_customer_sync"
  91. role = aws_iam_role.portal_lambda_role.arn
  92. handler = "lambda_function.handler"
  93. runtime = "python3.8"
  94. timeout = "900"
  95. memory_size = "1024"
  96. vpc_config {
  97. subnet_ids = var.subnets
  98. security_group_ids = [data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id]
  99. }
  100. environment {
  101. variables = merge(var.customer_vars, local.environment_vars)
  102. }
  103. tags = merge(local.standard_tags, var.tags)
  104. lifecycle {
  105. # Ignoring changes to the code of the function so that we won't
  106. # overlay changes to the function made outside of terraform. Installing
  107. # new versions of a lambda should not be a terraform-ish action we don't think
  108. ignore_changes = [
  109. last_modified,
  110. source_code_hash
  111. ]
  112. }
  113. }
  114. resource "aws_lambda_function_event_invoke_config" "portal_customer_sync" {
  115. function_name = aws_lambda_function.portal_customer_sync.function_name
  116. maximum_retry_attempts = 0
  117. }
  118. resource "aws_lambda_event_source_mapping" "portal_customer_sync" {
  119. event_source_arn = aws_sqs_queue.sqs_queue.arn
  120. function_name = aws_lambda_function.portal_customer_sync.arn
  121. batch_size = 1
  122. }