Skip to content

Instantly share code, notes, and snippets.

@hiroki1117
Created January 28, 2022 09:05
Show Gist options
  • Save hiroki1117/95d6396ae2a29eaf363d627bf59416c7 to your computer and use it in GitHub Desktop.
Save hiroki1117/95d6396ae2a29eaf363d627bf59416c7 to your computer and use it in GitHub Desktop.
terraformでapi gatewayを定義する(REST)
resource "aws_api_gateway_rest_api" "rest_api" {
name = "youtubebackup_api"
description = "rest api"
endpoint_configuration {
types = ["REGIONAL"]
}
}
resource "aws_api_gateway_resource" "sample" {
rest_api_id = aws_api_gateway_rest_api.rest_api.id
parent_id = aws_api_gateway_rest_api.rest_api.root_resource_id
path_part = "sample"
}
resource "aws_api_gateway_method" "sample_get" {
authorization = "NONE"
http_method = "GET"
resource_id = aws_api_gateway_resource.sample.id
rest_api_id = aws_api_gateway_rest_api.rest_api.id
}
resource "aws_api_gateway_method_response" "sample_get" {
rest_api_id = aws_api_gateway_rest_api.rest_api.id
resource_id = aws_api_gateway_resource.sample.id
http_method = aws_api_gateway_method.sample_get.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration" "sample_api_integration" {
rest_api_id = aws_api_gateway_rest_api.rest_api.id
resource_id = aws_api_gateway_resource.sample.id
http_method = aws_api_gateway_method.sample_get.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.video_info_lambda.invoke_arn
}
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.video_info_lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.rest_api.execution_arn}/*/${aws_api_gateway_method.sample_get.http_method}${aws_api_gateway_resource.sample.path}"
}

Terraformリソース

  • aws_api_gateway_rest_api
  • aws_api_gateway_resource
  • aws_api_gateway_method
  • aws_api_gateway_method_response
  • aws_api_gateway_integration
  • aws_lambda_permission

関係

  • 大元になるREST API
  • RESTAPI~Resourceは1対Nの関係
  • Resource~methodは1対Nの関係
  • methodlambdaintegrationは1対1対1の関係

ポイント

  • apiのバックエンドとlambdaを結びつけるintegrationがポイント
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment