forked from nozaq/terraform-aws-lambda-auto-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
137 lines (111 loc) · 4.11 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
terraform {
required_version = ">= 0.15.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.50.0"
}
null = {
source = "hashicorp/null"
version = ">= 3.1.0"
}
archive = {
source = "hashicorp/archive"
version = ">= 2.2.0"
}
}
}
#---------------------------------------------------------------------------------------------------
# IAM role for Lambda function
#---------------------------------------------------------------------------------------------------
resource "aws_iam_role" "this" {
name_prefix = var.iam_role_name_prefix
assume_role_policy = data.aws_iam_policy_document.assume.json
permissions_boundary = var.permissions_boundary
tags = var.tags
}
data "aws_iam_policy_document" "assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = var.allowed_services
}
}
}
resource "aws_iam_role_policy_attachment" "basic" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "vpc" {
count = var.vpc_config == null ? 0 : 1
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
resource "aws_iam_role_policy_attachment" "lambda" {
count = length(var.policy_arns)
role = aws_iam_role.this.name
policy_arn = var.policy_arns[count.index]
}
#---------------------------------------------------------------------------------------------------
# CloudWatch Log group
#---------------------------------------------------------------------------------------------------
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/${var.function_name}"
retention_in_days = var.retention_in_days
kms_key_id = var.kms_key_id
tags = var.tags
}
#---------------------------------------------------------------------------------------------------
# Lambda function
#---------------------------------------------------------------------------------------------------
resource "aws_lambda_function" "this" {
filename = data.archive_file.source.output_path
role = aws_iam_role.this.arn
source_code_hash = data.archive_file.source.output_base64sha256
runtime = var.runtime
handler = var.handler
description = var.description
function_name = var.function_name
memory_size = var.memory_size
reserved_concurrent_executions = var.reserved_concurrent_executions
layers = var.layers
timeout = var.timeout
publish = var.publish
dynamic "dead_letter_config" {
for_each = var.dead_letter_config == null ? [] : [var.dead_letter_config]
content {
target_arn = dead_letter_config.value.target_arn
}
}
dynamic "environment" {
for_each = var.environment == null ? [] : [var.environment]
content {
variables = environment.value.variables
}
}
dynamic "tracing_config" {
for_each = var.tracing_config == null ? [] : [var.tracing_config]
content {
mode = tracing_config.value.mode
}
}
dynamic "vpc_config" {
for_each = var.vpc_config == null ? [] : [var.vpc_config]
content {
security_group_ids = vpc_config.value.security_group_ids
subnet_ids = vpc_config.value.subnet_ids
}
}
# If this configuration is not provided when environment variables are in use,
# AWS Lambda uses a default service key. If this configuration is provided when
# environment variables are not in use, the AWS Lambda API does not save this
# configuration and Terraform will show a perpetual difference of adding the key.
kms_key_arn = var.environment == null ? null : var.lambda_kms_key_arn
tags = var.tags
lifecycle {
ignore_changes = [filename]
}
depends_on = [aws_iam_role_policy_attachment.basic, aws_iam_role_policy_attachment.vpc[0], aws_cloudwatch_log_group.this]
}