Skip to content

Commit 57ccb77

Browse files
committed
⚡ better TF
1 parent 2af8f56 commit 57ccb77

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

data.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
###############
2+
# IAM #
3+
###############
4+
data "aws_iam_policy_document" "lambda" {
5+
statement {
6+
actions = [
7+
"logs:CreateLogStream",
8+
"logs:PutLogEvents"
9+
]
10+
resources = [
11+
"arn:aws:logs:*:*:*"
12+
]
13+
}
14+
statement {
15+
actions = [
16+
"xray:PutTraceSegments",
17+
"xray:PutTelemetryRecords",
18+
]
19+
resources = [
20+
"arn:aws:xray:*:*:*"
21+
]
22+
}
23+
}
24+
25+
###############
26+
# Lambda #
27+
###############
28+
data "archive_file" "python_lambda_package" {
29+
type = "zip"
30+
source_file = "${path.module}/src/handler.py"
31+
output_path = "lambda.zip"
32+
}

main.tf

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# IAM #
33
###############
44
resource "aws_iam_role" "iam_for_lambda" {
5-
name = "iam_for_lambda"
5+
name = "${var.function_name}-role"
66
assume_role_policy = <<EOF
77
{
88
"Version": "2012-10-17",
@@ -20,34 +20,12 @@ resource "aws_iam_role" "iam_for_lambda" {
2020
EOF
2121
}
2222

23-
# NOTE: We are deleteing the logs:CreateLogGroup action since we are creating a log group resource in terraform
24-
data "aws_iam_policy_document" "lambda_logs" {
25-
statement {
26-
actions = [
27-
"logs:CreateLogStream",
28-
"logs:PutLogEvents"
29-
]
30-
resources = [
31-
"arn:aws:logs:*:*:*"
32-
]
33-
}
34-
statement {
35-
actions = [
36-
"xray:PutTraceSegments",
37-
"xray:PutTelemetryRecords",
38-
]
39-
resources = [
40-
"arn:aws:xray:*:*:*"
41-
]
42-
}
43-
}
44-
4523
# IAM policy for logging from a lambda
4624
resource "aws_iam_policy" "iam_policy_for_lambda" {
47-
name = "aws_iam_policy_for_terraform_aws_lambda_log_role"
25+
name = "${var.function_name}-policy"
4826
path = "/"
4927
description = "AWS IAM Policy for managing aws lambda role"
50-
policy = data.aws_iam_policy_document.lambda_logs.json
28+
policy = data.aws_iam_policy_document.lambda.json
5129
}
5230

5331
# Policy Attachment on the role.
@@ -56,16 +34,9 @@ resource "aws_iam_role_policy_attachment" "attach_iam_policy_to_iam_role" {
5634
policy_arn = aws_iam_policy.iam_policy_for_lambda.arn
5735
}
5836

59-
6037
###############
6138
# Lambda #
6239
###############
63-
data "archive_file" "python_lambda_package" {
64-
type = "zip"
65-
source_file = "${path.module}/src/handler.py"
66-
output_path = "lambda.zip"
67-
}
68-
6940
resource "aws_lambda_function" "lambda" {
7041
function_name = var.function_name
7142
filename = "lambda.zip"
@@ -77,7 +48,7 @@ resource "aws_lambda_function" "lambda" {
7748
timeout = 10
7849
layers = [aws_lambda_layer_version.xray.arn]
7950
depends_on = [
80-
aws_cloudwatch_log_group.lambda_logs
51+
aws_cloudwatch_log_group.lambda
8152
]
8253
tracing_config {
8354
mode = "Active"
@@ -98,7 +69,7 @@ resource "aws_lambda_layer_version" "xray" {
9869
# Cloudwatch Logs #
9970
##########################
10071
# NOTE: The cloudwatch log group HAS to follow this naming convention for lambda logging
101-
resource "aws_cloudwatch_log_group" "lambda_logs" {
72+
resource "aws_cloudwatch_log_group" "lambda" {
10273
name = "/aws/lambda/${var.function_name}"
10374
retention_in_days = 14
10475
}

0 commit comments

Comments
 (0)