-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
99 lines (86 loc) · 3.1 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
terraform {
required_version = "1.5.1"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.23.1"
}
}
}
locals {
// Since EventBridge Scheduler is not yet supported by localstack, we conditionally set the below
// lambda_trigger local value if var.eventbridge_scheduler_enabled is false.
eventbridge_scheduler_trigger = {
principal = "scheduler.amazonaws.com"
source_arn = try(aws_scheduler_schedule.default[0].arn, "")
}
cloudwatch_events_trigger = {
principal = "events.amazonaws.com"
source_arn = try(aws_cloudwatch_event_rule.schedule[0].arn, "")
}
lambda_trigger = var.eventbridge_scheduler_enabled ? local.eventbridge_scheduler_trigger : local.cloudwatch_events_trigger
dd_tags = merge(
{
for item in compact(split(",", try(var.additional_environment_variables.DD_TAGS, ""))) :
split(":", trimspace(item))[0] => try(split(":", trimspace(item))[1], "")
},
var.datadog_custom_tags,
{ handlername = lower(var.function_name), },
)
}
data "aws_s3_bucket" "grants_source_data" {
bucket = var.grants_source_data_bucket_name
}
module "lambda_execution_policy" {
source = "cloudposse/iam-policy/aws"
version = "1.0.1"
iam_source_policy_documents = var.additional_lambda_execution_policy_documents
iam_policy_statements = {
AllowS3Upload = {
effect = "Allow"
actions = ["s3:PutObject"]
resources = [
# Path: /sources/YYYY/mm/dd/grants.gov/archive.zip
"${data.aws_s3_bucket.grants_source_data.arn}/sources/*/*/*/grants.gov/archive.zip"
]
}
}
}
module "lambda_artifact" {
source = "../taskfile_lambda_builder"
autobuild = var.lambda_autobuild
binary_base_path = var.lambda_binaries_base_path
function_name = var.function_name
s3_bucket = var.lambda_artifact_bucket
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "6.2.0"
function_name = "${var.namespace}-${var.function_name}"
description = "Downloads and stores the daily XML database extract from Grants.gov"
role_permissions_boundary = var.permissions_boundary_arn
attach_cloudwatch_logs_policy = true
cloudwatch_logs_retention_in_days = var.log_retention_in_days
attach_policy_json = true
policy_json = module.lambda_execution_policy.json
handler = "bootstrap"
runtime = "provided.al2"
architectures = [var.lambda_arch]
publish = true
layers = var.lambda_layer_arns
create_package = false
s3_existing_package = {
bucket = var.lambda_artifact_bucket
key = module.lambda_artifact.s3_object_key
}
timeout = 120 # 2 minutes, in seconds
environment_variables = merge(var.additional_environment_variables, {
DD_TAGS = join(",", sort([for k, v in local.dd_tags : "${k}:${v}"]))
GRANTS_GOV_BASE_URL = "https://www.grants.gov"
GRANTS_SOURCE_DATA_BUCKET_NAME = data.aws_s3_bucket.grants_source_data.id
LOG_LEVEL = var.log_level
})
allowed_triggers = {
Schedule = local.lambda_trigger
}
}