lambda.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.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.zip"
  8. }
  9. resource "aws_iam_role" "lambda_connection_authorization" {
  10. name = "awsclientvpn-connection-handler"
  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"
  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"
  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.lambda_handler"
  61. source_code_hash = data.archive_file.lambda_connection_authorization.output_base64sha256
  62. #environment {
  63. # variables = {
  64. # # TODO: Set logging level
  65. # }
  66. #}
  67. tags = merge(var.standard_tags, var.tags)
  68. }
  69. #module "lambda_function" {
  70. # source = "terraform-aws-modules/lambda/aws"
  71. #
  72. # function_name = "AWSClientVPN-ConnectionHandler"
  73. # description = "Determines whether user is allowed to log in."
  74. # handler = "connection_handler.lambda_handler"
  75. # runtime = "python3.9"
  76. # timeout = 30 # Cannot be changes on a connection handler
  77. # publish = true
  78. #
  79. # source_path = "${path.module}/files/connection_authorization/connection_handler.py"
  80. #
  81. # attach_policy_json = true
  82. # policy_json = <<EOF
  83. #{
  84. # "Version": "2012-10-17",
  85. # "Statement": [
  86. # {
  87. # "Effect": "Allow",
  88. # "Action": [
  89. # "ec2:DescribeClientVpnConnections",
  90. # "ec2:TerminateClientVpnConnections"
  91. # ],
  92. # "Resource": "*"
  93. # }
  94. # ]
  95. #}
  96. #EOF
  97. ## The following 3 permissions are autoatically added by the module:
  98. ## "logs:CreateLogStream",
  99. ## "logs:CreateLogGroup",
  100. ## "logs:PutLogEvents",
  101. # tags = merge(var.standard_tags, var.tags)
  102. #}