lambda.tf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Lambda function to refuse concurrent connections
  2. data "archive_file" "lambda_connection_authorization" {
  3. type = "zip"
  4. source_file = "${path.module}/files/connection_authorization/connection_handler_disconnect_multiples.py"
  5. # 0666 results in "more consistent behavior" according to https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/archive_file
  6. output_file_mode = "0666"
  7. output_path = "${path.module}/files/connection_authorization/connection_handle_with_disconnect.zip"
  8. }
  9. resource "aws_iam_role" "lambda_connection_authorization" {
  10. name = "awsclientvpn-connection-handler${var.suffix}"
  11. path = "/lambda/"
  12. assume_role_policy = <<EOF
  13. {
  14. "Version": "2012-10-17",
  15. "Statement": [
  16. {
  17. "Action": "sts:AssumeRole",
  18. "Principal": {
  19. "Service": "lambda.amazonaws.com"
  20. },
  21. "Effect": "Allow",
  22. "Sid": ""
  23. }
  24. ]
  25. }
  26. EOF
  27. }
  28. data "aws_iam_policy_document" "lambda_connection_authorization_policy_doc" {
  29. statement {
  30. sid = ""
  31. effect = "Allow"
  32. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  33. resources = ["*"]
  34. actions = [
  35. "ec2:DescribeClientVpnConnections",
  36. "ec2:TerminateClientVpnConnections",
  37. "logs:CreateLogStream",
  38. "logs:CreateLogGroup",
  39. "logs:PutLogEvents",
  40. ]
  41. }
  42. }
  43. resource "aws_iam_policy" "lambda_connection_authorization_policy" {
  44. name = "awsclientvpn-connection-handler${var.suffix}"
  45. path = "/lambda/"
  46. policy = data.aws_iam_policy_document.lambda_connection_authorization_policy_doc.json
  47. }
  48. resource "aws_iam_role_policy_attachment" "lambda_connection_authorization_policy_attachment" {
  49. role = aws_iam_role.lambda_connection_authorization.name
  50. policy_arn = aws_iam_policy.lambda_connection_authorization_policy.arn
  51. }
  52. # tfsec:ignore:aws-lambda-enable-tracing We do not enable X-Ray Tracing for Lambda
  53. resource "aws_lambda_function" "lambda_connection_authorization" {
  54. # checkov:skip=CKV_AWS_50: see tfsec ignore X-Ray Tracing
  55. function_name = "AWSClientVPN-ConnectionHandler${var.suffix}"
  56. description = "Only allows one concurrent connection"
  57. runtime = "python3.9"
  58. memory_size = 128
  59. publish = true
  60. timeout = 30 # Cannot be changed (maybe can be reduced?)
  61. filename = data.archive_file.lambda_connection_authorization.output_path
  62. role = aws_iam_role.lambda_connection_authorization.arn
  63. handler = "connection_handler_disconnect_multiples.lambda_handler"
  64. source_code_hash = data.archive_file.lambda_connection_authorization.output_base64sha256
  65. environment {
  66. variables = {
  67. LOGLEVEL = var.log_level
  68. MODULELOGLEVEL = var.module_log_level
  69. }
  70. }
  71. tags = merge(local.standard_tags, var.tags)
  72. }
  73. #module "lambda_function" {
  74. # source = "terraform-aws-modules/lambda/aws"
  75. #
  76. # function_name = "AWSClientVPN-ConnectionHandler"
  77. # description = "Determines whether user is allowed to log in."
  78. # handler = "connection_handler.lambda_handler"
  79. # runtime = "python3.9"
  80. # timeout = 30 # Cannot be changes on a connection handler
  81. # publish = true
  82. #
  83. # source_path = "${path.module}/files/connection_authorization/connection_handler.py"
  84. #
  85. # attach_policy_json = true
  86. # policy_json = <<EOF
  87. #{
  88. # "Version": "2012-10-17",
  89. # "Statement": [
  90. # {
  91. # "Effect": "Allow",
  92. # "Action": [
  93. # "ec2:DescribeClientVpnConnections",
  94. # "ec2:TerminateClientVpnConnections"
  95. # ],
  96. # "Resource": "*"
  97. # }
  98. # ]
  99. #}
  100. #EOF
  101. ## The following 3 permissions are autoatically added by the module:
  102. ## "logs:CreateLogStream",
  103. ## "logs:CreateLogGroup",
  104. ## "logs:PutLogEvents",
  105. # tags = merge(local.standard_tags, var.tags)
  106. #}