-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·152 lines (118 loc) · 3.93 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
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
146
147
148
149
150
151
152
locals {
name = coalesce(module.this.name, var.name, "uploader")
artifact_src_local_path = var.artifact_src_local_path
artifact_src_bucket_arn = coalesce(var.artifact_src_bucket_arn, var.artifact_dst_bucket_arn)
artifact_src_bucket_name = data.aws_arn.artifact_src_bucket.resource
artifact_src_bucket_path = trim(var.artifact_src_bucket_path, "/")
artifact_dst_bucket_arn = var.artifact_dst_bucket_arn
artifact_dst_bucket_name = data.aws_arn.artifact_dst_bucket.resource
artifact_dst_bucket_path = trimprefix(var.artifact_dst_bucket_path, "/")
aws_partition = data.aws_partition.current.partition
}
data "aws_partition" "current" {}
data "aws_arn" "artifact_src_bucket" {
arn = local.artifact_src_bucket_arn
}
data "aws_arn" "artifact_dst_bucket" {
arn = local.artifact_dst_bucket_arn
}
# ================================================================= artifact ===
resource "aws_s3_object" "artifact" {
count = module.this.enabled ? 1 : 0
bucket = local.artifact_src_bucket_name
key = local.artifact_src_bucket_path
source = local.artifact_src_local_path
}
# tflint-ignore: terraform_unused_declarations
data "aws_lambda_invocation" "artifact" {
count = module.this.enabled ? 1 : 0
function_name = aws_lambda_function.this[0].function_name
input = jsonencode({
artifact_src_bucket_name = local.artifact_src_bucket_name
artifact_src_bucket_path = local.artifact_src_bucket_path
artifact_dst_bucket_name = local.artifact_dst_bucket_name
artifact_dst_bucket_path = local.artifact_dst_bucket_path
})
depends_on = [
aws_lambda_function.this,
aws_s3_object.artifact,
]
}
# ================================================================= uploader ===
module "uploader_label" {
source = "cloudposse/label/null"
version = "0.25.0"
name = local.name
attributes = local.name != "uploader" ? ["uploader"] : []
context = module.this.context
}
data "archive_file" "uploader" {
count = module.uploader_label.enabled ? 1 : 0
type = "zip"
source_file = "${path.module}/assets/uploader.py"
output_path = "${path.module}/dist/uploader.zip"
}
resource "aws_lambda_function" "this" {
count = module.uploader_label.enabled ? 1 : 0
function_name = module.uploader_label.id
filename = data.archive_file.uploader[0].output_path
source_code_hash = data.archive_file.uploader[0].output_base64sha256
handler = "uploader.lambda_handler"
runtime = "python3.8"
timeout = 90
role = aws_iam_role.this[0].arn
layers = []
tags = module.uploader_label.tags
depends_on = [
aws_cloudwatch_log_group.this,
]
}
resource "aws_cloudwatch_log_group" "this" {
count = module.uploader_label.enabled ? 1 : 0
name = "/aws/lambda/${module.uploader_label.id}"
retention_in_days = 90
tags = module.uploader_label.tags
}
# ---------------------------------------------------------------------- iam ---
resource "aws_iam_role" "this" {
count = module.uploader_label.enabled ? 1 : 0
name = module.uploader_label.id
description = ""
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow"
Principal = { "Service" : "lambda.amazonaws.com" }
Action = ["sts:AssumeRole", "sts:TagSession"]
}]
})
managed_policy_arns = [
"arn:${local.aws_partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
inline_policy {
name = "access"
policy = data.aws_iam_policy_document.this[0].json
}
tags = module.uploader_label.tags
}
data "aws_iam_policy_document" "this" {
count = module.uploader_label.enabled ? 1 : 0
statement {
effect = "Allow"
actions = [
"s3:GetObject",
]
resources = [
"${local.artifact_src_bucket_arn}/*"
]
}
statement {
effect = "Allow"
actions = [
"s3:PutObject",
]
resources = [
"${local.artifact_dst_bucket_arn}/*"
]
}
}