Skip to content

Commit f434b58

Browse files
SD-2156. Environment and Slack variables
1 parent e05e6de commit f434b58

File tree

10 files changed

+86
-189
lines changed

10 files changed

+86
-189
lines changed

asg.tf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# ASG notification
22
resource "aws_autoscaling_notification" "manage_dns_asg_notification" {
3-
count = var.enabled ? 1 : 0
4-
53
group_names = var.asg_names
64

75
notifications = [
@@ -10,6 +8,6 @@ resource "aws_autoscaling_notification" "manage_dns_asg_notification" {
108
"autoscaling:EC2_INSTANCE_TERMINATE",
119
]
1210

13-
topic_arn = join("", aws_sns_topic.manage_dns_asg_sns.*.arn)
11+
topic_arn = aws_sns_topic.dns.arn
1412
}
1513

iam.tf

Lines changed: 44 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,46 @@
1-
# Lambda policy for logging
2-
resource "aws_iam_role_policy" "lambda_manage_dns_logging_policy" {
3-
count = var.enabled ? 1 : 0
4-
5-
name = "${var.service}_lambda_dns_logging_policy"
6-
role = join("", aws_iam_role.lambda_manage_dns_role.*.id)
7-
8-
policy = <<EOF
9-
{
10-
"Version": "2012-10-17",
11-
"Statement": [
12-
{
13-
"Effect": "Allow",
14-
"Action": [
15-
"logs:CreateLogGroup",
16-
"logs:CreateLogStream",
17-
"logs:PutLogEvents"
18-
],
19-
"Resource": "*"
20-
}
21-
]
22-
}
23-
EOF
24-
25-
}
26-
27-
# Lambda policy for managing dns
28-
resource "aws_iam_role_policy" "lambda_manage_dns_policy" {
29-
count = var.enabled ? 1 : 0
30-
31-
name = "${var.service}_lambda_route53_policy"
32-
role = join("", aws_iam_role.lambda_manage_dns_role.*.id)
33-
34-
policy = <<EOF
35-
{
36-
"Version": "2012-10-17",
37-
"Statement": [
38-
{
39-
"Effect": "Allow",
40-
"Action": [
41-
"route53:GetHostedZone",
42-
"route53:ChangeResourceRecordSets",
43-
"route53:ListResourceRecordSets"
44-
],
45-
"Resource": "arn:aws:route53:::hostedzone/${var.zone_id}"
46-
},
47-
{
48-
"Effect": "Allow",
49-
"Action": [
50-
"autoscaling:DescribeAutoScalingGroups",
51-
"autoscaling:DescribeAutoScalingInstances"
52-
],
53-
"Resource": "*"
54-
},
55-
{
56-
"Effect": "Allow",
57-
"Action": [
58-
"ec2:DescribeInstances"
59-
],
60-
"Resource": "*"
61-
}
62-
]
63-
}
64-
EOF
65-
66-
}
67-
68-
# Lambda role
69-
resource "aws_iam_role" "lambda_manage_dns_role" {
70-
count = var.enabled ? 1 : 0
71-
72-
name_prefix = "${var.service}_lambda_dns"
73-
74-
assume_role_policy = <<EOF
75-
{
76-
"Version": "2012-10-17",
77-
"Statement": [
78-
{
79-
"Action": "sts:AssumeRole",
80-
"Principal": {
81-
"Service": "lambda.amazonaws.com"
82-
},
83-
"Effect": "Allow",
84-
"Sid": ""
85-
}
86-
]
87-
}
88-
EOF
89-
90-
}
91-
92-
resource "aws_lambda_permission" "manage_dns_asg_sns" {
93-
count = var.enabled ? 1 : 0
94-
95-
statement_id = "AllowExecutionFromSNS"
96-
action = "lambda:InvokeFunction"
97-
function_name = join("", aws_lambda_function.manage_dns.*.arn)
98-
principal = "sns.amazonaws.com"
99-
source_arn = join("", aws_sns_topic.manage_dns_asg_sns.*.arn)
1+
# Lambda policy
2+
data "aws_iam_policy_document" "lambda" {
3+
statement {
4+
actions = [
5+
"route53:GetHostedZone",
6+
"route53:ChangeResourceRecordSets",
7+
"route53:ListResourceRecordSets",
8+
]
9+
10+
resources = [
11+
"arn:aws:route53:::hostedzone/${var.zone_id}",
12+
]
13+
}
14+
15+
statement {
16+
actions = [
17+
"autoscaling:DescribeAutoScalingGroups",
18+
"autoscaling:DescribeAutoScalingInstances",
19+
]
20+
21+
resources = [
22+
"*",
23+
]
24+
}
25+
26+
statement {
27+
actions = [
28+
"ec2:DescribeInstances",
29+
]
30+
31+
resources = [
32+
"*",
33+
]
34+
}
35+
36+
statement {
37+
actions = [
38+
"sts:AssumeRole",
39+
]
40+
41+
resources = [
42+
"*",
43+
]
44+
}
10045
}
10146

include/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

include/lambda.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
service = os.environ['SERVICE']
99
ttl = int(os.environ['TTL'])
1010
dns_role_arn = os.environ.get('DNS_ROLE_ARN')
11+
webhook_url = os.environ['SLACK_WEBHOOK']
12+
environment = os.environ['ENVIRONMENT']
1113

1214
private_instance_record_template = os.environ['PRIVATE_INSTANCE_RECORD_TEMPLATE']
1315
private_asg_record_template = os.environ['PRIVATE_ASG_RECORD_TEMPLATE']

lambda.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Lambda function
22
module "lambda" {
3-
source = "github.com/claranet/terraform-aws-lambda?ref=v1.2.0"
3+
source = "git@github.com:Daemon-Solutions/terraform-aws-lambda?ref=v1.2.0"
44
function_name = var.lambda_function_name
55
description = "Manages DNS records for ${join(", ", var.asg_names)} AutoScaling Group(s)"
66
handler = "lambda.lambda_handler"
7-
runtime = "python3.7"
7+
runtime = var.runtime
88
layers = var.lambda_layers
99
timeout = 300
1010
source_path = "${path.module}/include/lambda.py"
11-
policy = {
11+
build_command = "${var.runtime} build.py '$filename' '$runtime' '$source'"
12+
policy = {
1213
json = data.aws_iam_policy_document.lambda.json
1314
}
1415

@@ -17,6 +18,8 @@ module "lambda" {
1718
ZONE_ID = var.zone_id
1819
DNS_ROLE_ARN = var.dns_role_arn
1920
SERVICE = var.service
21+
SLACK_WEBHOOK = var.slack_webhook
22+
ENVIRONMENT = var.environment
2023
PRIVATE_INSTANCE_RECORD_TEMPLATE = var.private_instance_record_template
2124
PRIVATE_ASG_RECORD_TEMPLATE = var.private_asg_record_template
2225
PUBLIC_ASG_RECORD_TEMPLATE = var.public_asg_record_template
@@ -57,4 +60,3 @@ resource "null_resource" "notify_sns_topic" {
5760
command = "python ${path.module}/include/publish.py ${data.aws_region.current.name} ${element(var.asg_names, count.index)} ${aws_sns_topic.dns.arn}"
5861
}
5962
}
60-

main.tf

Lines changed: 0 additions & 55 deletions
This file was deleted.

outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
output "lambda_manage_dns_role_arn" {
2-
value = join("", aws_iam_role.lambda_manage_dns_role.*.name)
2+
value = module.lambda.role_arn
33
}
44

55
output "lambda_function_arn" {
6-
value = join("", aws_lambda_function.manage_dns.*.arn)
6+
value = module.lambda.function_arn
77
}
88

99
output "lambda_function_name" {
10-
value = join("", aws_lambda_function.manage_dns.*.function_name)
10+
value = module.lambda.function_name
1111
}
1212

1313
output "sns_topic_arn" {
14-
value = join("", aws_sns_topic.manage_dns_asg_sns.*.arn)
14+
value = aws_sns_topic.dns.arn
1515
}
1616

sns.tf

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
# SNS topic
2-
resource "aws_sns_topic" "manage_dns_asg_sns" {
3-
count = var.enabled ? 1 : 0
4-
2+
resource "aws_sns_topic" "dns" {
53
name = var.sns_topic_name
64
}
75

86
# SNS subscription
97
resource "aws_sns_topic_subscription" "sns_topic_subscription" {
10-
count = var.enabled ? 1 : 0
11-
12-
topic_arn = join("", aws_sns_topic.manage_dns_asg_sns.*.arn)
8+
topic_arn = aws_sns_topic.dns.arn
139
protocol = "lambda"
14-
endpoint = join("", aws_lambda_function.manage_dns.*.arn)
10+
endpoint = module.lambda.function_arn
1511
}
1612

terraform.tf

Lines changed: 0 additions & 8 deletions
This file was deleted.

variables.tf

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
variable "enabled" {
2-
description = "Enable or disable the Lambda DNS functionality."
3-
type = string
4-
default = "1"
5-
}
6-
71
variable "lambda_function_name" {
82
description = "The name of the Lambda Function to create, which will manage the Autoscaling Groups"
93
type = string
104
}
115

6+
variable "lambda_layers" {
7+
description = "List of Lambda Layer Version ARNs to attach to the Lambda Function"
8+
type = list(string)
9+
default = []
10+
}
11+
1212
variable "zone_id" {
1313
description = "Id of a zone file to add records to"
1414
type = string
1515
}
1616

17+
variable "dns_role_arn" {
18+
description = "ARN of a role to assume to manage DNS records. Useful if DNS zone is in different account"
19+
type = string
20+
default = ""
21+
}
22+
1723
variable "asg_names" {
18-
description = "Name of the Autoscaling Groups to attach this Lambda Function to"
24+
description = "The Autoscaling Group names to attach to this Lambda Function"
1925
type = list(string)
2026
}
2127

2228
variable "asg_count" {
2329
description = "Number of the Autoscaling Groups defined in asg_names variable. Only here because count cannot be computed"
24-
default = "1"
30+
default = 1
2531
}
2632

2733
variable "sns_topic_name" {
@@ -69,3 +75,15 @@ variable "ttl" {
6975
default = 60
7076
}
7177

78+
variable runtime {
79+
description = "Runtime binary"
80+
default = "python3.7"
81+
}
82+
83+
variable "slack_webhook" {
84+
description = "slack webhook for notifications"
85+
}
86+
87+
variable "environment" {
88+
description = "Environment"
89+
}

0 commit comments

Comments
 (0)