-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.tf
83 lines (71 loc) · 2.13 KB
/
function.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
resource "aws_lambda_function" "jokester" {
function_name = "${local.application}${var.resource_suffix}"
filename = data.archive_file.jokester_code.output_path
runtime = "nodejs20.x"
handler = "index.handler"
source_code_hash = data.archive_file.jokester_code.output_base64sha256
role = aws_iam_role.lambda_exec.arn
environment {
variables = {
JOKE_TABLE_SUFFIX = var.resource_suffix
}
}
tags = {
Application = local.application
Environment = var.environment
}
}
data "archive_file" "jokester_code" {
type = "zip"
source_dir = "${path.module}/../unit-tests"
output_path = "/tmp/${local.application}-code${var.resource_suffix}.zip"
}
resource "aws_cloudwatch_log_group" "jokester" {
name = "/aws/lambda/${aws_lambda_function.jokester.function_name}"
retention_in_days = 30
tags = {
Application = local.application
Environment = var.environment
}
}
resource "aws_iam_role" "lambda_exec" {
name = "${local.application}-exec${var.resource_suffix}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
tags = {
Application = local.application
Environment = var.environment
}
}
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_policy" "read_jokes_db_table" {
name = "${local.application}-jokes${var.resource_suffix}"
policy = data.aws_iam_policy_document.access_jokes_table.json
}
data "aws_iam_policy_document" "access_jokes_table" {
statement {
actions = [
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:BatchGetItem",
"dynamodb:GetItem",
]
resources = [aws_dynamodb_table.jokes.arn]
}
}
resource "aws_iam_role_policy_attachment" "dynamodb_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = aws_iam_policy.read_jokes_db_table.arn
}