-
Notifications
You must be signed in to change notification settings - Fork 3
/
user-conf.tf
139 lines (134 loc) · 4.23 KB
/
user-conf.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
127
128
129
130
131
132
133
134
135
136
137
138
139
locals {
wg_cognito_user_pool_arn = var.cognito_user_pool_id != null ? (
"arn:aws:cognito-idp:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:userpool/${var.cognito_user_pool_id}" ) : (
try("arn:aws:cognito-idp:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:userpool/${module.wg_cognito_user_pool.id}", "") )
}
resource "aws_iam_role" "create_user_conf" {
name = "${local.name}-create-user-conf"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_policy" "create_user_conf" {
name = "${local.name}-create_user_conf"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter"
],
"Resource": [
"arn:aws:ssm:${local.region}:${(local.account)}:parameter/${var.prefix}*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeNetworkInterfaces",
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:DescribeInstances",
"ec2:AttachNetworkInterface"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"iam:GetGroup"
],
"Resource": "arn:aws:iam::${local.account}:group/${var.wg_group_name}"
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "${module.wg_manage.lambda_function_arn}"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "create_user_conf" {
policy_arn = aws_iam_policy.create_user_conf.arn
role = aws_iam_role.create_user_conf.name
}
resource "aws_iam_policy" "get_config_cognito" {
name = "${local.name}-get_config_cognito"
count = var.users_management_type == "cognito" ? 1 : 0
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cognito-idp:AdminListGroupsForUser"
],
"Resource": "${local.wg_cognito_user_pool_arn}"
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "${module.wg_manage.lambda_function_arn}"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "get_config_cognito" {
count = var.users_management_type == "cognito" ? 1 : 0
policy_arn = aws_iam_policy.get_config_cognito[0].arn
role = aws_iam_role.create_user_conf.name
}
module "create_user_conf" {
source = "terraform-aws-modules/lambda/aws"
version = "2.7.0"
create_package = true
create_role = false
create = true
create_layer = false
create_function = true
publish = true
function_name = "${local.name}-create-user-conf"
runtime = "python3.9"
handler = "app.handler"
memory_size = 512
timeout = 30
lambda_role = aws_iam_role.create_user_conf.arn
package_type = "Zip"
source_path = var.users_management_type == "iam" ? "${path.module}/lambdas/user_conf" : "${path.module}/lambdas/get_user_conf_cognito"
environment_variables = {
WG_SSM_USERS_PREFIX = local.wg_ssm_user_prefix
WG_SSM_CONFIG_PATH = local.wg_ssm_config
IAM_WG_GROUP_NAME = var.wg_group_name
COGNITO_USER_POOL_ID = var.cognito_user_pool_id != null ? var.cognito_user_pool_id : module.wg_cognito_user_pool.id
COGNITO_GROUP_NAME = var.cognito_user_group
WG_MANAGE_FUNCTION_NAME = "${local.name}-wg-manage"
}
}