main.tf 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_splunk_sg" {
  57. vpc_id = var.vpc_id
  58. name = "portal-data-sync-lambda-splunk-sg"
  59. description = "Allow Lambda access to Moose"
  60. }
  61. resource "aws_security_group_rule" "portal_lambda_splunk_out" {
  62. type = "egress"
  63. from_port = 8089
  64. to_port = 8089
  65. protocol = "tcp"
  66. cidr_blocks = ["10.0.0.0/8"]
  67. description = "All Splunk SH"
  68. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  69. }
  70. resource "aws_security_group_rule" "portal_lambda_splunk_in" {
  71. type = "ingress"
  72. from_port = 8089
  73. to_port = 8089
  74. protocol = "tcp"
  75. description = "Moose SH"
  76. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  77. self = "true"
  78. }
  79. # Env variables for bootstrap only; true secrets should be in vault
  80. resource "aws_lambda_function" "portal_data_sync" {
  81. description = "Sync data between Splunk and Portal"
  82. filename = "code.zip"
  83. source_code_hash = filebase64sha256("code.zip")
  84. function_name = "portal_data_sync"
  85. role = aws_iam_role.portal-lambda-role.arn
  86. handler = "lambda_function.lambda_handler"
  87. runtime = "python3.7"
  88. timeout = "600"
  89. vpc_config {
  90. subnet_ids = var.subnets
  91. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  92. }
  93. environment {
  94. variables = {
  95. "CUSTOMER_1_NAME" = "AFS"
  96. "CUSTOMER_2_NAME" = "NGA"
  97. "CUSTOMER_3_NAME" = "MOOSE"
  98. "CUSTOMER_5_NAME" = "MA_COVID"
  99. "CUSTOMER_6_NAME" = "LA_COVID"
  100. "CUSTOMER_7_NAME" = "DC_COVID"
  101. "CUSTOMER_8_NAME" = "NIH"
  102. "CUSTOMER_9_NAME" = "BAS"
  103. "CUSTOMER_10_NAME" = "FRTIB"
  104. "CUSTOMER_11_NAME" = "DOED"
  105. "CUSTOMER_12_NAME" = "CA_COVID"
  106. "HTTP_PROXY" = "http://${var.proxy}"
  107. "HTTPS_PROXY" = "http://${var.proxy}"
  108. "NO_PROXY" = "${var.dns_info["legacy_private"]["zone"]},${var.dns_info["private"]["zone"]}"
  109. "VAULT_HOST" = "vault.${var.dns_info["private"]["zone"]}"
  110. "VAULT_PATH" = "portal/data/lambda_sync_env"
  111. "VERIFY_PORTAL_SSL" = "0"
  112. }
  113. }
  114. tags = merge(var.standard_tags, var.tags)
  115. lifecycle {
  116. # Ignoring changes to the code of the function so that we won't
  117. # overlay changes to the function made outside of terraform. Installing
  118. # new versions of a lambda should not be a terraform-ish action we don't think
  119. ignore_changes = [
  120. last_modified,
  121. source_code_hash
  122. ]
  123. }
  124. }