![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
こんな悩みを解決できる記事を書きました!
僕は現役フリーランスエンジニア(歴8年)で、資格は13個保有しています。
結論、ChaliceでAPI GatewayにCognito認証を付けるのは超簡単です。
具体的なソースコードを載せているので安心してくださいね。
![](https://traveler0401.com/wp-content/uploads/2022/12/【AWS-Chalice】API-GatewayにCognito認証を付ける方法.jpg)
![](https://traveler0401.com/wp-content/uploads/2022/12/【AWS-Chalice】API-GatewayにCognito認証を付ける方法.jpg)
本記事を読めば、ChaliceでAPI GatewayにCognito認証を付ける方法が理解できるので早速見ていきましょう。
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
すぐ読み終わるので、ぜひ最後まで読んでくださいね♪
ChaliceでAPI GatewayにCognito認証を付ける方法
早速ですが、ChaliceでAPI GatewayにCognito認証を付けてみます。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization.png)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization.png)
認可は「なし」に設定されています。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization2.jpg)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization2.jpg)
Cognitoを開き、ユーザープールの管理をクリックします。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization3.png)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization3.png)
ユーザープールを作成するをクリックします。
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
これ以降はステップ通りにポチポチするだけなので割愛します。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization5.jpg)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization5.jpg)
プールARNをコピーします。
from chalice import Chalice, CognitoUserPoolAuthorizer
app = Chalice(app_name='hello-world')
authorizer = CognitoUserPoolAuthorizer(
'MyPool', provider_arns=['コピーしたプールARN'])
@app.route('/', methods=['GET'], authorizer=authorizer)
def index():
return {'hello': 'world'}
authorizerを設定することでCognito認証が付与されます。
オーソライザーについて
「MyPool」はオーソライザー名なので任意の値を設定できます。
↑のソースコードでデプロイすると、下記のようなオーソライザーが自動で作成されます。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization7.jpg)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization7.jpg)
$ chalice deploy
デプロイします。
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization6.png)
![](https://traveler0401.com/wp-content/uploads/2022/12/chalice_cognito_authorization6.png)
Cognito認証が付与されていればOKです。
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
簡単でしたね…
よくある質問
ChaliceでAPI GatewayにCognito認証を付けるにはどうすればよいですか?
ユーザプールを作成し、authorizerを設定すればOKです。
詳しくは先ほどのソースコードをご確認ください。
Chaliceをローカルで起動した場合はどうなりますか?
ローカル起動した場合、認証は無視されます。
ですので、直接APIを実行しても認証エラーにはなりません。
IAM認証を付ける方法を教えてもらえますか?
下記をご覧ください。
![](https://traveler0401.com/wp-content/uploads/2022/12/aws-chalice-iam-authorization-300x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/12/aws-chalice-iam-authorization-300x150.png)
カスタムオーソライザーを付ける方法を教えてもらえますか?
下記をご覧ください。
![](https://traveler0401.com/wp-content/uploads/2022/12/aws-chalice-custom-authorization-300x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/12/aws-chalice-custom-authorization-300x150.png)
公式ドキュメントはありますか?
下記になります。
まとめ
今回は、ChaliceでAPI GatewayにCognito認証を付ける方法について解説しました。
以下が本記事のまとめになります。
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
最後までお読みいただき、ありがとうございました!
- クソおすすめ本
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
![](https://traveler0401.com/wp-content/uploads/2022/11/izumi-200-150x150.png)
海外のエンジニアがどういった思考で働いているかが理解できます。
海外に行く気はないけど海外エンジニアの動向が気になる雑魚エンジニアにおすすめです(本当におすすめな本しか紹介しないのでご安心を)。