この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
API GatewayでAPIを作成するとき、エンドポイントタイプを選択できます。
- エッジ最適化APIエンドポイント
- リージョンAPIエンドポイント
- プライベートAPIエンドポイント
ふと、気になりました。API Gatewayのデフォルトは「エッジ最適化APIエンドポイント」のため、あとから「リージョンAPIエンドポイント」に変更したいとき、APIのエンドポイント(URL)は、変わる? 変わらない?
というわけで、試してみました。
おすすめの方
- AWS SAMでAPI Gatewayのエンドポイントタイプを設定したい方
- カスタムドメイン利用時ではない
実験手順
- 「エッジ最適化APIエンドポイント」のAPIを新規作成する
- エンドポイントタイプが「エッジ最適化APIエンドポイント」であることを確認する
- エンドポイントタイプを「リージョンAPIエンドポイント」に変更する
- エンドポイントタイプが「リージョンAPIエンドポイント」であることを確認する
- API自体のエンドポイント(URL)が同じであることを確認する
1. 「エッジ最適化APIエンドポイント」のAPIを新規作成する
sam init
sam init \
--runtime python3.9 \
--name Api-EndpointType-Sample \
--app-template hello-world \
--package-type Zip
SAMテンプレート
エンドポイントタイプを「EDGE」にしています。
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Api-EndpointType-Sample
Resources:
HelloWorldApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
EndpointConfiguration:
Type: EDGE
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Timeout: 3
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
RestApiId: !Ref HelloWorldApi
HelloWorldFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${HelloWorldFunction}
Outputs:
HelloWorldApi:
Value: !Sub "https://${HelloWorldApi}.execute-api.${AWS::Region}.amazonaws.com/v1/hello/"
Lambdaコード
app.py
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
}),
}
デプロイ
sam build
sam package \
--output-template-file packaged.yaml \
--s3-bucket cm-fujii.genki-deploy
sam deploy \
--template-file packaged.yaml \
--stack-name Api-EndpointType-Sample-Stack \
--s3-bucket cm-fujii.genki-deploy \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset
APIエンドポイント(URL)の取得
aws cloudformation describe-stacks \
--stack-name Api-EndpointType-Sample-Stack \
--query 'Stacks[].Outputs'
[
[
{
"OutputKey": "HelloWorldApi",
"OutputValue": "https://94l55kswa7.execute-api.ap-northeast-1.amazonaws.com/v1/hello/"
}
]
]
APIにアクセスする
無事にアクセスできました。APIのエンドポイントタイプを変更しても、このURLが同じかどうかを確認します。
$ curl "https://94l55kswa7.execute-api.ap-northeast-1.amazonaws.com/v1/hello/"
{"message": "hello world"}
2. エンドポイントタイプが「エッジ最適化APIエンドポイント」であることを確認する
エンドポイントタイプは、「Edge」になっていました。
3. エンドポイントタイプを「リージョンAPIエンドポイント」に変更する
SAMテンプレート
一部抜粋です。「REGIONAL」にしました。
template.yaml
Resources:
HelloWorldApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
EndpointConfiguration:
Type: REGIONAL
デプロイ
再びデプロイします。
sam build
sam package \
--output-template-file packaged.yaml \
--s3-bucket cm-fujii.genki-deploy
sam deploy \
--template-file packaged.yaml \
--stack-name Api-EndpointType-Sample-Stack \
--s3-bucket cm-fujii.genki-deploy \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset
4. エンドポイントタイプが「リージョンAPIエンドポイント」であることを確認する
エンドポイントタイプは、「Regional」になっていました。「ID」も同じですね。
5. API自体のエンドポイント(URL)が同じであることを確認する
API自体のエンドポイント(URL)は、変わっていませんでした。
$ curl "https://94l55kswa7.execute-api.ap-northeast-1.amazonaws.com/v1/hello/"
{"message": "hello world"}
さいごに
ふと疑問に思ったので試してみました。API自体のエンドポイント(URL)は同じなので安心しました。