main.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. locals {
  2. # Env variables for bootstrap only; true secrets should be in vault
  3. environment_vars = {
  4. "HTTP_PROXY" = "http://${var.proxy}"
  5. "HTTPS_PROXY" = "http://${var.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 access to Moose"
  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_splunk_in" {
  38. type = "ingress"
  39. from_port = 8089
  40. to_port = 8089
  41. protocol = "tcp"
  42. description = "Moose SH"
  43. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  44. self = "true"
  45. }
  46. resource "aws_lambda_function" "portal_scheduler" {
  47. description = "Used to schedule Portal sync jobs"
  48. filename = "code.zip"
  49. source_code_hash = filebase64sha256("code.zip")
  50. function_name = "portal_scheduler"
  51. role = aws_iam_role.portal_lambda_role.arn
  52. handler = "lambda_function.scheduler"
  53. runtime = "python3.8"
  54. timeout = "180"
  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_event_invoke_config" "portal_scheduler" {
  74. function_name = aws_lambda_function.portal_scheduler.function_name
  75. maximum_retry_attempts = 0
  76. }
  77. resource "aws_lambda_function" "portal_customer_sync" {
  78. description = "Sync data between Splunk and Portal"
  79. filename = "code.zip"
  80. source_code_hash = filebase64sha256("code.zip")
  81. function_name = "portal_customer_sync"
  82. role = aws_iam_role.portal_lambda_role.arn
  83. handler = "lambda_function.handler"
  84. runtime = "python3.8"
  85. timeout = "900"
  86. vpc_config {
  87. subnet_ids = var.subnets
  88. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  89. }
  90. environment {
  91. variables = merge(var.customer_vars, local.environment_vars)
  92. }
  93. tags = merge(var.standard_tags, var.tags)
  94. lifecycle {
  95. # Ignoring changes to the code of the function so that we won't
  96. # overlay changes to the function made outside of terraform. Installing
  97. # new versions of a lambda should not be a terraform-ish action we don't think
  98. ignore_changes = [
  99. last_modified,
  100. source_code_hash
  101. ]
  102. }
  103. }
  104. resource "aws_lambda_function_event_invoke_config" "portal_customer_sync" {
  105. function_name = aws_lambda_function.portal_customer_sync.function_name
  106. maximum_retry_attempts = 0
  107. }
  108. resource "aws_lambda_event_source_mapping" "portal_customer_sync" {
  109. event_source_arn = aws_sqs_queue.sqs_queue.arn
  110. function_name = aws_lambda_function.portal_customer_sync.arn
  111. batch_size = 1
  112. }