lambda.tf 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. resources = ["*"]
  33. actions = [
  34. "ec2:DescribeClientVpnConnections",
  35. "ec2:TerminateClientVpnConnections",
  36. "logs:CreateLogStream",
  37. "logs:CreateLogGroup",
  38. "logs:PutLogEvents",
  39. ]
  40. }
  41. }
  42. resource "aws_iam_policy" "lambda_connection_authorization_policy" {
  43. name = "awsclientvpn-connection-handler${var.suffix}"
  44. path = "/lambda/"
  45. policy = data.aws_iam_policy_document.lambda_connection_authorization_policy_doc.json
  46. }
  47. resource "aws_iam_role_policy_attachment" "lambda_connection_authorization_policy_attachment" {
  48. role = aws_iam_role.lambda_connection_authorization.name
  49. policy_arn = aws_iam_policy.lambda_connection_authorization_policy.arn
  50. }
  51. resource "aws_lambda_function" "lambda_connection_authorization" {
  52. function_name = "AWSClientVPN-ConnectionHandler${var.suffix}"
  53. description = "Only allows one concurrent connection"
  54. runtime = "python3.9"
  55. memory_size = 128
  56. publish = true
  57. timeout = 30 # Cannot be changed (maybe can be reduced?)
  58. filename = data.archive_file.lambda_connection_authorization.output_path
  59. role = aws_iam_role.lambda_connection_authorization.arn
  60. handler = "connection_handler_disconnect_multiples.lambda_handler"
  61. source_code_hash = data.archive_file.lambda_connection_authorization.output_base64sha256
  62. environment {
  63. variables = {
  64. LOGLEVEL = var.log_level
  65. MODULELOGLEVEL = var.module_log_level
  66. }
  67. }
  68. tags = merge(local.standard_tags, var.tags)
  69. }
  70. #module "lambda_function" {
  71. # source = "terraform-aws-modules/lambda/aws"
  72. #
  73. # function_name = "AWSClientVPN-ConnectionHandler"
  74. # description = "Determines whether user is allowed to log in."
  75. # handler = "connection_handler.lambda_handler"
  76. # runtime = "python3.9"
  77. # timeout = 30 # Cannot be changes on a connection handler
  78. # publish = true
  79. #
  80. # source_path = "${path.module}/files/connection_authorization/connection_handler.py"
  81. #
  82. # attach_policy_json = true
  83. # policy_json = <<EOF
  84. #{
  85. # "Version": "2012-10-17",
  86. # "Statement": [
  87. # {
  88. # "Effect": "Allow",
  89. # "Action": [
  90. # "ec2:DescribeClientVpnConnections",
  91. # "ec2:TerminateClientVpnConnections"
  92. # ],
  93. # "Resource": "*"
  94. # }
  95. # ]
  96. #}
  97. #EOF
  98. ## The following 3 permissions are autoatically added by the module:
  99. ## "logs:CreateLogStream",
  100. ## "logs:CreateLogGroup",
  101. ## "logs:PutLogEvents",
  102. # tags = merge(local.standard_tags, var.tags)
  103. #}