-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.tf
More file actions
132 lines (108 loc) · 3.69 KB
/
lambda.tf
File metadata and controls
132 lines (108 loc) · 3.69 KB
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
############################################
# IAM ROLE
############################################
resource "aws_iam_role" "lambda_exec_role" {
name = "tc-lambda-identification-auth-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow",
Principal = { Service = "lambda.amazonaws.com" },
Action = "sts:AssumeRole"
}]
})
}
############################################
# IAM POLICY - COGNITO
############################################
resource "aws_iam_policy" "lambda_cognito_policy" {
name = "lambda-cognito-policy"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"cognito-idp:AdminCreateUser",
"cognito-idp:AdminSetUserPassword",
"cognito-idp:AdminInitiateAuth",
"cognito-idp:AdminGetUser"
],
Resource = aws_cognito_user_pool.pool.arn
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_cognito_attach" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = aws_iam_policy.lambda_cognito_policy.arn
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "lambda_vpc_access" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
############################################
# SECURITY GROUP - LAMBDA
############################################
resource "aws_security_group" "id_lambda" {
name = "tc-id-lambda-sg"
description = "Security group for Lambda ID function"
vpc_id = data.terraform_remote_state.infra.outputs.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
############################################
# LAMBDA FUNCTION
############################################
resource "aws_lambda_function" "id_lambda" {
function_name = "lambda-identification-auth"
depends_on = [aws_iam_role_policy_attachment.lambda_logs]
role = aws_iam_role.lambda_exec_role.arn
handler = "tech.buildrun.lambda.Handler::handleRequest"
runtime = "java17"
timeout = 30
filename = var.lambda_jar_path
source_code_hash = filebase64sha256(var.lambda_jar_path)
environment {
variables = {
DB_URL = local.jdbc_url
USER_POOL_ID = aws_cognito_user_pool.pool.id
CLIENT_ID = aws_cognito_user_pool_client.client.id
}
}
vpc_config {
subnet_ids = data.terraform_remote_state.infra.outputs.private_subnets
security_group_ids = [aws_security_group.id_lambda.id]
}
tags = var.tags
}
############################################
# API GATEWAY PERMISSION
############################################
resource "aws_lambda_permission" "apigw_invoke_lambda" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.id_lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${data.aws_apigatewayv2_api.hackathon_api.execution_arn}/*/*"
}
############################################
# RULE - LAMBDA -> RDS
############################################
resource "aws_security_group_rule" "id_lambda_to_rds" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "tcp"
source_security_group_id = aws_security_group.id_lambda.id
security_group_id = data.aws_security_group.rds.id
}