やってみる

リソース

  • API Gateway
  • Lambda
  • Auth0 API

AWSでAPIを構築

SAM(Serverless Application Model) を用いて構築します。

sam initを実行しプロジェクトを作成します。

  • Quick Start Templatesを使用
  • runtimeはnodejs12.x
  • Project nameは任意のもの(default: sam-app)
  • application templatesはHello World
$ sam init
Which template source would you like to use?
	1 - AWS Quick Start Templates
	2 - Custom Template Location
Choice: 1

Which runtime would you like to use?
	1 - nodejs12.x
	2 - python3.8
	3 - ruby2.7
	4 - go1.x
	5 - java11
	6 - dotnetcore3.1
	7 - nodejs10.x
	8 - python3.7
	9 - python3.6
	10 - python2.7
	11 - ruby2.5
	12 - java8
	13 - dotnetcore2.1
Runtime: 1

Project name [sam-app]:

Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git

AWS quick start application templates:
	1 - Hello World Example
	2 - Step Functions Sample App (Stock Trader)
	3 - Quick Start: From Scratch
	4 - Quick Start: Scheduled Events
	5 - Quick Start: S3
	6 - Quick Start: SNS
	7 - Quick Start: SQS
	8 - Quick Start: Web Backend
Template selection: 1

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: nodejs12.x
Dependency Manager: npm
Application Template: hello-world
Output Directory: .

Next steps can be found in the README file at ./sam-app/README.md

HTTP APIを作成したいのでファイルを修正します。 template.yamlというファイルがあるので、そのファイルの22行目をType: HttpApiに変更します。 33行目の${ServerlessRestApi}${ServerlessHttpApi}に変更します。

$ $ cd sam-app/ ### initを実行してできたディレクトリ
$ vi template.yaml

~~~~~~~~
22           Type: HttpApi

33           Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
~~~~~~~~

sam buildを実行します。

$ sam build
Building function 'HelloWorldFunction'
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrc
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

sam deploy --guidedを実行してデプロイします

  • Stack Name: スタック名
  • AWS Region; AWSリージョン
  • Confirm changes before deploy: Y
  • Allow SAM CLI IAM role creation: Y
  • HelloWorldFunction may not have authorization defined, Is this okay?: y
  • Save arguments to samconfig.toml: Y
  • Deploy this changeset?: y

デプロイが成功すると Successfully ~~~が表示されます。

$ sam deploy --guided
~~~~~
Successfully created/updated stack - sam-app in ap-northeast-1

AWSのコンソールを確認してみます。

#aws #auth0 #amazon api gateway #jwt authorizers

Amazon API GatewayのHTTP APIをAuth0と統合して保護してみる
1.90 GEEK