-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
74 lines (57 loc) · 1.92 KB
/
main.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
resource "aws_lambda_function" "this" {
function_name = var.name
runtime = "nodejs20.x"
handler = "index.handler"
timeout = 5
reserved_concurrent_executions = 3
environment {
variables = {
CLOUDWATCH_LOGS_GROUP_ARN = aws_cloudwatch_log_group.logs.arn
}
}
role = aws_iam_role.lambda.arn
filename = data.archive_file.lambda.output_path
source_code_hash = data.archive_file.lambda.output_base64sha256
tags = var.tags
depends_on = [aws_cloudwatch_log_group.lambda]
}
data "archive_file" "lambda" {
type = "zip"
source_dir = "${path.module}/lambda/src"
output_path = ".terraform/tmp/lambda/${var.name}.zip"
}
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda" {
name = "lambda-${var.name}"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
tags = var.tags
}
resource "aws_iam_role_policy" "lambda_cloudwatch_log_group" {
name = "cloudwatch-log-group"
role = aws_iam_role.lambda.name
policy = data.aws_iam_policy_document.lambda_cloudwatch_log_group.json
}
resource "aws_iam_role_policy" "lambda_cloudwatch_log_group_logs" {
name = "cloudwatch-log-group-logs"
role = aws_iam_role.lambda.name
policy = data.aws_iam_policy_document.logs_cloudwatch_log_group.json
}
resource "aws_iam_role_policy" "lambda_s3_bucket_readonly" {
name = "s3-bucket-readonly"
role = aws_iam_role.lambda.name
policy = data.aws_iam_policy_document.s3_bucket_readonly.json
}
resource "aws_lambda_permission" "s3_bucket_invoke_function" {
function_name = aws_lambda_function.this.arn
action = "lambda:InvokeFunction"
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.this.arn
}