forked from TailorDev/hello-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_lambda.tf
84 lines (76 loc) · 2.65 KB
/
hello_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
# First, we need a role to play with Lambda
resource "aws_iam_role" "iam_role_for_lambda" {
name = "iam_role_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
# Here is a first lambda function that will run the code `hello_lambda.handler`
module "lambda" {
source = "./lambda"
name = "hello_lambda"
runtime = "python2.7"
role = "${aws_iam_role.iam_role_for_lambda.arn}"
}
# This is a second lambda function that will run the code
# `hello_lambda.post_handler`
module "lambda_post" {
source = "./lambda"
name = "hello_lambda"
handler = "post_handler"
runtime = "python2.7"
role = "${aws_iam_role.iam_role_for_lambda.arn}"
}
# Now, we need an API to expose those functions publicly
resource "aws_api_gateway_rest_api" "hello_api" {
name = "Hello API"
}
# The API requires at least one "endpoint", or "resource" in AWS terminology.
# The endpoint created here is: /hello
resource "aws_api_gateway_resource" "hello_api_res_hello" {
rest_api_id = "${aws_api_gateway_rest_api.hello_api.id}"
parent_id = "${aws_api_gateway_rest_api.hello_api.root_resource_id}"
path_part = "hello"
}
# Until now, the resource created could not respond to anything. We must set up
# a HTTP method (or verb) for that!
# This is the code for method GET /hello, that will talk to the first lambda
module "hello_get" {
source = "./api_method"
rest_api_id = "${aws_api_gateway_rest_api.hello_api.id}"
resource_id = "${aws_api_gateway_resource.hello_api_res_hello.id}"
method = "GET"
path = "${aws_api_gateway_resource.hello_api_res_hello.path}"
lambda = "${module.lambda.name}"
region = "${var.aws_region}"
account_id = "${var.aws_account_id}"
}
# This is the code for method POST /hello, that will talk to the second lambda
module "hello_post" {
source = "./api_method"
rest_api_id = "${aws_api_gateway_rest_api.hello_api.id}"
resource_id = "${aws_api_gateway_resource.hello_api_res_hello.id}"
method = "POST"
path = "${aws_api_gateway_resource.hello_api_res_hello.path}"
lambda = "${module.lambda_post.name}"
region = "${var.aws_region}"
account_id = "${var.aws_account_id}"
}
# We can deploy the API now! (i.e. make it publicly available)
resource "aws_api_gateway_deployment" "hello_api_deployment" {
rest_api_id = "${aws_api_gateway_rest_api.hello_api.id}"
stage_name = "production"
description = "Deploy methods: ${module.hello_get.http_method} ${module.hello_post.http_method}"
}