-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.tf
More file actions
96 lines (81 loc) · 2.52 KB
/
main.tf
File metadata and controls
96 lines (81 loc) · 2.52 KB
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
resource "aws_api_gateway_rest_api" "api" {
name = "rest"
api_key_source = "HEADER"
}
resource "aws_api_gateway_deployment" "deployment" {
rest_api_id = aws_api_gateway_rest_api.api.id
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_api_gateway_method.method,
aws_api_gateway_integration.integration
]
}
resource "aws_api_gateway_stage" "stage" {
stage_name = "dev"
rest_api_id = aws_api_gateway_rest_api.api.id
deployment_id = aws_api_gateway_deployment.deployment.id
# needed to prevent Terraform from updating this resource every time
cache_cluster_size = "0.5"
cache_cluster_enabled = false
}
resource "aws_api_gateway_resource" "resource" {
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.resource.id
authorization = "NONE"
http_method = "ANY"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
/* credentials = "DO NOT SET" */
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda.invoke_arn
}
resource "aws_lambda_function" "lambda" {
filename = "lambda.zip"
function_name = "mylambda"
role = aws_iam_role.role.arn
handler = "lambda.handler"
source_code_hash = filebase64sha256("lambda.zip")
runtime = "nodejs12.x"
environment {
variables = {
foo = "bar"
}
}
}
resource "aws_lambda_permission" "permission" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "apigateway.amazonaws.com"
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*/*" // must contain full path as defined by API
}
resource "aws_iam_role" "role" {
name = "test"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}