This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathroute53-ddns.tf
149 lines (125 loc) · 4.9 KB
/
route53-ddns.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
variable "debug" {
type = "string"
default = "false"
description = "Whether or not to enable debug logging in python lambda function"
}
variable "username" {
type = "string"
description = "Username for basic authentication"
}
variable "password" {
type = "string"
description = "Password for basic authentication"
}
variable "stage_name" {
type = "string"
default = "nic"
description = "Endpoint name to use for stage"
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {
current = true
}
data "aws_iam_policy_document" "lambda-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "role" {
name = "lambda_route53-ddns"
assume_role_policy = "${data.aws_iam_policy_document.lambda-assume-role-policy.json}"
}
resource "aws_iam_role_policy_attachment" "lambda-basic-execution-role" {
role = "${aws_iam_role.role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "route53fullaccess" {
role = "${aws_iam_role.role.name}"
policy_arn = "arn:aws:iam::aws:policy/AmazonRoute53FullAccess"
}
resource "aws_lambda_function" "lambda" {
description = "DynDNS v2 API compatible front end for Route 53"
function_name = "route53-ddns"
handler = "route53_ddns.handler"
runtime = "python2.7"
filename = "route53-ddns.zip"
source_code_hash = "${base64sha256(file("route53-ddns.zip"))}"
role = "${aws_iam_role.role.arn}"
environment = {
variables = {
DEBUG = "${var.debug}"
}
}
}
resource "aws_lambda_function" "authorizer" {
description = "Authorizer for DynDNS v2 API compatible front end for Route 53"
function_name = "route53-ddns-authorizer"
handler = "route53_ddns_authorizer.handler"
runtime = "python2.7"
filename = "route53-ddns-authorizer.zip"
source_code_hash = "${base64sha256(file("route53-ddns-authorizer.zip"))}"
role = "${aws_iam_role.role.arn}"
environment = {
variables = {
DEBUG = "${var.debug}"
USERNAME = "${var.username}"
PASSWORD = "${var.password}"
}
}
}
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "route53-ddns-sid"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda.function_name}"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}/*"
}
resource "aws_lambda_permission" "apigw_lambda_authorizer" {
statement_id = "route53-ddns-authorizer-sid"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.authorizer.function_name}"
principal = "apigateway.amazonaws.com"
}
resource "aws_api_gateway_rest_api" "api" {
name = "route53-ddns"
description = "Proxy API for dyndns2 API Lambda"
}
resource "aws_api_gateway_authorizer" "apigw_authorizer" {
name = "route53-ddns-authorizer"
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
authorizer_uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.authorizer.arn}/invocations"
authorizer_result_ttl_in_seconds = 3600
}
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
parent_id = "${aws_api_gateway_rest_api.api.root_resource_id}"
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "method" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_resource.proxy.id}"
http_method = "ANY"
authorization = "CUSTOM"
authorizer_id = "${aws_api_gateway_authorizer.apigw_authorizer.id}"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_resource.proxy.id}"
http_method = "${aws_api_gateway_method.method.http_method}"
integration_http_method = "POST"
type = "AWS_PROXY"
uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.lambda.arn}/invocations"
}
resource "aws_api_gateway_deployment" "deployment" {
depends_on = ["aws_api_gateway_method.method"]
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
stage_name = "${var.stage_name}"
stage_description = "Deployed at ${timestamp()}"
}
output "url" {
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.${data.aws_region.current.name}.amazonaws.com/${var.stage_name}"
}