-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.tf
More file actions
213 lines (180 loc) · 5.59 KB
/
main.tf
File metadata and controls
213 lines (180 loc) · 5.59 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
resource "random_pet" "random" {
length = 2
}
data "aws_region" "current" {}
resource "aws_iam_role" "apigateway" {
name = "rest-apigw-sfn"
assume_role_policy = data.aws_iam_policy_document.apigw_assume.json
}
data "aws_iam_policy_document" "apigw_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["apigateway.amazonaws.com"]
}
}
}
resource "aws_iam_policy_attachment" "policy_invoke_sfn" {
name = random_pet.random.id
roles = [aws_iam_role.apigateway.name]
policy_arn = "arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess"
}
resource "aws_api_gateway_rest_api" "api" {
name = random_pet.random.id
}
resource "aws_api_gateway_resource" "resource" {
parent_id = aws_api_gateway_rest_api.api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.api.id
path_part = "test"
}
resource "aws_api_gateway_method" "method" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
credentials = aws_iam_role.apigateway.arn
http_method = aws_api_gateway_method.method.http_method
type = "AWS"
uri = "arn:aws:apigateway:${data.aws_region.current.name}:states:action/StartExecution"
integration_http_method = "POST"
request_templates = {
"application/json" = <<EOF
{
"input": "$util.escapeJavaScript($input.json('$'))",
"stateMachineArn": "${aws_sfn_state_machine.sfn_state_machine.arn}"
}
EOF
}
}
resource "aws_cloudwatch_log_group" "apigateway" {
name = "/aws/apigateway/${aws_api_gateway_rest_api.api.name}"
retention_in_days = 3
}
resource "aws_api_gateway_stage" "example" {
deployment_id = aws_api_gateway_deployment.deployment.id
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "dev"
access_log_settings {
destination_arn = aws_cloudwatch_log_group.apigateway.arn
format = jsonencode({
"requestId" : "$context.requestId",
"ip" : "$context.identity.sourceIp",
"caller" : "$context.identity.caller",
"user" : "$context.identity.user",
"requestTime" : "$context.requestTime",
"httpMethod" : "$context.httpMethod",
"resourcePath" : "$context.resourcePath",
"status" : "$context.status",
"protocol" : "$context.protocol",
"responseLength" : "$context.responseLength"
})
}
depends_on = [aws_api_gateway_method.method]
}
resource "aws_api_gateway_method_response" "response_200" {
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
status_code = "200"
}
resource "aws_api_gateway_integration_response" "response_200" {
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
status_code = aws_api_gateway_method_response.response_200.status_code
depends_on = [
aws_api_gateway_integration.integration
]
}
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_rest_api.api, aws_api_gateway_method.method, aws_api_gateway_integration.integration]
}
data "aws_iam_policy_document" "sfn_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["states.amazonaws.com"]
}
}
}
resource "aws_iam_role" "sfn" {
name = "rest-api-sfn"
assume_role_policy = data.aws_iam_policy_document.sfn_assume.json
}
resource "aws_sfn_state_machine" "sfn_state_machine" {
name = random_pet.random.id
role_arn = aws_iam_role.sfn.arn
definition = <<EOF
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "${aws_lambda_function.lambda.arn}",
"End": true
}
}
}
EOF
}
resource "aws_iam_policy" "policy_invoke_lambda" {
name = "RestStepFunctionLambdaFunctionInvocationPolicy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction",
"lambda:InvokeAsync"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "iam_for_sfn_attach_policy_invoke_lambda" {
role = aws_iam_role.sfn.name
policy_arn = aws_iam_policy.policy_invoke_lambda.arn
}
#
# Lambda
#
resource "aws_lambda_function" "lambda" {
filename = "lambda.zip"
function_name = random_pet.random.id
role = aws_iam_role.lambda_role.arn
handler = "lambda.handler"
source_code_hash = filebase64sha256("lambda.zip")
runtime = "nodejs14.x"
}
resource "aws_iam_role" "lambda_role" {
name = "rest-lambda-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume.json
}
data "aws_iam_policy_document" "lambda_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "lambda_policy_auth" {
role = aws_iam_role.lambda_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}