Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 9a730f9

Browse files
committed
0.2.5 release
1 parent d790600 commit 9a730f9

File tree

4 files changed

+65
-91
lines changed

4 files changed

+65
-91
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This Lambda Function forwards subject & body of SNS messages to CloudWatch Log G
3030
``` ruby
3131
module "sns_logger" {
3232
source = "robertpeteuil/sns-to-cloudwatch-logs-lambda/aws"
33-
version = "0.2.3"
33+
version = "0.2.5"
3434

3535
aws_region = "us-west-2"
3636
sns_topic_name = "projectx-logging"

main.tf

+44-66
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
# -------------------------------------------------------------------------------------------------------------
1+
# -----------------------------------------------------------------
22
# AWS SNS TO CLOUDWATCH LOGS LAMBDA GATEWAY
3-
# -------------------------------------------------------------------------------------------------------------
3+
# -----------------------------------------------------------------
44

5-
# Only tested on Terraform 0.11.1+
65
terraform {
7-
required_version = ">= 0.11.1"
6+
required_version = "~> 0.11.7"
87
}
98

10-
# -------------------------------------------------------------------------------------------------------------
9+
# -----------------------------------------------------------------
1110
# CREATE LAMBDA FUNCTION - SNS TO CLOUDWATCH LOGS GATEWAY
12-
# environment variables used for the log_group and log_stream so they aren't hardcoded into the function
13-
# function can be published (versioned) by setting the optional lambda_publish_func flag
14-
# -------------------------------------------------------------------------------------------------------------
11+
# environment variables used for the 'log_group' and 'log_stream'
12+
# function published if 'lambda_publish_func' set
13+
# -----------------------------------------------------------------
1514

1615
resource "aws_lambda_function" "sns_cloudwatchlog" {
1716
function_name = "${var.lambda_func_name}"
@@ -36,72 +35,69 @@ resource "aws_lambda_function" "sns_cloudwatchlog" {
3635
}
3736
}
3837

39-
# -------------------------------------------------------------------------------------------------------------
38+
# -----------------------------------------------------------------
4039
# SNS TOPIC
41-
# create new topic if create_sns_topic == true
42-
# otherwise retrieve existing topic metadata
43-
# topic arn used in "lambda_permssion" and "aws_sns_topic_subscription"
44-
# -------------------------------------------------------------------------------------------------------------
40+
# create new topic (if create_sns_topic set), else use existing topic
41+
# arn referenced by "lambda_permssion" and "aws_sns_topic_subscription"
42+
# -----------------------------------------------------------------
4543

4644
# create if specified
4745
resource "aws_sns_topic" "sns_log_topic" {
4846
count = "${var.create_sns_topic ? 1 : 0}"
4947
name = "${var.sns_topic_name}"
5048
}
5149

52-
# find existing if not creating
50+
# retrieve topic if not created, arn referenced
5351
data "aws_sns_topic" "sns_log_topic" {
5452
count = "${var.create_sns_topic ? 0 : 1}"
5553
name = "${var.sns_topic_name}"
5654
}
5755

58-
# -------------------------------------------------------------------------------------------------------------
56+
# -----------------------------------------------------------------
5957
# CLOUDWATCH LOG GROUP
60-
# create new log_group if create_log_group == true
61-
# -------------------------------------------------------------------------------------------------------------
58+
# create new log_group (if create_log_group set)
59+
# -----------------------------------------------------------------
6260

6361
resource "aws_cloudwatch_log_group" "sns_logged_item_group" {
6462
count = "${var.create_log_group ? 1 : 0}"
6563
name = "${var.log_group_name}"
6664
retention_in_days = "${var.log_group_retention_days}"
6765
}
6866

69-
# retrieve metadata for log group if no created, so arn can be included in outputs
67+
# retrieve log group if not created, arn included in outputs
7068
data "aws_cloudwatch_log_group" "sns_logged_item_group" {
7169
count = "${var.create_log_group ? 0 : 1}"
7270
name = "${var.log_group_name}"
7371
}
7472

75-
# -------------------------------------------------------------------------------------------------------------
76-
# CLOUDWATCH LOG STREAM IF create_log_stream == true
77-
# stream created in log_group specified or created
78-
# -------------------------------------------------------------------------------------------------------------
73+
# -----------------------------------------------------------------
74+
# CLOUDWATCH LOG STREAM
75+
# created new log stream (if create_log_stream set)
76+
# -----------------------------------------------------------------
7977

78+
# create stream in log_group previously created or specified
8079
resource "aws_cloudwatch_log_stream" "sns_logged_item_stream" {
8180
count = "${var.create_log_stream ? 1 : 0}"
8281
name = "${var.log_stream_name}"
8382
log_group_name = "${var.create_log_group ? join("", aws_cloudwatch_log_group.sns_logged_item_group.*.name) : var.log_group_name}"
8483
}
8584

86-
# -------------------------------------------------------------------------------------------------------------
85+
# -----------------------------------------------------------------
8786
# SUBSCRIBE LAMBDA FUNCTION TO SNS TOPIC
88-
# Lambda function subscription to sns topic
89-
# -------------------------------------------------------------------------------------------------------------
87+
# -----------------------------------------------------------------
9088

9189
resource "aws_sns_topic_subscription" "lambda" {
9290
topic_arn = "${var.create_sns_topic ? join("", aws_sns_topic.sns_log_topic.*.arn) : join("", data.aws_sns_topic.sns_log_topic.*.arn)}"
9391
protocol = "lambda"
94-
endpoint = "${var.lambda_publish_func ? aws_lambda_function.sns_cloudwatchlog.qualified_arn : aws_lambda_function.sns_cloudwatchlog.arn}"
92+
endpoint = "${var.lambda_publish_func ? aws_lambda_function.sns_cloudwatchlog.qualified_arn : aws_lambda_function.sns_cloudwatchlog.arn}"
9593
}
9694

97-
# -------------------------------------------------------------------------------------------------------------
98-
# ENABLE SNS TOPIC AS LAMBDA FUNCTION TRIGGER
99-
# use multiple resource blocks as condition parameters aren't possible until Terraform v0.12.0
100-
# -------------------------------------------------------------------------------------------------------------
101-
10295
# -----------------------------------------------------------------
103-
# function published - "qualifier" parameter set to function version
96+
# ENABLE SNS TOPIC AS LAMBDA FUNCTION TRIGGER
97+
# multiple resource blockss until 'null' parameter feature in Terraform v0.12.0
10498
# -----------------------------------------------------------------
99+
100+
# function published - "qualifier" set to function version
105101
resource "aws_lambda_permission" "sns_cloudwatchlog_published" {
106102
count = "${var.lambda_publish_func ? 1 : 0}"
107103
statement_id = "AllowExecutionFromSNS"
@@ -112,9 +108,7 @@ resource "aws_lambda_permission" "sns_cloudwatchlog_published" {
112108
qualifier = "${aws_lambda_function.sns_cloudwatchlog.version}"
113109
}
114110

115-
# -----------------------------------------------------------------
116-
# function not published - "qualifier" parameter not be set
117-
# -----------------------------------------------------------------
111+
# function not published - dont specify "qualifier" parameter
118112
resource "aws_lambda_permission" "sns_cloudwatchlog" {
119113
count = "${var.lambda_publish_func ? 0 : 1}"
120114
statement_id = "AllowExecutionFromSNS"
@@ -124,30 +118,24 @@ resource "aws_lambda_permission" "sns_cloudwatchlog" {
124118
source_arn = "${var.create_sns_topic ? join("", aws_sns_topic.sns_log_topic.*.arn) : join("", data.aws_sns_topic.sns_log_topic.*.arn)}"
125119
}
126120

127-
# -------------------------------------------------------------------------------------------------------------
121+
# -------------------------------------------------------------------------------------
128122
# CREATE IAM ROLE AND POLICIES FOR LAMBDA FUNCTION
129-
# -------------------------------------------------------------------------------------------------------------
123+
# -------------------------------------------------------------------------------------
130124

131-
# -----------------------------------------------------------------
132-
# Create base IAM role
133-
# -----------------------------------------------------------------
125+
# Create IAM role
134126
resource "aws_iam_role" "lambda_cloudwatch_logs" {
135127
name = "lambda_${lower(var.lambda_func_name)}"
136128
assume_role_policy = "${data.aws_iam_policy_document.lambda_cloudwatch_logs.json}"
137129
}
138130

139-
# -----------------------------------------------------------------
140-
# Add policy enabling access to other AWS services
141-
# -----------------------------------------------------------------
131+
# Add base Lambda Execution policy
142132
resource "aws_iam_role_policy" "lambda_cloudwatch_logs_polcy" {
143133
name = "lambda_${lower(var.lambda_func_name)}_policy"
144134
role = "${aws_iam_role.lambda_cloudwatch_logs.id}"
145135
policy = "${data.aws_iam_policy_document.lambda_cloudwatch_logs_policy.json}"
146136
}
147137

148-
# -----------------------------------------------------------------
149-
# JSON POLICY - execution
150-
# -----------------------------------------------------------------
138+
# JSON POLICY - assume role
151139
data "aws_iam_policy_document" "lambda_cloudwatch_logs" {
152140
statement {
153141
actions = ["sts:AssumeRole"]
@@ -159,9 +147,7 @@ data "aws_iam_policy_document" "lambda_cloudwatch_logs" {
159147
}
160148
}
161149

162-
# -----------------------------------------------------------------
163-
# JSON POLICY - enable access to other AWS services
164-
# -----------------------------------------------------------------
150+
# JSON POLICY - base Lambda Execution policy
165151
data "aws_iam_policy_document" "lambda_cloudwatch_logs_policy" {
166152
statement {
167153
actions = [
@@ -174,13 +160,11 @@ data "aws_iam_policy_document" "lambda_cloudwatch_logs_policy" {
174160
}
175161
}
176162

177-
# -------------------------------------------------------------------------------------------------------------
178-
# CREATE CLOUDWATCH TRIGGER EVENT TO PERIODICALLY CONTACT THE LAMBDA FUNCTION AND PREVENT IT FROM SUSPENDING
179-
# -------------------------------------------------------------------------------------------------------------
180-
181163
# -----------------------------------------------------------------
182-
# create cloudwatch event to run every 15 minutes
164+
# CREATE CLOUDWATCH EVENT TO PREVENT LAMBDA FUNCTION SUSPENSION
183165
# -----------------------------------------------------------------
166+
167+
# create cloudwatch event to run every 15 minutes
184168
resource "aws_cloudwatch_event_rule" "warmer" {
185169
count = "${var.create_warmer_event ? 1 : 0}"
186170

@@ -189,9 +173,7 @@ resource "aws_cloudwatch_event_rule" "warmer" {
189173
schedule_expression = "rate(15 minutes)"
190174
}
191175

192-
# -----------------------------------------------------------------
193-
# set event target as sns_to_cloudwatch_logs lambda function
194-
# -----------------------------------------------------------------
176+
# set event target as sns_to_cloudwatch_logs lambda function
195177
resource "aws_cloudwatch_event_target" "warmer" {
196178
count = "${var.create_warmer_event ? 1 : 0}"
197179

@@ -208,14 +190,12 @@ resource "aws_cloudwatch_event_target" "warmer" {
208190
JSON
209191
}
210192

211-
# -------------------------------------------------------------------------------------------------------------
212-
# ENABLE CLOUDWATCH EVENT AS LAMBDA FUNCTION TRIGGER
213-
# use multiple resource blocks as condition parameters aren't possible until Terraform v0.12.0
214-
# -------------------------------------------------------------------------------------------------------------
215-
216193
# -----------------------------------------------------------------
217-
# function published - "qualifier" parameter set to function version
194+
# ENABLE CLOUDWATCH EVENT AS LAMBDA FUNCTION TRIGGER
195+
# multiple resource blockss until 'null' parameter feature in Terraform v0.12.0
218196
# -----------------------------------------------------------------
197+
198+
# function published - "qualifier" set to function version
219199
resource "aws_lambda_permission" "warmer_published" {
220200
count = "${var.create_warmer_event ? var.lambda_publish_func ? 1 : 0 : 0}"
221201

@@ -227,9 +207,7 @@ resource "aws_lambda_permission" "warmer_published" {
227207
qualifier = "${aws_lambda_function.sns_cloudwatchlog.version}"
228208
}
229209

230-
# -----------------------------------------------------------------
231-
# function not published - "qualifier" parameter not be set
232-
# -----------------------------------------------------------------
210+
# function not published - dont specify "qualifier" parameter
233211
resource "aws_lambda_permission" "warmer" {
234212
count = "${var.create_warmer_event ? var.lambda_publish_func ? 0 : 1 : 0}"
235213

outputs.tf

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# -------------------------------------------------------------------------------------------------------------
1+
# -----------------------------------------------------------------
22
# AWS SNS TO CLOUDWATCH LOGS LAMBDA GATEWAY - OUTPUTS
3-
# -------------------------------------------------------------------------------------------------------------
3+
# -----------------------------------------------------------------
44

55
output "lambda_name" {
6-
description = "Name assigned to the Lambda Function."
6+
description = "Name assigned to Lambda Function."
77
value = "${var.lambda_func_name}"
88
}
99

@@ -18,7 +18,7 @@ output "lambda_version" {
1818
}
1919

2020
output "lambda_last_modified" {
21-
description = "The date the Lambda Function was last modified."
21+
description = "The date Lambda Function was last modified."
2222
value = "${aws_lambda_function.sns_cloudwatchlog.last_modified}"
2323
}
2424

variables.tf

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,75 @@
1-
# -------------------------------------------------------------------------------------------------------------
1+
# -----------------------------------------------------------------
22
# REQUIRED VARIABLES WITHOUT DEFAULT VALUES
3-
# -------------------------------------------------------------------------------------------------------------
3+
# -----------------------------------------------------------------
44

55
variable aws_region {
66
type = "string"
7-
description = "Region where AWS resources will be created and used."
7+
description = "Region where AWS resources will be created."
88
}
99

1010
variable sns_topic_name {
1111
type = "string"
12-
description = "Name of SNS Topic to be logged to CloudWatch Logs."
12+
description = "Name of SNS Topic logging to CloudWatch Log."
1313
}
1414

1515
variable log_group_name {
1616
type = "string"
17-
description = "Name of CloudWatch Log Group to create or use."
17+
description = "Name of CloudWatch Log Group created or used (if previously created)."
1818
}
1919

2020
variable log_stream_name {
2121
type = "string"
22-
description = "Name of CloudWatch Log Stream to create or use. If using an existing stream, it must exist in the Log group specified in 'log_group_name'."
22+
description = "Name of CloudWatch Log Stream created or used (if previously created). If using an existing stream it must exist in the Log group specified in 'log_group_name'."
2323
}
2424

25-
# -------------------------------------------------------------------------------------------------------------
26-
# VARIABLES DEFINITIONS WITH DEFAULT VALUES
27-
# -------------------------------------------------------------------------------------------------------------
28-
2925
# -----------------------------------------------------------------
30-
# SNS, LOG GROUP, LOG STREAM
26+
# VARIABLES DEFINITIONS WITH DEFAULT VALUES
3127
# -----------------------------------------------------------------
3228

29+
# SNS TOPIC, LOG GROUP, LOG STREAM
30+
3331
variable create_sns_topic {
3432
default = true
35-
description = "Boolean flag that determines if SNS topic: 'sns_topic_name' is created. If 'false' it uses an existing topic of that name."
33+
description = "Boolean flag that determines if SNS topic, 'sns_topic_name' is created. If 'false' it uses an existing topic of that name."
3634
}
3735

3836
variable create_log_group {
3937
default = true
40-
description = "Boolean flag that determines if log group: 'log_group_name' is created. If 'false' it uses an existing group of that name."
38+
description = "Boolean flag that determines if log group, 'log_group_name' is created. If 'false' it uses an existing group of that name."
4139
}
4240

4341
variable create_log_stream {
4442
default = true
45-
description = "Boolean flag that determines if log stream: 'log_stream_name' is created. If 'false' it uses an existing stream of that name."
43+
description = "Boolean flag that determines if log stream, 'log_stream_name' is created. If 'false' it uses an existing stream of that name."
4644
}
4745

4846
variable log_group_retention_days {
4947
default = 0
5048
description = "Number of days to retain data in the log group (0 = always retain)."
5149
}
5250

53-
# -----------------------------------------------------------------
5451
# LAMBDA FUNCTION
55-
# -----------------------------------------------------------------
5652

5753
variable lambda_func_name {
5854
type = "string"
5955
default = "SNStoCloudWatchLogs"
60-
description = "Name to assign to the Lambda Function."
56+
description = "Name to assign to Lambda Function."
6157
}
6258

6359
variable lambda_description {
6460
type = "string"
6561
default = "Route SNS messages to CloudWatch Logs"
66-
description = "Description to assign to the Lambda Function."
62+
description = "Description to assign to Lambda Function."
6763
}
6864

6965
variable lambda_publish_func {
7066
default = false
71-
description = "Boolean flag that determines if the Lambda function is published as a version."
67+
description = "Boolean flag that determines if Lambda function is published as a version."
7268
}
7369

7470
variable create_warmer_event {
7571
default = false
76-
description = "Boolean flag that determines if a CloudWatch Trigger event is created to prevent the Lambda function from suspending."
72+
description = "Boolean flag that determines if a CloudWatch Trigger event is created to prevent Lambda function from suspending."
7773
}
7874

7975
variable lambda_timeout {

0 commit comments

Comments
 (0)