-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.tf
More file actions
97 lines (89 loc) · 2.2 KB
/
lambda.tf
File metadata and controls
97 lines (89 loc) · 2.2 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
resource "aws_iam_role" "iam_for_lambda" {
count = var.lambda_enabled ? 1 : 0
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "lambda_policy" {
count = var.lambda_enabled ? 1 : 0
name = "rdsproxylambdapolicy"
description = "Lambda permissions for rds proxy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"rds-db:connect"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_policy_attach" {
count = var.lambda_enabled ? 1 : 0
role = aws_iam_role.iam_for_lambda[0].name
policy_arn = aws_iam_policy.lambda_policy[0].arn
}
resource "aws_lambda_function" "lambda_rds_proxy_test" {
count = var.lambda_enabled ? 1 : 0
function_name = "lambdards"
filename = "./app.zip"
handler = "app.lambda_handler"
role = aws_iam_role.iam_for_lambda[0].arn
source_code_hash = filebase64sha256("./app.zip")
runtime = "python3.8"
timeout = 5
memory_size = 128
environment {
variables = {
ENDPOINT = aws_db_proxy.rds_proxy[0].endpoint
PORT = "3306"
USER = var.username
DBNAME = "mydb"
LOG_LEVEL = "INFO"
POWERTOOLS_SERVICE_NAME = "lambda_rds_proxy"
}
}
vpc_config {
subnet_ids = [aws_subnet.subnet_a[0].id, aws_subnet.subnet_b[0].id]
security_group_ids = [aws_security_group.allow_mysql[0].id]
}
tags = {
Environment = "rdsproxy"
}
}