-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlambda.tf
145 lines (131 loc) · 4.89 KB
/
lambda.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
140
141
142
143
144
145
resource "aws_iam_role" "coepi_lambda_backend_role" {
name = "iam_for_coepi_lambda_${var.region}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
locals {
jarfile = "../build/libs/coepi-backend-kotlin-1.0.0-all.jar"
}
resource "aws_lambda_function" "coepi_lambda" {
filename = local.jarfile
function_name = "CoEpiServerLambda"
role = aws_iam_role.coepi_lambda_backend_role.arn
handler = "org.coepi.api.CoEpiHandler::handleRequest"
source_code_hash = filebase64sha256(local.jarfile)
memory_size = 512
timeout = 10
runtime = "java11"
}
resource "aws_lambda_function" "tcn_lambda" {
filename = local.jarfile
function_name = "TCNServerLambda"
role = aws_iam_role.coepi_lambda_backend_role.arn
handler = "org.coepi.api.v4.TCNCloudAPIHandler"
source_code_hash = filebase64sha256(local.jarfile)
memory_size = 512
timeout = 10
runtime = "java11"
}
//TODO these policy perms could be tightened.
resource "aws_iam_policy" "lambda_dynamodb_access" {
name = "coepi_lambda_dynamodb_policy_${var.region}"
path = "/"
description = "IAM policy for DynamoDB access from lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:DescribeContributorInsights",
"dynamodb:RestoreTableToPointInTime",
"dynamodb:ListTagsOfResource",
"dynamodb:UpdateContributorInsights",
"dynamodb:UpdateContinuousBackups",
"dynamodb:TagResource",
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:DescribeContinuousBackups",
"dynamodb:BatchGetItem",
"dynamodb:UpdateTimeToLive",
"dynamodb:BatchWriteItem",
"dynamodb:ConditionCheckItem",
"dynamodb:UntagResource",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:DescribeStream",
"dynamodb:UpdateItem",
"dynamodb:DescribeTimeToLive",
"dynamodb:DescribeGlobalTableSettings",
"dynamodb:GetShardIterator",
"dynamodb:DescribeGlobalTable",
"dynamodb:RestoreTableFromBackup",
"dynamodb:DescribeBackup",
"dynamodb:GetRecords",
"dynamodb:DescribeTableReplicaAutoScaling"
],
"Resource": [
"${aws_dynamodb_table.coapi-dynamodb-table.arn}",
"${aws_dynamodb_table.tcn-dynamodb-table.arn}"
]
},
{
"Effect": "Allow",
"Action": [
"dynamodb:DescribeReservedCapacityOfferings",
"dynamodb:DescribeReservedCapacity",
"dynamodb:PurchaseReservedCapacityOfferings",
"dynamodb:DescribeLimits",
"dynamodb:ListStreams"
],
"Resource": [
"${aws_dynamodb_table.coapi-dynamodb-table.arn}",
"${aws_dynamodb_table.tcn-dynamodb-table.arn}"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_dynamodb_policy_attachment" {
role = aws_iam_role.coepi_lambda_backend_role.name
policy_arn = aws_iam_policy.lambda_dynamodb_access.arn
}
resource "aws_iam_role_policy_attachment" "lambda_cloudwatch_policy_attachment" {
role = aws_iam_role.coepi_lambda_backend_role.name
policy_arn = var.cloudwatch_policy_arn
}
resource "aws_lambda_permission" "lambda_apigateway" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.coepi_lambda.function_name
principal = "apigateway.amazonaws.com"
# The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.coepi_api_gateway.execution_arn}/*/*"
}
resource "aws_lambda_permission" "tcn_lambda_apigateway" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.tcn_lambda.function_name
principal = "apigateway.amazonaws.com"
# The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.tcn_api_gateway.execution_arn}/*/*"
}