-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
135 lines (120 loc) · 4.51 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
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
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
resource "aws_iam_role_policy" "test_policy" {
name = "test_policy"
role = "${aws_iam_role.lambda-registry_iam.id}"
policy = "${file("policies/lambda-role-policy.json")}"
}
resource "aws_iam_role" "lambda-registry_iam" {
name = "lambda-registry_iam"
assume_role_policy = "${file("policies/lambda-role.json")}"
}
resource "aws_s3_bucket_object" "lambda_function" {
bucket = "${var.s3_bucket}"
key = "lambda-registry.zip"
source = "lambda-registry.zip"
etag = "${md5(file("lambda-registry.zip"))}"
}
resource "aws_lambda_function" "lambda-registry_lambda" {
s3_bucket = "${var.s3_bucket}"
s3_key = "lambda-registry.zip"
function_name = "${var.lambda_function_name}"
role = "${aws_iam_role.lambda-registry_iam.arn}"
handler = "index.handler"
runtime = "nodejs4.3"
timeout = 20
source_code_hash = "${base64sha256(file("lambda-registry.zip"))}"
depends_on = ["aws_s3_bucket_object.lambda_function"]
}
resource "aws_api_gateway_rest_api" "lambda-registry_api" {
name = "lambda-registry"
description = "API for HAProxy Configuration Generation"
depends_on = ["aws_lambda_function.lambda-registry_lambda"]
}
resource "aws_api_gateway_resource" "lambda-registry_resource" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
parent_id = "${aws_api_gateway_rest_api.lambda-registry_api.root_resource_id}"
path_part = "generate"
}
resource "aws_api_gateway_method" "lambda-registry_method" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
resource_id = "${aws_api_gateway_resource.lambda-registry_resource.id}"
http_method = "POST"
authorization = "NONE"
request_models = {
"application/json" = "${aws_api_gateway_model.lambda-registry_request_model.name}"
}
}
resource "aws_api_gateway_integration" "lambda-registry_integration" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
resource_id = "${aws_api_gateway_resource.lambda-registry_resource.id}"
http_method = "${aws_api_gateway_method.lambda-registry_method.http_method}"
type = "AWS"
integration_http_method = "${aws_api_gateway_method.lambda-registry_method.http_method}"
uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${aws_lambda_function.lambda-registry_lambda.arn}/invocations"
depends_on = ["aws_lambda_function.lambda-registry_lambda"]
}
resource "aws_api_gateway_model" "lambda-registry_request_model" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
name = "Configuration"
description = "A configuration schema"
content_type = "application/json"
schema = <<EOF
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "lambda-registryConfiguration",
"type": "array",
"properties": {
"mode": { "type": "string" },
"name": { "type": "string" },
"predicate": { "type": "string" },
"cookie": { "type": "string" },
"servers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"ip": { "type": "string" }
}
}
}
}
}
EOF
}
resource "aws_api_gateway_model" "lambda-registry_response_model" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
name = "ConfigurationFile"
description = "A configuration file schema"
content_type = "application/json"
schema = <<EOF
{
"type": "object"
}
EOF
}
resource "aws_api_gateway_method_response" "200" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
resource_id = "${aws_api_gateway_resource.lambda-registry_resource.id}"
http_method = "${aws_api_gateway_method.lambda-registry_method.http_method}"
status_code = "200"
response_models = {
"application/json" = "${aws_api_gateway_model.lambda-registry_response_model.name}"
}
}
resource "aws_api_gateway_integration_response" "lambda-registry_integration_response" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
resource_id = "${aws_api_gateway_resource.lambda-registry_resource.id}"
http_method = "${aws_api_gateway_method.lambda-registry_method.http_method}"
status_code = "${aws_api_gateway_method_response.200.status_code}"
depends_on = ["aws_api_gateway_integration.lambda-registry_integration"]
}
resource "aws_api_gateway_deployment" "production" {
rest_api_id = "${aws_api_gateway_rest_api.lambda-registry_api.id}"
stage_name = "api"
depends_on = ["aws_api_gateway_integration.lambda-registry_integration"]
}