main.tf 3.7 KB

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