あめがえるのITブログ

頑張りすぎない。ほどほどに頑張るブログ。

【AWS】APIGateway(HTTP統合)を作ってみた


APIGateway(HTTP統合)を作ってみた

APIGateway(HTTP統合)とは

AWS API Gatewayの機能の一つで、HTTP統合(HTTP Integration)は、API Gatewayが受け取ったリクエストを、そのまま別のHTTPエンドポイント(例えば、EC2上のアプリや外部API)に転送する仕組み。

やること

TerraformでAPIGateway(HTTP統合)の構成を作成し、バックエンド(https://httpbin.org/)へ転送する。

実践

1.下記Terraformコードを作成
main.tf

provider "aws" {
  region  = "ap-northeast-1"
  profile = "testvault"
}

resource "aws_api_gateway_rest_api" "proxy_api" {
  name = "ProxyTestAPI"
}

resource "aws_api_gateway_resource" "proxy_root" {
  rest_api_id = aws_api_gateway_rest_api.proxy_api.id
  parent_id   = aws_api_gateway_rest_api.proxy_api.root_resource_id
  path_part   = "proxy"
}

resource "aws_api_gateway_resource" "proxy_path" {
  rest_api_id = aws_api_gateway_rest_api.proxy_api.id
  parent_id   = aws_api_gateway_resource.proxy_root.id
  path_part   = "{proxy+}"
}

resource "aws_api_gateway_method" "proxy_get" {
  rest_api_id   = aws_api_gateway_rest_api.proxy_api.id
  resource_id   = aws_api_gateway_resource.proxy_path.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "proxy_http_backend" {
  rest_api_id             = aws_api_gateway_rest_api.proxy_api.id
  resource_id             = aws_api_gateway_resource.proxy_path.id
  http_method             = aws_api_gateway_method.proxy_get.http_method
  type                    = "HTTP_PROXY"
  integration_http_method = "GET"
  uri                     = "https://httpbin.org/anything/{proxy}"
}

resource "aws_api_gateway_deployment" "proxy_deploy" {
  rest_api_id = aws_api_gateway_rest_api.proxy_api.id
  stage_name  = "v1"

  triggers = {
    redeploy = timestamp()
  }

  depends_on = [
    aws_api_gateway_integration.proxy_http_backend
  ]
}


2.Terraformデプロイ実行

# terraform init
# terraform plan
# terraform apply


3.動作確認
3-1.AWS - APIGateway - 作成したAPIGatewayを選択し、「ステージ」にあるURLをコピー

3-2.コマンドプロンプト等でcurlコマンドを実行

> curl https://x97psftwik.execute-api.ap-northeast-1.amazonaws.com/v1/proxy/{proxy+}

https://httpbin.org/から返答が返ってきていることを確認

>curl https://x97psftwik.execute-api.ap-northeast-1.amazonaws.com/v1/proxy/{proxy+}
{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Cloudfront-Forwarded-Proto": "https",
    "Cloudfront-Is-Desktop-Viewer": "true",
    "Cloudfront-Is-Mobile-Viewer": "false",
    "Cloudfront-Is-Smarttv-Viewer": "false",
    "Cloudfront-Is-Tablet-Viewer": "false",
    "Cloudfront-Viewer-Asn": "2516",
    "Cloudfront-Viewer-Country": "JP",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.11.1",
    "X-Amz-Cf-Id": "JmERtT-WbvUEAe8_sql4eMmQPudU-fZSCKs08cCiRaTZCUQnktHuVg==",
    "X-Amzn-Apigateway-Api-Id": "x97psftwik",
    "X-Amzn-Trace-Id": "Self=1-680c9401-0d9a9c816e1b62bf224c7300;Root=1-680c9400-6683db5f11b5ba74191618f0",
    "X-Custom-Header": "MyHeaderValue"
  },
  "json": null,
  "method": "GET",
  "origin": "106.146.28.188, 52.46.63.71, 3.112.162.47",
  "url": "https://httpbin.org/anything/proxy+"
}



感想

さて寝ようっと。