開発部の「エイジ」です。
今回はterraformを使ってAPI Gatewayを構築する手順を書きたいと思います。
関連記事
terraformとは
HashiCorpが手がける、インフラ構築や設定をコード(テンプレートファイル)を使って自動化するためのツールであり、 Infrastructure as Code を実現するOSSです。
APIとは
Application Programming Interfaceの略でありアプリケーションの連携をするのに用いられます
terraformを使用することのメリット
Terraformインストール
1 2 3 |
# brew install terraform |
Terraformのバージョン確認
1 2 3 |
# terraform version |
AWSの環境構築
今回はAWS CLIを利用するにあたりiamユーザーを作ります。
IAM ユーザーを作る
AWS CLIを使うのでプログラムによるアクセスにも☑️してください。
管理権限をAdministratorAccessにしてください。
無事作成できたら次にAWS CLIをインストールします。
AWS CLI
1 2 3 |
# sudo pip install awscli |
AWS CLIの設定
先ほど制作したiamユーザーの情報を登録します。
1 2 3 |
# aws configure --profile ユーザー名 |
一つづづ先ほど作成したiamユーザーから設定していきます。
1 2 3 4 5 6 7 |
AWS Access Key ID [None]: AWS Secret Access Key [None]: Default region name [None]: Default output format [None]: json # Asia Pacific (Tokyo) ap-northeast-1 |
Terraformを作成する。
これからAPI Gateway作成までを書いていきます。
まずはTerraformのコマンドを記します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
初期化 Terraformで新しく設定を記述した場合、初期化を行う必要があります。 # terraform init 確認(所謂dry-run) # terraform plan 適用 コードの状態をAWS上へ適用 # terraform apply すべて消去するコマンド # terraform destroy リソースの閲覧 # terraform show |
まずはTerraformを書き込むファイルを作成します。
1 2 3 |
touch main.tf |
※Terraformは拡張子が.tfのファイルを自動的にテンプレートとして認識してくれます。
API Gateway の宣言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
provider "aws" { profile = "プロフィール名" region = "ap-northeast-1" } resource "aws_api_gateway_rest_api" "MyDemoAPI" { name = "MyDemoAPI" description= "MyDemoAPI" endpoint_configuration { types = ["REGIONAL"] } } |
コマンドを実行します。
1 2 3 4 5 |
# terraform init # terraform plan # terraform apply |
実際にAWS上で確かめると作成されています。
次にリソースの設定をしていきます。
aws_api_gateway_resourceの宣言
リソースとはAPIのパスです。リソースを指定することで任意のパスを作ることができます。
1 2 3 4 5 6 7 |
resource "aws_api_gateway_resource" "MyDemoResource" { rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id path_part = "mydemoresource" } |
実行するとルートの直下へ/mydemoresourceが作成されます。
aws_api_gateway_methodの宣言
ここでは受け付けることができるクエリパラメータや必須のパラメータ, 認可の有無, API Keyの有無... などと実際に受け付けるリクエストを絞り込む, いわばリクエストの受付のような役割を担います.
リソースで指定した物にメソッドを付与します。今回はGETを指定いたします。
1 2 3 4 5 6 7 8 9 10 11 12 |
resource "aws_api_gateway_method" "MyDemoMethod" { rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id resource_id = aws_api_gateway_resource.MyDemoResource.id http_method = "GET" response_parameters = { "method.response.header.Access-Control-Allow-Headers" = true, "method.response.header.Access-Control-Allow-Methods" = true, "method.response.header.Access-Control-Allow-Origin" = true } } |
aws_api_gateway_integrationの宣言
統合リクエストではバックエンドへどのように繋ぐのか決めます。
ntegration Requestはどのバックエンドにどのようにデータを渡すかを決定します. バックエンドとは例えばLambda関数やAWSサービスなどといった実際にリクエストを処理するコンポーネントのことです.
統合ポイントのtypeを指定します。今回はMOCKを指定しました。
1 2 3 4 5 6 7 8 9 10 11 |
resource "aws_api_gateway_integration" "MyDemoIntegration" { rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id resource_id = aws_api_gateway_resource.MyDemoResource.id http_method = aws_api_gateway_method.MyDemoMethod.http_method type = "MOCK" request_templates = { "application/json" = "{ \"statusCode\": 200 }" } } |
aws_api_gateway_integration_responseの宣言
バックエンドから返ってきたレスポンスに関する設定を行います. 例えばHTTPステータスコードをマッピングしたり, レスポンスの内容の変換を行ったりします.
1 2 3 4 5 6 7 8 |
resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" { rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id resource_id = aws_api_gateway_resource.MyDemoResource.id http_method = aws_api_gateway_method.MyDemoMethod.http_method status_code = aws_api_gateway_method_response.response_200.status_code } |
aws_api_gateway_method_responseの宣言
クライアントから受け取ったリクエストに対するAPI Gatewayの最終的なレスポンスをここで定義します. ステータスコードやHTTPヘッダなどといった部分です.
レスポンスのステータスコードを設定します。
1 2 3 4 5 6 7 8 |
resource "aws_api_gateway_method_response" "response_200" { rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id resource_id = aws_api_gateway_resource.MyDemoResource.id http_method = aws_api_gateway_method.MyDemoMethod.http_method status_code = "200" } |
最後にデプロイをします。
aws_api_gateway_deploymentの宣言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
resource "aws_api_gateway_deployment" "MyDemoDeployment" { depends_on = [aws_api_gateway_integration.MyDemoIntegration] rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id stage_name = "test" variables = { "answer" = "42" } lifecycle { create_before_destroy = true } } |
そして実行すると作成されます。
以上になります。お読みいただきありがとうございました。
このブログは株式会社CoLabMixによる技術ブログです。
GCP、AWSなどでのインフラ構築・運用や、クローリング・分析・検索などを主体とした開発を行なっています。
Ruby on RailsやDjango、Pythonなどの開発依頼などお気軽にお声がけください。
開発パートナーを増やしたいという企業と積極的に繋がっていきたいです。