-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda.tf
More file actions
131 lines (105 loc) · 3.2 KB
/
lambda.tf
File metadata and controls
131 lines (105 loc) · 3.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
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
resource "aws_iam_role" "lambda_role" {
name = local.function_name
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "lambda_vpc_execution" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
role = aws_iam_role.lambda_role.name
}
data "aws_iam_policy_document" "lambda_s3" {
statement {
sid = "FindBucket"
effect = "Allow"
resources = ["arn:aws:s3:::*"] # tfsec:ignore:aws-iam-no-policy-wildcards Need to find the bucket.
actions = [
"s3:GetBucketLocation",
"s3:ListAllMyBuckets",
]
}
statement {
sid = "ReadBucket"
effect = "Allow"
# tfsec:ignore:aws-iam-no-policy-wildcards Need to read all objects in the bucket.
resources = [
local.s3_bucket.arn,
"${local.s3_bucket.arn}/*",
]
actions = [
"s3:GetObject"
]
}
}
resource "aws_iam_policy" "lambda_s3" {
policy = data.aws_iam_policy_document.lambda_s3.json
name = "${local.function_name}-s3"
}
resource "aws_iam_role_policy_attachment" "lambda_s3" {
policy_arn = aws_iam_policy.lambda_s3.arn
role = aws_iam_role.lambda_role.name
}
resource "aws_iam_role_policy_attachment" "xray" {
count = var.enable_tracing ? 1 : 0
policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
role = aws_iam_role.lambda_role.name
}
resource "terraform_data" "build_lambda" {
provisioner "local-exec" {
working_dir = "lambda"
command = "GOARCH=${var.architecture} GOOS=linux go build -o bootstrap main.go"
}
triggers_replace = {
always_run = timestamp()
}
}
data "archive_file" "lambda_zip" {
output_path = "lambda/lambda.zip"
source_file = "${path.module}/lambda/bootstrap"
type = "zip"
depends_on = [
terraform_data.build_lambda
]
}
# tfsec:ignore:aws-lambda-enable-tracing Tracing is optional.
resource "aws_lambda_function" "this" {
function_name = local.function_name
handler = "bootstrap"
role = aws_iam_role.lambda_role.arn
runtime = "provided.al2"
architectures = [var.architecture == "arm64" ? var.architecture : "x86_64"]
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
environment {
variables = {
S3_BUCKET = local.s3_bucket.id
}
}
dynamic "vpc_config" {
for_each = length(var.security_group_ids) > 0 && length(var.subnet_ids) > 0 ? [1] : []
content {
security_group_ids = var.security_group_ids
subnet_ids = var.subnet_ids
}
}
dynamic "tracing_config" {
for_each = var.enable_tracing ? [1] : []
content {
mode = "Active"
}
}
tags = local.tags
}
# tfsec:ignore:aws-cloudwatch-log-group-customer-key Nothing sensistive in the logs so AWS managed key is appropriate.
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/${local.function_name}"
retention_in_days = 7
}