It is a framework that can automatically deploy AWS Gateway settings and IAM roles starting from AWS Lambda.
: thought_balloon: I tried to find a framework that can create a web application using Lambda at explosive speed.
$ pip install chalice
You need to configure ~ / .aws / config`` ~ / .aws / credentials
. (Please refer to other articles)
$ chalice new-project handson
$ cd handson
$ ls -1a
./
../
.chalice/
.gitignore
app.py
requirements.txt
app.py is the main program.
from chalice import Chalice
app = Chalice(app_name='handson')
@app.route('/')
def index():
return {'hello': 'world'}
A decoder is attached to the method, and the path set in the argument becomes the API Gateway setting.
@app.route('/XXX')
$ chalice local
Serving on http://127.0.0.1:8000
Restarting local dev server.
Serving on http://127.0.0.1:8000
In another terminal, when you send a request, the string set in return of app.py will be returned.
$ curl http://127.0.0.1:8000/
{"hello":"world"}
$ chalice deploy
Creating deployment package.
Creating IAM role: handson-dev
Creating lambda function: handson-dev
Creating Rest API
Resources deployed:
- Lambda ARN: arn:aws:lambda:ap-northeast-1:012345678900:function:handson-dev
- Rest API URL: https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/api/
When deployed, (1) IAM role (2) lambda (3) API Gateway will be created and attached.
If you want to change the stage setting, you need to add it to .chalice / config.json
.chalice/config.json
{
"version": "2.0",
"app_name": "chalicehandson",
"stages": {
"dev": {
"api_gateway_stage": "api"
},
"prod": {
"api_gateway_stage": "api-prod"
}
}
}
Deploy by specifying stage
$ chalice deploy --stage prod
Creating deployment package.
Reusing existing deployment package.
Creating IAM role: handson-prod
Creating lambda function: handson-prod
Creating Rest API
Resources deployed:
- Lambda ARN: arn:aws:lambda:ap-northeast-1:01234567890000:function:handson-prod
- Rest API URL: https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/api-prod/
$ chalice delete
$ chalice delete --stage prod
Delete the one created by deployment (in this case, these three IAM roles, lambda, API Gateway)
Since Stage deploys both dev and prod, I am executing the command to delete the two.
In the case of aws + python, it's very convenient. I'm looking forward to seeing more and more services available. After that, there are various methods of chalice and they are rounded, so it seems that the learning cost is lower than the serverless framework.
--When you start local, it will restart automatically when you save it. --Unlike serverless framework, you can't create a cloudformation stack. --Even if you set Stage, API Gateway will be created with a different URL --The API Gateway path is cut, but there is only one lambda to connect
https://qiita.com/takeh/items/e52ad1c541a435e2b2e3 https://programmagick.com/blogs/aws_chalice_intro/ https://www.youtube.com/watch?v=u4LKbQZawaQ https://aws.github.io/chalice/index.html
Recommended Posts