· 6 years ago · Oct 17, 2019, 06:00 PM
1provider "aws" {
2 region = "us-west-2"
3 access_key = ""
4 secret_key = ""
5}
6
7variable "myregion" {
8 default="us-west-2"
9}
10
11variable "accountId" {
12 default=""
13}
14
15
16resource "aws_api_gateway_rest_api" "example" {
17 name = "regional-example"
18 description= "Regional APIs"
19 endpoint_configuration {
20 types = ["REGIONAL"]
21 }
22}
23
24resource "aws_api_gateway_resource" "MyDemoResource" {
25 rest_api_id = "${aws_api_gateway_rest_api.example.id}"
26 parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}"
27 path_part = "mydemoresource"
28}
29
30resource "aws_api_gateway_method" "MyDemoMethod" {
31 rest_api_id = "${aws_api_gateway_rest_api.example.id}"
32 resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
33 http_method = "GET"
34 authorization = "NONE"
35}
36
37resource "aws_api_gateway_integration" "integration" {
38 rest_api_id = "${aws_api_gateway_rest_api.example.id}"
39 resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
40 http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
41 integration_http_method = "GET"
42 type = "AWS_PROXY"
43 uri = "arn:aws:lambda:us-west-2:710035157152:function:function2"
44}
45
46# Lambda
47resource "aws_lambda_permission" "apigw_lambda" {
48 statement_id = "AllowExecutionFromAPIGateway"
49 action = "lambda:InvokeFunction"
50 function_name = "function2"
51 principal = "apigateway.amazonaws.com"
52
53 # More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
54 #*/GET/pets
55 source_arn = "arn:aws:execute-api:${var.myregion}:${var.accountId}:${aws_api_gateway_rest_api.example.id}/*/${aws_api_gateway_method.MyDemoMethod.http_method}${aws_api_gateway_resource.MyDemoResource.path}"
56}