main.tf 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. locals {
  2. environment_vars = {
  3. "HTTP_PROXY" = "http://${var.proxy}"
  4. "HTTPS_PROXY" = "http://${var.proxy}"
  5. "NO_PROXY" = "${var.dns_info["legacy_private"]["zone"]},${var.dns_info["private"]["zone"]}"
  6. "VAULT_HOST" = "vault.${var.dns_info["private"]["zone"]}"
  7. "VAULT_PATH" = "portal/data/lambda_sync_env"
  8. "VERIFY_PORTAL_SSL" = "0"
  9. }
  10. }
  11. data "aws_iam_policy_document" "policy_portal_data_sync_lambda" {
  12. statement {
  13. effect = "Allow"
  14. actions = [
  15. "ec2:CreateNetworkInterface",
  16. "logs:CreateLogStream",
  17. "ec2:DescribeNetworkInterfaces",
  18. "logs:DescribeLogStreams",
  19. "ec2:DeleteNetworkInterface",
  20. "logs:PutRetentionPolicy",
  21. "logs:CreateLogGroup",
  22. "logs:PutLogEvents"
  23. ]
  24. resources = ["*"]
  25. }
  26. }
  27. resource "aws_iam_policy" "policy_portal_data_sync_lambda" {
  28. name = "policy_portal_data_sync_lambda"
  29. path = "/"
  30. policy = data.aws_iam_policy_document.policy_portal_data_sync_lambda.json
  31. description = "IAM policy for portal_data_sync_lambda"
  32. }
  33. resource "aws_iam_role" "portal-lambda-role" {
  34. name = "portal-data-sync-lambda-role"
  35. assume_role_policy = <<EOF
  36. {
  37. "Version": "2012-10-17",
  38. "Statement": [
  39. {
  40. "Sid": "",
  41. "Effect": "Allow",
  42. "Principal": {
  43. "Service": [
  44. "lambda.amazonaws.com"
  45. ]
  46. },
  47. "Action": "sts:AssumeRole"
  48. }
  49. ]
  50. }
  51. EOF
  52. }
  53. resource "aws_iam_role_policy_attachment" "lambda-role" {
  54. role = aws_iam_role.portal-lambda-role.name
  55. policy_arn = aws_iam_policy.policy_portal_data_sync_lambda.arn
  56. }
  57. ####
  58. #
  59. #Security Group
  60. #
  61. ####
  62. data "aws_security_group" "typical-host" {
  63. name = "typical-host"
  64. vpc_id = var.vpc_id
  65. }
  66. resource "aws_security_group" "portal_lambda_splunk_sg" {
  67. vpc_id = var.vpc_id
  68. name = "portal-data-sync-lambda-splunk-sg"
  69. description = "Allow Lambda access to Moose"
  70. }
  71. resource "aws_security_group_rule" "portal_lambda_splunk_out" {
  72. type = "egress"
  73. from_port = 8089
  74. to_port = 8089
  75. protocol = "tcp"
  76. cidr_blocks = ["10.0.0.0/8"]
  77. description = "All Splunk SH"
  78. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  79. }
  80. resource "aws_security_group_rule" "portal_lambda_splunk_in" {
  81. type = "ingress"
  82. from_port = 8089
  83. to_port = 8089
  84. protocol = "tcp"
  85. description = "Moose SH"
  86. security_group_id = aws_security_group.portal_lambda_splunk_sg.id
  87. self = "true"
  88. }
  89. # Env variables for bootstrap only; true secrets should be in vault
  90. resource "aws_lambda_function" "portal_data_sync" {
  91. description = "Sync data between Splunk and Portal"
  92. filename = "code.zip"
  93. source_code_hash = filebase64sha256("code.zip")
  94. function_name = "portal_data_sync"
  95. role = aws_iam_role.portal-lambda-role.arn
  96. handler = "lambda_function.lambda_handler"
  97. runtime = "python3.7"
  98. timeout = "720"
  99. vpc_config {
  100. subnet_ids = var.subnets
  101. security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.portal_lambda_splunk_sg.id ]
  102. }
  103. environment {
  104. variables = merge(var.customer_vars, local.environment_vars)
  105. }
  106. tags = merge(var.standard_tags, var.tags)
  107. lifecycle {
  108. # Ignoring changes to the code of the function so that we won't
  109. # overlay changes to the function made outside of terraform. Installing
  110. # new versions of a lambda should not be a terraform-ish action we don't think
  111. ignore_changes = [
  112. last_modified,
  113. source_code_hash
  114. ]
  115. }
  116. }