forked from dod-iac/terraform-aws-lambda-function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
195 lines (182 loc) · 6.17 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* ## Usage
*
* Creates an AWS Lambda Function.
*
* ```hcl
* module "lambda_function" {
* source = "dod-iac/lambda-function/aws"
*
* execution_role_name = format(
* "app-%s-func-lambda-execution-role-%s",
* var.application,
* var.environment
* )
*
* function_name = format(
* "app-%s-func-%s-%s",
* var.application,
* var.environment,
* data.aws_region.current.name
* )
*
* function_description = "Function description."
*
* filename = format("../../lambda/%s-func.zip", var.application)
*
* handler = "index.handler"
*
* runtime = "nodejs12.x"
*
* environment_variables = var.environment_variables
*
* tags = {
* Application = var.application
* Environment = var.environment
* Automation = "Terraform"
* }
* }
* ```
*
* Use the optional `execution_role_policy_document` variable to override the IAM policy document for the IAM role.
*
* Use the optional `cloudwatch_schedule_expression` variable to schedule execution of the Lambda using CloudWatch Events.
*
* Use the optional `kms_key_arn` variable to encrypt the environment variables with a custom KMS key. Use the `dod-iac/lambda-kms-key/aws` module to create a KMS key.
*
* Use the optional `security_group_ids` and `subnet_ids` variables to run the function within a VPC.
*
* ## Testing
*
* Run all terratest tests using the `terratest` script. If using `aws-vault`, you could use `aws-vault exec $AWS_PROFILE -- terratest`. The `AWS_DEFAULT_REGION` environment variable is required by the tests. Use `TT_SKIP_DESTROY=1` to not destroy the infrastructure created during the tests. Use `TT_VERBOSE=1` to log all tests as they are run. Use `TT_TIMEOUT` to set the timeout for the tests, with the value being in the Go format, e.g., 15m. Use `TT_TEST_NAME` to run a specific test by name.
*
* ## Terraform Version
*
* Terraform 0.12. Pin module version to ~> 1.0.1 . Submit pull-requests to master branch.
*
* Terraform 0.11 is not supported.
*
* ## License
*
* This project constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. However, because the project utilizes code licensed from contributors and other third parties, it therefore is licensed under the MIT License. See LICENSE file for more information.
*/
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
resource "aws_iam_role" "execution_role" {
name = var.execution_role_name
assume_role_policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = var.tags
}
data "aws_iam_policy_document" "execution_role" {
statement {
sid = "CreateCloudWatchLogGroups"
actions = [
"logs:CreateLogGroup"
]
effect = "Allow"
resources = [
format(
"arn:%s:logs:*:*:log-group:/aws/lambda/*",
data.aws_partition.current.partition
)
]
}
statement {
sid = "CreateCloudWatchLogStreamsAndPutLogEvents"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
effect = "Allow"
resources = [
format(
"arn:%s:logs:*:*:log-group:/aws/lambda/*:log-stream:*",
data.aws_partition.current.partition
)
]
}
}
resource "aws_iam_policy" "execution_role" {
name = length(var.execution_role_policy_name) > 0 ? var.execution_role_policy_name : var.execution_role_name
path = "/"
policy = length(var.execution_role_policy_document) > 0 ? var.execution_role_policy_document : data.aws_iam_policy_document.execution_role.json
}
resource "aws_iam_role_policy_attachment" "execution_role" {
role = aws_iam_role.execution_role.name
policy_arn = aws_iam_policy.execution_role.arn
}
resource "aws_lambda_function" "main" {
depends_on = [
aws_iam_role_policy_attachment.execution_role
]
function_name = var.function_name
description = var.function_description
filename = var.filename
source_code_hash = filebase64sha256(var.filename)
handler = var.handler
layers = var.layers
kms_key_arn = length(var.kms_key_arn) > 0 && length(var.environment_variables) > 0 ? var.kms_key_arn : null
runtime = var.runtime
role = aws_iam_role.execution_role.arn
timeout = var.timeout
memory_size = var.memory_size
publish = true
tags = var.tags
environment {
variables = var.environment_variables
}
dynamic "vpc_config" {
for_each = length(var.security_group_ids) > 0 && length(var.subnet_ids) > 0 ? [1] : []
content {
security_group_ids = var.security_group_ids
subnet_ids = var.subnet_ids
}
}
}
#
# CloudWatch Events
#
resource "aws_cloudwatch_event_rule" "main" {
count = length(var.cloudwatch_schedule_expression) > 0 ? 1 : 0
name = length(var.cloudwatch_rule_name) > 0 ? var.cloudwatch_rule_name : var.function_name
description = length(var.cloudwatch_rule_description) > 0 ? var.cloudwatch_rule_description : ""
schedule_expression = var.cloudwatch_schedule_expression
}
resource "aws_cloudwatch_event_target" "main" {
count = length(var.cloudwatch_schedule_expression) > 0 ? 1 : 0
rule = aws_cloudwatch_event_rule.main.0.name
target_id = length(var.cloudwatch_target_id) > 0 ? var.cloudwatch_target_id : var.function_name
arn = aws_lambda_function.main.arn
}
resource "aws_lambda_permission" "main" {
count = length(var.cloudwatch_schedule_expression) > 0 ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.main.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.main.0.arn
}
#
# Event Sources
#
resource "aws_lambda_event_source_mapping" "main" {
count = length(var.event_sources)
function_name = aws_lambda_function.main.arn
event_source_arn = var.event_sources[count.index].event_source_arn
}