Skip to content

Commit 6d59b41

Browse files
lambda-promtail: Various Terraform fixes (#8549)
* lambda-promtail: omit_extra_labels_prefix is a boolean This can be changed without a migration as it hasn't been in a release yet * lambda-promtail: fix typo * lambda-promtail: declare variables as a set * lambda-promtail: add a versions file with provider requirements * lambda-promtail: remove provider block - There's no reason to specify a region here - Empty provider blocks are not needed in terraform for quite some time now (and infact they end up producing a warning about their deprecation) * lambda-promtail: bucket_names can be the empty array Without this, you get an error that `resources` cannot be the empty list * lambda-promtail: convert inline policy to a aws_iam_policy_document
1 parent 3d1ef31 commit 6d59b41

File tree

3 files changed

+69
-65
lines changed

3 files changed

+69
-65
lines changed

tools/lambda-promtail/main.tf

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
provider "aws" {
2-
region = "us-east-2"
3-
}
4-
51
data "aws_region" "current" {}
62

73
resource "aws_iam_role" "iam_for_lambda" {
@@ -21,56 +17,56 @@ resource "aws_iam_role" "iam_for_lambda" {
2117
})
2218
}
2319

24-
resource "aws_iam_role_policy" "logs" {
25-
name = "lambda-logs"
26-
role = aws_iam_role.iam_for_lambda.name
27-
policy = jsonencode({
28-
"Statement" : [
29-
{
30-
"Action" : [
31-
"logs:CreateLogGroup",
32-
"logs:CreateLogStream",
33-
"logs:PutLogEvents",
34-
],
35-
"Effect" : "Allow",
36-
"Resource" : "arn:aws:logs:*:*:*",
37-
},
38-
{
39-
"Action" : [
40-
"s3:GetObject",
41-
],
42-
"Effect" : "Allow",
43-
"Resource" : [
44-
for bucket in toset(var.bucket_names) : "arn:aws:s3:::${bucket}/*"
45-
]
46-
},
47-
{
48-
"Action" : [
49-
"kms:Decrypt",
50-
],
51-
"Effect" : "Allow",
52-
"Resource" : "arn:aws:kms:*:*:*",
53-
},
54-
{
55-
"Action" : [
56-
"ec2:DescribeNetworkInterfaces",
57-
"ec2:CreateNetworkInterface",
58-
"ec2:DeleteNetworkInterface",
59-
"ec2:DescribeInstances",
60-
"ec2:AttachNetworkInterface"
61-
],
62-
"Effect" : "Allow",
63-
"Resource" : "*",
64-
},
65-
{
66-
"Action" : [
67-
"kinesis:*",
68-
],
69-
"Effect" : "Allow",
70-
"Resource" : "*"
71-
}
20+
data "aws_iam_policy_document" "logs" {
21+
statement {
22+
actions = [
23+
"logs:CreateLogGroup",
24+
"logs:CreateLogStream",
25+
"logs:PutLogEvents",
7226
]
73-
})
27+
resources = ["arn:aws:logs:*:*:*"]
28+
}
29+
30+
dynamic "statement" {
31+
for_each = var.bucket_names
32+
content {
33+
actions = [
34+
"s3:GetObject",
35+
]
36+
resources = ["arn:aws:s3:::${statement.value}/*"]
37+
}
38+
}
39+
40+
statement {
41+
actions = [
42+
"kms:Decrypt",
43+
]
44+
resources = ["arn:aws:kms:*:*:*"]
45+
}
46+
47+
statement {
48+
actions = [
49+
"ec2:DescribeNetworkInterfaces",
50+
"ec2:CreateNetworkInterface",
51+
"ec2:DeleteNetworkInterface",
52+
"ec2:DescribeInstances",
53+
"ec2:AttachNetworkInterface",
54+
]
55+
resources = ["*"]
56+
}
57+
58+
statement {
59+
actions = [
60+
"kinesis:*",
61+
]
62+
resources = ["*"]
63+
}
64+
}
65+
66+
resource "aws_iam_role_policy" "logs" {
67+
name = "lambda-logs"
68+
role = aws_iam_role.iam_for_lambda.name
69+
policy = data.aws_iam_policy_document.logs.json
7470
}
7571

7672
data "aws_iam_policy" "lambda_vpc_execution" {
@@ -113,7 +109,7 @@ resource "aws_lambda_function" "lambda_promtail" {
113109
KEEP_STREAM = var.keep_stream
114110
BATCH_SIZE = var.batch_size
115111
EXTRA_LABELS = var.extra_labels
116-
OMIT_EXTRA_LABELS_PREFIX = var.omit_extra_labels_prefix
112+
OMIT_EXTRA_LABELS_PREFIX = var.omit_extra_labels_prefix ? "true" : "false"
117113
TENANT_ID = var.tenant_id
118114
SKIP_TLS_VERIFY = var.skip_tls_verify
119115
PRINT_LOG_LINE = var.print_log_line
@@ -142,7 +138,7 @@ resource "aws_lambda_permission" "lambda_promtail_allow_cloudwatch" {
142138
# However, if you need to provide an actual filter_pattern for a specific log group you should
143139
# copy this block and modify it accordingly.
144140
resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" {
145-
for_each = toset(var.log_group_names)
141+
for_each = var.log_group_names
146142
name = "lambdafunction_logfilter_${each.value}"
147143
log_group_name = each.value
148144
destination_arn = aws_lambda_function.lambda_promtail.arn
@@ -152,15 +148,15 @@ resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" {
152148
}
153149

154150
resource "aws_lambda_permission" "allow-s3-invoke-lambda-promtail" {
155-
for_each = toset(var.bucket_names)
151+
for_each = var.bucket_names
156152
action = "lambda:InvokeFunction"
157153
function_name = aws_lambda_function.lambda_promtail.arn
158154
principal = "s3.amazonaws.com"
159155
source_arn = "arn:aws:s3:::${each.value}"
160156
}
161157

162158
resource "aws_kinesis_stream" "kinesis_stream" {
163-
for_each = toset(var.kinesis_stream_name)
159+
for_each = var.kinesis_stream_name
164160
name = each.value
165161
shard_count = 1
166162
retention_period = 48
@@ -176,15 +172,15 @@ resource "aws_kinesis_stream" "kinesis_stream" {
176172
}
177173

178174
resource "aws_lambda_event_source_mapping" "kinesis_event_source" {
179-
for_each = toset(var.kinesis_stream_name)
175+
for_each = var.kinesis_stream_name
180176
event_source_arn = aws_kinesis_stream.kinesis_stream[each.key].arn
181177
function_name = aws_lambda_function.lambda_promtail.arn
182178
starting_position = "LATEST"
183179
depends_on = [aws_kinesis_stream.kinesis_stream]
184180
}
185181

186182
resource "aws_s3_bucket_notification" "push-to-lambda-promtail" {
187-
for_each = toset(var.bucket_names)
183+
for_each = var.bucket_names
188184
bucket = each.value
189185

190186
lambda_function {

tools/lambda-promtail/variables.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ variable "write_address" {
55
}
66

77
variable "bucket_names" {
8-
type = list(string)
8+
type = set(string)
99
description = "List of S3 bucket names to create Event Notifications for."
1010
default = []
1111
}
1212

1313
variable "log_group_names" {
14-
type = list(string)
14+
type = set(string)
1515
description = "List of CloudWatch Log Group names to create Subscription Filters for."
1616
default = []
1717
}
@@ -67,9 +67,9 @@ variable "extra_labels" {
6767
}
6868

6969
variable "omit_extra_labels_prefix" {
70-
type = string
70+
type = bool
7171
description = "Whether or not to omit the prefix `__extra_` from extra labels defined in the variable `extra_labels`."
72-
default = "false"
72+
default = false
7373
}
7474

7575
variable "batch_size" {
@@ -92,7 +92,7 @@ variable "lambda_vpc_security_groups" {
9292

9393
variable "kms_key_arn" {
9494
type = string
95-
description = "kms key arn for encryp env vars."
95+
description = "kms key arn for encrypting env vars."
9696
default = ""
9797
}
9898

@@ -103,7 +103,7 @@ variable "skip_tls_verify" {
103103
}
104104

105105
variable "kinesis_stream_name" {
106-
type = list(string)
106+
type = set(string)
107107
description = "Enter kinesis name if kinesis stream is configured as event source in lambda."
108108
default = []
109109
}

tools/lambda-promtail/versions.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_version = ">= 0.15"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)