-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda.tf
70 lines (55 loc) · 2.49 KB
/
lambda.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
resource "aws_sns_topic" "lifecycle" {
name = var.name
kms_master_key_id = var.sns_topic_kms_key_id
tags = var.extra_tags
}
resource "aws_sns_topic_subscription" "lifecycle" {
topic_arn = aws_sns_topic.lifecycle.arn
protocol = "lambda"
endpoint = module.lambda.lambda_function_arn
}
module "lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "3.2.1"
function_name = "${var.name}-lifecycle"
handler = var.lambda_handler
source_path = var.lambda_source_path
runtime = var.lambda_runtime
timeout = var.lambda_timeout
kms_key_arn = var.kms_key_arn
reserved_concurrent_executions = var.reserved_concurrent_executions
# If publish is disabled, there will be "Error adding new Lambda Permission for notify_slack: InvalidParameterValueException: We currently do not support adding policies for $LATEST."
publish = true
environment_variables = var.lambda_environment_variables
create_role = var.lambda_role == ""
lambda_role = var.lambda_role
role_name = "${var.name}-lifecycle"
role_permissions_boundary = var.iam_role_boundary_policy_arn
role_tags = var.iam_role_tags
attach_network_policy = var.lambda_function_vpc_subnet_ids != null
allowed_triggers = {
AllowExecutionFromSNS = {
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.lifecycle.arn
}
}
store_on_s3 = var.lambda_function_store_on_s3
s3_bucket = var.lambda_function_s3_bucket
vpc_subnet_ids = var.lambda_function_vpc_subnet_ids
vpc_security_group_ids = var.lambda_function_vpc_security_group_ids
tags = var.extra_tags
}
data "aws_region" "current" {}
resource "null_resource" "assign_default_sg" {
# workaround for sg still attached to eni created by lambda function
# https://github.com/hashicorp/terraform-provider-aws/issues/10329
triggers = {
aws_region = data.aws_region.current.name
lambda_subnet_id = var.lambda_function_vpc_subnet_ids != null ? var.lambda_function_vpc_subnet_ids[0] : ""
lambda_sg_id = var.lambda_function_vpc_security_group_ids != null ? var.lambda_function_vpc_security_group_ids[0] : ""
}
provisioner "local-exec" {
when = destroy
command = "/bin/bash ${path.module}/scripts/update-lambda-eni.sh ${self.triggers.aws_region} ${self.triggers.lambda_subnet_id} ${self.triggers.lambda_sg_id}"
}
}