This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
lambda.tf
154 lines (135 loc) · 4.64 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
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
resource "aws_iam_role" "lambda-cloudwatch-dns-service" {
name = "lambda-dns-service.${var.region}.i.${var.environment}.${var.dns["domain_name"]}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "lambda-cloudwatch-dns-service" {
name = "lambda-cloudwatch-dns-service.${var.region}.i.${var.environment}.${var.dns["domain_name"]}"
role = "${aws_iam_role.lambda-cloudwatch-dns-service.name}"
lifecycle {
create_before_destroy = true
}
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"autoscaling:DescribeAutoScalingGroups",
"route53:ChangeResourceRecordSets",
"route53:ListResourceRecordSets"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda-cloudwatch-dns-service-xray" {
role = "${aws_iam_role.lambda-cloudwatch-dns-service.name}"
policy_arn = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}
resource "aws_iam_role_policy_attachment" "lambda-cloudwatch-dns-service-logs" {
role = "${aws_iam_role.lambda-cloudwatch-dns-service.name}"
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
}
data "archive_file" "lambda-dns-service" {
type = "zip"
source_file = "${path.module}/files/lambda-dns-service/dist/bundle.js"
output_path = "${path.module}/files/lambda-dns-service.dist.zip"
}
resource "aws_lambda_function" "cloudwatch-dns-service" {
filename = "${path.module}/files/lambda-dns-service.dist.zip"
source_code_hash = "${data.archive_file.lambda-dns-service.output_base64sha256}"
function_name = "${var.role}-cloudwatch-dns-service-${var.environment}"
role = "${aws_iam_role.lambda-cloudwatch-dns-service.arn}"
handler = "bundle.handler"
runtime = "nodejs6.10"
timeout = 10
depends_on = [
"data.archive_file.lambda-dns-service",
"aws_iam_role_policy_attachment.lambda-cloudwatch-dns-service-xray",
]
tracing_config = {
mode = "Active"
}
environment {
variables = {
HOSTED_ZONE_ID = "${aws_route53_zone.default.id}"
DOMAIN = "i.${var.environment}.${var.dns["domain_name"]}"
}
}
}
resource "aws_cloudwatch_event_rule" "autoscaling" {
name = "cloudwatch-dns-autoscaling-${var.environment}"
depends_on = ["aws_lambda_function.cloudwatch-dns-service"]
event_pattern = <<PATTERN
{
"source": [
"aws.autoscaling"
],
"detail-type": [
"EC2 Instance Launch Successful",
"EC2 Instance Terminate Successful",
"EC2 Instance Launch Unsuccessful",
"EC2 Instance Terminate Unsuccessful"
]
}
PATTERN
}
resource "aws_cloudwatch_event_target" "lambda-cloudwatch-dns-service-autoscaling" {
target_id = "lambda-cloudwatch-dns-service"
rule = "${aws_cloudwatch_event_rule.autoscaling.name}"
arn = "${aws_lambda_function.cloudwatch-dns-service.arn}"
depends_on = ["aws_lambda_function.cloudwatch-dns-service", "aws_cloudwatch_event_rule.autoscaling"]
}
resource "aws_lambda_permission" "cloudwatch-dns-service-autoscaling" {
statement_id = "AllowExecutionFromCloudWatchAutoScaling"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.cloudwatch-dns-service.arn}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.autoscaling.arn}"
depends_on = ["aws_lambda_function.cloudwatch-dns-service", "aws_cloudwatch_event_rule.autoscaling"]
}
resource "aws_cloudwatch_event_rule" "ec2" {
name = "cloudwatch-dns-ec2-${var.environment}"
event_pattern = <<PATTERN
{
"source": [
"aws.ec2"
],
"detail-type": [
"EC2 Instance State-change Notification"
]
}
PATTERN
}
resource "aws_cloudwatch_event_target" "lambda-cloudwatch-dns-service-ec2" {
target_id = "lambda-cloudwatch-dns-service"
rule = "${aws_cloudwatch_event_rule.ec2.name}"
arn = "${aws_lambda_function.cloudwatch-dns-service.arn}"
depends_on = ["aws_lambda_function.cloudwatch-dns-service", "aws_cloudwatch_event_rule.ec2"]
}
resource "aws_lambda_permission" "cloudwatch-dns-service-ec2" {
statement_id = "AllowExecutionFromCloudWatchEC2"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.cloudwatch-dns-service.arn}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.ec2.arn}"
depends_on = ["aws_lambda_function.cloudwatch-dns-service", "aws_cloudwatch_event_rule.ec2"]
}