Skip to content

Commit 2c5df5e

Browse files
feat: fixed hardcoded value by defined in variable file (#27)
* feat: fixed hardcoded value by defined in variable file * feat: fixed all the lambda functions * feat: verified tfchecks --------- Co-authored-by: Anmol Nagpal <[email protected]>
1 parent 74ab1db commit 2c5df5e

File tree

6 files changed

+62
-28
lines changed

6 files changed

+62
-28
lines changed

_example/basic-function/example.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module "lambda" {
1414
source = "../../"
1515
name = local.name
1616
environment = local.environment
17-
filename = "../../lambda_packages/existing_package.zip"
17+
filename = "../../lambda_packages/index.zip"
1818
handler = "index.lambda_handler"
1919
runtime = "python3.7"
2020
variables = {

_example/complete-function/example.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module "lambda" {
1616
environment = local.environment
1717
create_layers = true
1818
timeout = 60
19-
filename = "../../lambda_packages/existing_package.zip"
19+
filename = "../../lambda_packages/index.zip"
2020
handler = "index.lambda_handler"
2121
runtime = "python3.8"
2222
compatible_architectures = ["arm64"]
@@ -31,7 +31,7 @@ module "lambda" {
3131
names = [
3232
"python_layer"
3333
]
34-
layer_filenames = ["../../lambda_packages/guardduty_enabler.zip"]
34+
layer_filenames = ["../../lambda_packages/layer.zip"]
3535
compatible_runtimes = [
3636
["python3.8"]
3737
]

lambda_packages/layer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import json
3+
4+
def lambda_handler(event, context):
5+
json_region = os.environ['AWS_REGION']
6+
return {
7+
"statusCode": 200,
8+
"headers": {
9+
"Content-Type": "application/json"
10+
},
11+
"body": json.dumps({
12+
"Region ": json_region
13+
})
14+
}

main.tf

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,18 @@ resource "aws_lambda_permission" "default" {
149149
## Terraform module to create Iam role resource on AWS for lambda.
150150
##-----------------------------------------------------------------------------
151151
resource "aws_iam_role" "default" {
152-
count = var.enable && var.create_iam_role ? 1 : 0
153-
name = format("%s-role", module.labels.id)
154-
155-
assume_role_policy = <<EOF
156-
{
157-
"Version": "2012-10-17",
158-
"Statement": [
159-
{
160-
"Action": "sts:AssumeRole",
161-
"Principal": {
162-
"Service": "lambda.amazonaws.com"
163-
},
164-
"Effect": "Allow",
165-
"Sid": ""
166-
}
167-
]
168-
}
169-
EOF
152+
count = var.enable && var.create_iam_role ? 1 : 0
153+
name = format("%s-testrole", module.labels.id)
154+
assume_role_policy = var.assume_role_policy
170155
}
171156

172157
##-----------------------------------------------------------------------------
173158
## Terraform module to create Iam policy resource on AWS for lambda.
174159
##-----------------------------------------------------------------------------
175160
resource "aws_iam_policy" "default" {
176161
count = var.enable && var.create_iam_role ? 1 : 0
177-
name = format("%s-logging", module.labels.id)
178-
path = "/"
162+
name = format("%s-testlogging", module.labels.id)
163+
path = var.aws_iam_policy_path
179164
description = "IAM policy for logging from a lambda"
180165
policy = data.aws_iam_policy_document.default[0].json
181166
}
@@ -213,13 +198,13 @@ resource "aws_kms_key" "kms" {
213198

214199
resource "aws_kms_alias" "kms-alias" {
215200
count = var.enable && var.enable_kms ? 1 : 0
216-
name = format("alias/%s-lambda-keys", module.labels.id)
201+
name = format("alias/%s-testlambda-keys", module.labels.id)
217202
target_key_id = aws_kms_key.kms[0].key_id
218203
}
219204

220205
resource "aws_kms_alias" "kms-alias-cloudwatch" {
221206
count = var.enable && var.enable_kms && !var.existing_cloudwatch_log_group ? 1 : 0
222-
name = format("alias/%s-lambda-cloudwatch-keys", module.labels.id)
207+
name = format("alias/%s-testlambda-cloudwatch-keys", module.labels.id)
223208
target_key_id = aws_kms_key.kms[1].key_id
224209
}
225210

@@ -301,7 +286,7 @@ data "aws_cloudwatch_log_group" "lambda" {
301286

302287
resource "aws_cloudwatch_log_group" "lambda" {
303288
count = var.enable && !var.existing_cloudwatch_log_group ? 1 : 0
304-
name = "/aws/lambda/${module.labels.id}"
289+
name = "/aws/testlambda/${module.labels.id}"
305290
retention_in_days = var.cloudwatch_logs_retention_in_days
306291
kms_key_id = var.enable_kms ? aws_kms_key.kms[1].arn : var.cloudwatch_logs_kms_key_arn
307292
tags = module.labels.tags
@@ -322,7 +307,7 @@ data "aws_iam_policy_document" "logs" {
322307

323308
resource "aws_iam_policy" "logs" {
324309
count = var.enable && var.create_iam_role && var.attach_cloudwatch_logs_policy ? 1 : 0
325-
name = "aws_lambda-logs"
310+
name = var.aws_iam_policy_logs_name
326311
path = var.policy_path
327312
policy = data.aws_iam_policy_document.logs[0].json
328313
tags = module.labels.tags

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Module : Lambda
22
# Description : Terraform Lambda function module outputs.
3+
output "name" {
4+
value = module.labels.name
5+
description = "The name can identifying your Lambda Function."
6+
}
7+
38
output "arn" {
49
value = join("", aws_lambda_function.default[*].arn)
510
description = "The Amazon Resource Name (ARN) identifying your Lambda Function."

variables.tf

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,34 @@ variable "policy_path" {
412412
type = string
413413
default = null
414414
description = "Path of policies to that should be added to IAM role for Lambda Function"
415-
}
415+
}
416+
417+
variable "assume_role_policy" {
418+
type = string
419+
description = "assume role policy document in JSON format"
420+
default = <<EOF
421+
{
422+
"Version": "2012-10-17",
423+
"Statement": [
424+
{
425+
"Action": "sts:AssumeRole",
426+
"Principal": {
427+
"Service": "lambda.amazonaws.com"
428+
},
429+
"Effect": "Allow",
430+
"Sid": ""
431+
}
432+
]
433+
}
434+
EOF
435+
}
436+
variable "aws_iam_policy_logs_name" {
437+
type = string
438+
default = "aws_testlambda-logs"
439+
description = "IAM policy name mentioned here"
440+
}
441+
variable "aws_iam_policy_path" {
442+
type = string
443+
default = "/"
444+
description = "IAM policy path default value"
445+
}

0 commit comments

Comments
 (0)