Skip to content

Commit 6027c9d

Browse files
committed
configure lambda function
1 parent f0b14c3 commit 6027c9d

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

terraform-aws-sns/example/.terraform.lock.hcl

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform-aws-sns/example/example.tf

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module "sns_cloudwatch" {
88
description = "Capture each AWS Console Sign In"
99
sns_name = "mysns"
1010
sns_display_name = "demosns"
11+
lambda_function_name = "S3cloudHub_Test_Lambda_Function"
12+
lambda_function_runtime = "python3.8"
1113
}

terraform-aws-sns/main.tf

+62-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ resource "aws_sns_topic_subscription" "this" {
4343

4444
topic_arn = join("", aws_sns_topic.this.*.arn)
4545
protocol = var.subscribers[each.key].protocol
46-
endpoint = var.subscribers[each.key].endpoint
46+
endpoint = aws_lambda_function.terraform_lambda_func.arn
4747
endpoint_auto_confirms = var.subscribers[each.key].endpoint_auto_confirms
4848
raw_message_delivery = var.subscribers[each.key].raw_message_delivery
4949
}
@@ -68,4 +68,65 @@ data "aws_iam_policy_document" "sns_topic_policy" {
6868

6969
resources = [aws_sns_topic.this[count.index].arn]
7070
}
71+
}
72+
73+
resource "aws_iam_role" "lambda_role" {
74+
name = "S3cloudHub_Test_Lambda_Function_Role"
75+
assume_role_policy = <<EOF
76+
{
77+
"Version": "2012-10-17",
78+
"Statement": [
79+
{
80+
"Action": "sts:AssumeRole",
81+
"Principal": {
82+
"Service": "lambda.amazonaws.com"
83+
},
84+
"Effect": "Allow",
85+
"Sid": ""
86+
}
87+
]
88+
}
89+
EOF
90+
}
91+
resource "aws_iam_policy" "iam_policy_for_lambda" {
92+
93+
name = "aws_iam_policy_for_terraform_aws_lambda_role"
94+
path = "/"
95+
description = "AWS IAM Policy for managing aws lambda role"
96+
policy = <<EOF
97+
{
98+
"Version": "2012-10-17",
99+
"Statement": [
100+
{
101+
"Action": [
102+
"logs:CreateLogGroup",
103+
"logs:CreateLogStream",
104+
"logs:PutLogEvents"
105+
],
106+
"Resource": "arn:aws:logs:*:*:*",
107+
"Effect": "Allow"
108+
}
109+
]
110+
}
111+
EOF
112+
}
113+
114+
resource "aws_iam_role_policy_attachment" "attach_iam_policy_to_iam_role" {
115+
role = aws_iam_role.lambda_role.name
116+
policy_arn = aws_iam_policy.iam_policy_for_lambda.arn
117+
}
118+
119+
data "archive_file" "zip_the_python_code" {
120+
type = "zip"
121+
source_dir = "${path.module}/python/"
122+
output_path = "${path.module}/python/hello-python.zip"
123+
}
124+
125+
resource "aws_lambda_function" "terraform_lambda_func" {
126+
filename = "${path.module}/python/hello-python.zip"
127+
function_name = var.lambda_function_name
128+
role = aws_iam_role.lambda_role.arn
129+
handler = "index.lambda_handler"
130+
runtime = var.lambda_function_runtime
131+
depends_on = [aws_iam_role_policy_attachment.attach_iam_policy_to_iam_role]
71132
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def lambda_handler(event, context):
2+
message = 'Hello {} !'.format(event['key1'])
3+
return {
4+
'message' : message
5+
}

terraform-aws-sns/variable.tf

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ variable "sns_topic_policy_enabled" {
1313
default = true
1414
}
1515

16+
variable "lambda_function_name" {
17+
type = string
18+
default = ""
19+
}
20+
21+
variable "lambda_function_runtime" {
22+
type = string
23+
default = ""
24+
}
25+
1626
variable "sns_display_name" {
1727
type = string
1828
default = ""

0 commit comments

Comments
 (0)