-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
126 lines (104 loc) · 3 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
resource "random_string" "this" {
length = 8
special = false
upper = false
}
data "aws_s3_bucket" "artifacts" {
bucket = var.artifacts_bucket_name
}
data "aws_iam_policy_document" "lambda" {
# need to make this less permissive
statement {
sid = "AllowReadSSMParam"
actions = ["ssm:GetParameter*"]
resources = [
"arn:aws:ssm:*:${data.aws_caller_identity.current.account_id}:parameter${var.svc_user_pwd_ssm_key}"
]
}
statement {
sid = "AllowS3Write"
actions = [
"s3:Get*",
"s3:List*",
"s3:PutObject"
]
resources = [data.aws_s3_bucket.artifacts.arn]
}
}
data "aws_subnet_ids" "private" {
vpc_id = var.vpc_id
filter {
name = "tag:Network"
values = ["Private"]
}
}
resource "aws_security_group" "lambda" {
name = "${var.project_name}-ldap-query-sg-${random_string.this.result}"
description = "SG used by the ${var.project_name}-ldap-query-sg lambda function"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
module "lambda_layer" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git?ref=v7.15.0"
create_layer = true
description = "Contains python-ldap and its dependencies"
layer_name = "python-ldap-${random_string.this.result}"
build_in_docker = true
docker_file = "${path.module}/layer/Dockerfile"
docker_image = "python-ldap-${random_string.this.result}"
runtime = "python3"
source_path = [
{
pip_requirements = "${path.module}/layer/requirements.txt"
prefix_in_zip = "python"
}
]
compatible_runtimes = [
"python3.7",
"python3.8"
]
}
locals {
default_hands_off = [
"Administrator",
"Guest",
"AWS_WorkSpacesAdmin",
"AWS_WorkMail_Consul",
"krbtgt"
]
hands_off_accounts = concat(local.default_hands_off, var.additional_hands_off_accounts)
}
module "lambda" {
source = "github.com/claranet/terraform-aws-lambda"
function_name = "ldap-maintainer-${random_string.this.result}"
description = "Performs ldap query tasks"
handler = "lambda.handler"
runtime = "python3.7"
timeout = 300
source_path = "${abspath(path.module)}/lambda"
policy = data.aws_iam_policy_document.lambda
environment = {
variables = {
LDAPS_URL = var.ldaps_url
DOMAIN_BASE = var.domain_base_dn
SVC_USER_DN = var.svc_user_dn
SSM_KEY = var.svc_user_pwd_ssm_key
LOG_LEVEL = var.log_level
ARTIFACTS_BUCKET = var.artifacts_bucket_name
HANDS_OFF_ACCOUNTS = jsonencode(local.hands_off_accounts)
DAYS_SINCE_PWDLASTSET = var.days_since_pwdlastset
}
}
vpc_config = {
subnet_ids = data.aws_subnet_ids.private.ids
security_group_ids = [aws_security_group.lambda.id]
}
layers = [module.lambda_layer.this_lambda_layer_arn]
}