-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.tf
97 lines (88 loc) · 3.1 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
# This is required to get the AWS region via ${data.aws_region.current}.
data "aws_region" "current" {
}
# Define a Lambda function.
#
# The handler is the name of the executable for go1.x runtime.
resource "aws_lambda_function" "hello" {
function_name = "hello"
filename = "hello.zip"
handler = "hello"
source_code_hash = sha256(filebase64("hello.zip"))
role = aws_iam_role.hello.arn
runtime = "go1.x"
memory_size = 128
timeout = 1
}
# A Lambda function may access to other AWS resources such as S3 bucket. So an
# IAM role needs to be defined. This hello world example does not access to
# any resource, so the role is empty.
#
# The date 2012-10-17 is just the version of the policy language used here [1].
#
# [1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html
resource "aws_iam_role" "hello" {
name = "hello"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": {
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
}
POLICY
}
# Allow API gateway to invoke the hello Lambda function.
resource "aws_lambda_permission" "hello" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.hello.arn
principal = "apigateway.amazonaws.com"
}
# A Lambda function is not a usual public REST API. We need to use AWS API
# Gateway to map a Lambda function to an HTTP endpoint.
resource "aws_api_gateway_resource" "hello" {
rest_api_id = aws_api_gateway_rest_api.hello.id
parent_id = aws_api_gateway_rest_api.hello.root_resource_id
path_part = "hello"
}
resource "aws_api_gateway_rest_api" "hello" {
name = "hello"
}
# GET
# Internet -----> API Gateway
resource "aws_api_gateway_method" "hello" {
rest_api_id = aws_api_gateway_rest_api.hello.id
resource_id = aws_api_gateway_resource.hello.id
http_method = "GET"
authorization = "NONE"
}
# POST
# API Gateway ------> Lambda
# For Lambda the method is always POST and the type is always AWS_PROXY.
#
# The date 2015-03-31 in the URI is just the version of AWS Lambda.
resource "aws_api_gateway_integration" "hello" {
rest_api_id = aws_api_gateway_rest_api.hello.id
resource_id = aws_api_gateway_resource.hello.id
http_method = aws_api_gateway_method.hello.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.hello.arn}/invocations"
}
# This resource defines the URL of the API Gateway.
resource "aws_api_gateway_deployment" "hello_v1" {
depends_on = [
"aws_api_gateway_integration.hello"
]
rest_api_id = aws_api_gateway_rest_api.hello.id
stage_name = "v1"
}
# Set the generated URL as an output. Run `terraform output url` to get this.
output "url" {
value = "${aws_api_gateway_deployment.hello_v1.invoke_url}${aws_api_gateway_resource.hello.path}"
}