main.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. data "aws_iam_policy_document" "policy_portal_data_sync_lambda" {
  2. statement {
  3. effect = "Allow"
  4. actions = [
  5. "ec2:CreateNetworkInterface",
  6. "logs:CreateLogStream",
  7. "ec2:DescribeNetworkInterfaces",
  8. "logs:DescribeLogStreams",
  9. "ec2:DeleteNetworkInterface",
  10. "logs:PutRetentionPolicy",
  11. "logs:CreateLogGroup",
  12. "logs:PutLogEvents"
  13. ]
  14. resources = ["*"]
  15. }
  16. }
  17. resource "aws_iam_policy" "policy_portal_data_sync_lambda" {
  18. name = "policy_portal_data_sync_lambda"
  19. path = "/"
  20. policy = data.aws_iam_policy_document.policy_portal_data_sync_lambda.json
  21. description = "IAM policy for portal_data_sync_lambda"
  22. }
  23. resource "aws_iam_role" "portal-lambda-role" {
  24. name = "portal-data-sync-lambda-role"
  25. assume_role_policy = <<EOF
  26. {
  27. "Version": "2012-10-17",
  28. "Statement": [
  29. {
  30. "Sid": "",
  31. "Effect": "Allow",
  32. "Principal": {
  33. "Service": [
  34. "lambda.amazonaws.com"
  35. ]
  36. },
  37. "Action": "sts:AssumeRole"
  38. }
  39. ]
  40. }
  41. EOF
  42. }
  43. resource "aws_iam_role_policy_attachment" "lambda-role" {
  44. role = aws_iam_role.portal-lambda-role.name
  45. policy_arn = aws_iam_policy.policy_portal_data_sync_lambda.arn
  46. }
  47. ####
  48. #
  49. #Security Group
  50. #
  51. ####
  52. data "aws_security_group" "typical-host" {
  53. name = "typical-host"
  54. vpc_id = var.vpc_id
  55. }
  56. resource "aws_security_group" "portal_lambda_sg" {
  57. vpc_id = var.vpc_id
  58. name = "portal-data-sync-lambda-sg"
  59. description = "Allow Lambda access to Portal"
  60. }
  61. resource "aws_security_group_rule" "portal_lambda_https" {
  62. type = "egress"
  63. from_port = 443
  64. to_port = 443
  65. protocol = "tcp"
  66. cidr_blocks = ["0.0.0.0/0"]
  67. description = "Access to Portal"
  68. security_group_id = aws_security_group.portal_lambda_sg.id
  69. }
  70. resource "aws_security_group" "portal_lambda_splunk_sg" {
  71. vpc_id = var.vpc_id
  72. name = "portal-data-sync-lambda-splunk-sg"
  73. description = "Allow Lambda access to Moose"
  74. }
  75. resource "aws_security_group_rule" "portal_lambda_splunk_out" {
  76. type = "egress"
  77. from_port = 8089
  78. to_port = 8089
  79. protocol = "tcp"
  80. cidr_blocks = ["10.0.0.0/8"]
  81. description = "All Splunk SH"
  82. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  83. }
  84. resource "aws_security_group_rule" "portal_lambda_splunk_in" {
  85. type = "ingress"
  86. from_port = 8089
  87. to_port = 8089
  88. protocol = "tcp"
  89. description = "Moose SH"
  90. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  91. self = "true"
  92. }
  93. # Env variables for bootstrap only; true secrets should be in vault
  94. resource "aws_lambda_function" "portal_data_sync" {
  95. description = "Sync data between Splunk and Portal"
  96. filename = "code.zip"
  97. source_code_hash = filebase64sha256("code.zip")
  98. function_name = "portal_data_sync"
  99. role = aws_iam_role.portal-lambda-role.arn
  100. handler = "lambda_function.lambda_handler"
  101. runtime = "python3.7"
  102. timeout = "315"
  103. vpc_config {
  104. subnet_ids = var.subnets
  105. security_group_ids = [ aws_security_group.portal_lambda_sg.id, aws_security_group.portal_lambda_splunk_sg.id ]
  106. }
  107. environment {
  108. variables = {
  109. "CUSTOMER_1_NAME" = "AFS"
  110. "CUSTOMER_2_NAME" = "NGA"
  111. "CUSTOMER_3_NAME" = "MOOSE"
  112. "CUSTOMER_5_NAME" = "MA_COVID"
  113. "CUSTOMER_6_NAME" = "LA_COVID"
  114. "CUSTOMER_7_NAME" = "DC_COVID"
  115. "CUSTOMER_8_NAME" = "NIH"
  116. "HTTP_PROXY" = "http://${var.proxy}"
  117. "HTTPS_PROXY" = "http://${var.proxy}"
  118. "NO_PROXY" = var.dns_info["private"]["zone"]
  119. "VAULT_HOST" = "vault.${var.dns_info["private"]["zone"]}"
  120. "VAULT_PATH" = "portal/data/lambda_sync_env"
  121. }
  122. }
  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. }