ChaliceはPythonのみ対応しています。ChliceはPythonのライブラリなので、Pythonをインストールしている必要があります。また、AWS CLIを使用するので、AWS CLIもインストールしている必要があります。
Chaliceのインストール
Chaliceは以下のコマンドでインストールできます。
pip install chalice
Chaliceプロジェクトの作成
Chaliceプロジェクトは以下のコマンドで作成できます。
chalice new-project project-name
上記のコマンドで、project-nameフォルダが作成されます。フォルダには以下のファイルが作成されます。
.chalice/config.json
.gitignore
app.py
requirements.txt
config.jsonはアプリケーションの情報やでプリ情報、ステージごとの設定を管理するファイルです。app.pyはメインとなるプログラムを記述するファイルです。requirements.txtは依存ライブラリを記述する場所です。
AWSアクセスキーのセットアップ
AWSでユーザーを作成し、アクセスキーとシークレットアクセスキーを発行します。このキーは非常に重要な機密情報なので外部に漏れないように管理します。
以下のコマンドでAWSの接続情報を設定します。これがうまく設定できていないとAWSにデプロイできません。
aws configure --profile chalice
AWS Access Key ID [None]: xxxxxxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json
プログラムの最小構成
プロジェクトを作成すると、プログラムの最小構成が自動で追加されます。これはapp.pyに記述されています。コードは以下の通りです。
from chalice import Chalice
app = Chalice(app_name='project-name')
@app.route('/')
def index():
return {'hello': 'world'}
# The view function above will return {"hello": "world"}
# whenever you make an HTTP GET request to '/'.
#
# Here are a few more examples:
#
# @app.route('/hello/{name}')
# def hello_name(name):
# # '/hello/james' -> {"hello": "james"}
# return {'hello': name}
#
# @app.route('/users', methods=['POST'])
# def create_user():
# # This is the JSON body the user sent in their POST request.
# user_as_json = app.current_request.json_body
# # We'll echo the json body back to the user in a 'user' key.
# return {'user': user_as_json}
#
# See the README documentation for more examples.
#
ほとんどがコメントです。コメントに書いてあるようにプログラムのルーティングを行うことができます。
前:
次: