LoginSignup
9
0

More than 5 years have passed since last update.

Chaliceを使ってみた話

Last updated at Posted at 2017-12-20

Chaliceについて(基礎)

Chaliceを使ったきっかけ

仕事でAWS Lambdaを使うことになりましたが、AWS Lambdaを使う方法は色々ありました。
AWS Lambdaで実行するコードを作成して、AWSコンソールにアップロードしたら、
API Gatewayで設定する方法もありますが
最近話題になっているServerless Frameworkを使って、簡単にServerless Architectureができました。
また、Serverless Frameworkも色々ありますが、
先輩の提案でChailceというFrameworkを使うことにしました。

Chaliceとは

PythonベースであるServerless Frameworkとして、
AWS LambdaとAPI GatewayをまとめてServerless ArchitectureができるFrameworkです。
さすがに動かしてみないと分からないので、簡単にやってみました。

Chalice使い方

1.Vitualenv Install
Chaliceを使う時、Vitualenvをお勧めします。

$ pip install virtualenv
$ virtualenv ~/.virtualenvs/{virtualenv名} (virtualenv作成)
$ source ~/.virtualenvs/{virtualenv名}/bin/activate (仮想環境を有効化)

2.Chalice Install

$ pip install chalice

3.Project作成

$ chalice new-project {project名}

4.File構成

 /.chalice
 /.gitignore
 /app.py
 /requirements.txt

確認できたら、実際にchaliceを動かすapp.pyを見ると下記のようです。

app.py
from chalice import Chalice

app = Chalice(app_name='helloworld')


@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.
#

5.Deploy

ひとまず、Deployをやってみます。

$ chalice deploy

Initial creation of lambda function.
Creating role
Creating deployment package.
Lambda deploy done.
Initiating first time deployment...
Deploying to: dev

https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/

コマンドの下に表示されるURLにアクセスすると、
{'hello': 'world’}がresponseされていることが確認できます。

Deployの流れをDiagramでみると下記のようです。
スクリーンショット 2017-12-20 23.13.01.png

感想

何よりもAWSを触ってみて非常におもしろかったです。
Serverless Frameworkを通じて、エンジニアは他のところを気にせずに、
ビジネスロジックだけ夢中になれることに気づきました。
また、API Gateway + AWS Lambda + 他の技術が触れることを考えると、
もっとやってみたいと思いました。

参考資料

https://aws.amazon.com/jp/blogs/developer/preview-the-python-serverless-microframework-for-aws/
https://github.com/aws/chalice

9
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
0