chalice README

The project's README from github.com/aws/chalice. As one markdown file: README.md.

=========== AWS Chalice

.. image:: https://img.shields.io/pypi/v/chalice.svg?style=flat :target: https://pypi.python.org/pypi/chalice/ :alt: Package Version

.. image:: https://img.shields.io/pypi/pyversions/chalice.svg?style=flat :target: https://pypi.python.org/pypi/chalice/ :alt: Python Versions

.. image:: https://readthedocs.org/projects/chalice/badge/?version=latest :target: https://aws.github.io/chalice/?badge=latest :alt: Documentation Status

.. image:: https://img.shields.io/pypi/l/chalice.svg?style=flat :target: https://github.com/aws/chalice/blob/master/LICENSE :alt: License

.. image:: https://aws.github.io/chalice/_static/img/chalice-logo-whitespace.png :target: https://aws.github.io/chalice/ :alt: Chalice Logo

Chalice is a framework for writing serverless apps in Python. It allows you to quickly create and deploy applications that use AWS Lambda. It provides:

You can create Rest APIs:

.. code-block:: python

from chalice import Chalice

app = Chalice(app_name="helloworld")

@app.route("/")
def index():
    return {"hello": "world"}

Tasks that run on a periodic basis:

.. code-block:: python

from chalice import Chalice, Rate

app = Chalice(app_name="helloworld")

# Automatically runs every 5 minutes
@app.schedule(Rate(5, unit=Rate.MINUTES))
def periodic_task(event):
    return {"hello": "world"}

You can connect a lambda function to an S3 event:

.. code-block:: python

from chalice import Chalice

app = Chalice(app_name="helloworld")

# Whenever an object is uploaded to "mybucket"
# this lambda function will be invoked.

@app.on_s3_event(bucket="mybucket")
def handler(event):
    print(
        f"Object uploaded for bucket: {event.bucket}, key: {event.key}"
    )

As well as an SQS queue:

.. code-block:: python

from chalice import Chalice

app = Chalice(app_name="helloworld")

# Invoke this lambda function whenever a message
# is sent to the ``my-queue-name`` SQS queue.

@app.on_sqs_message(queue="my-queue-name")
def handler(event):
    for record in event:
        print(f"Message body: {record.body}")

And several other AWS resources.

Once you've written your code, you just run chalice deploy and Chalice takes care of deploying your app.

::

$ chalice deploy
...
https://endpoint/api

$ curl https://endpoint/api
{"hello": "world"}

Up and running in less than 30 seconds. Give this project a try and share your feedback with us here on GitHub.

The documentation is available here <https://aws.github.io/chalice/>__.

Quickstart

.. quick-start-begin

In this tutorial, you'll use the chalice command line utility to create and deploy a basic REST API. This quickstart uses Python 3.10, but AWS Chalice supports all versions of Python supported by AWS Lambda, which includes Python 3.10 through Python 3.14.

To install Chalice, we'll first create and activate a virtual environment in python3.10::

$ python3 --version
Python 3.10.20
$ python3 -m venv .venv
$ . .venv/bin/activate

Next we'll install Chalice using pip::

$ python3 -m pip install chalice

You can verify you have chalice installed by running::

$ chalice --help
Usage: chalice [OPTIONS] COMMAND [ARGS]...
...

Credentials

Before you can deploy an application, be sure you have credentials configured. If you have previously configured your machine to run boto3 (the AWS SDK for Python) or the AWS CLI then you can skip this section.

If this is your first time configuring credentials for AWS you can follow these steps to quickly get started::

$ mkdir ~/.aws
$ cat >> ~/.aws/config
[default]
aws_access_key_id=YOUR_ACCESS_KEY_HERE
aws_secret_access_key=YOUR_SECRET_ACCESS_KEY
region=YOUR_REGION (such as us-west-2, us-west-1, etc)

If you want more information on all the supported methods for configuring credentials, see the boto3 docs <https://docs.aws.amazon.com/boto3/latest/guide/configuration.html>__.

Creating Your Project

The next thing we'll do is use the chalice command to create a new project::

$ chalice new-project helloworld

This will create a helloworld directory. Cd into this directory. You'll see several files have been created for you::

$ cd helloworld
$ ls -la
drwxr-xr-x   .chalice
-rw-r--r--   app.py
-rw-r--r--   requirements.txt

You can ignore the .chalice directory for now, the two main files we'll focus on is app.py and requirements.txt.

Let's take a look at the app.py file:

.. code-block:: python

from chalice import Chalice

app = Chalice(app_name='helloworld')


@app.route('/')
def index():
    return {'hello': 'world'}

The new-project command created a sample app that defines a single view, /, that when called will return the JSON body {"hello": "world"}.

Deploying

Let's deploy this app. Make sure you're in the helloworld directory and run chalice deploy::

$ chalice deploy
Creating deployment package.
Creating IAM role: helloworld-dev
Creating lambda function: helloworld-dev
Creating Rest API
Resources deployed:
  - Lambda ARN: arn:aws:lambda:us-west-2:123456789012:function:helloworld-dev
  - Rest API URL: https://abcd.execute-api.us-west-2.amazonaws.com/api/

You now have an API up and running using API Gateway and Lambda::

$ curl https://abcd.execute-api.us-west-2.amazonaws.com/api/
{"hello": "world"}

Try making a change to the returned dictionary from the index() function. You can then redeploy your changes by running chalice deploy.

.. quick-start-end

Next Steps

You've now created your first app using chalice. You can make modifications to your app.py file and rerun chalice deploy to redeploy your changes.

At this point, there are several next steps you can take.

If you're done experimenting with Chalice and you'd like to cleanup, you can use the chalice delete command, and Chalice will delete all the resources it created when running the chalice deploy command.

::

$ chalice delete
Deleting Rest API: abcd4kwyl4
Deleting function arn:aws:lambda:region:123456789012:function:helloworld-dev
Deleting IAM Role helloworld-dev

Feedback

We'd also love to hear from you. Please create any GitHub issues for additional features you'd like to see over at https://github.com/aws/chalice/issues.

chalice (aws/chalice)

Searching Database…

11.1K ·GitHub
这是什么?Chalice 是一个用于编写无服务器应用的 Python 框架:你只需编写普通的 Python 函数,它就会在背后创建并部署相应的 AWS Lambda 函数和 REST API。
它是框架还是命令行工具?两者都是:它提供一个基于装饰器的 Python 框架,用于把函数接入 AWS 服务,同时还附带 chalice 命令行工具,用来创建、部署和管理你的应用。
我怎样才能部署第一个应用?先配置好 AWS 凭证,运行 chalice new-project myapp 生成项目骨架,编辑生成的 app.py,然后运行 chalice deploy —— README 里说不到 30 秒就能跑起来。
在试用之前我需要准备什么?需要一个 AWS 账号,并在 ~/.aws/config 中配置好凭证(access key、secret key、region),这和 boto3 用的配置方式相同。除了 Python 本身,不需要额外安装任何东西。
它只能用在 AWS 上吗?它绑定 AWS:Chalice 构建的所有东西 —— Lambda 函数、API Gateway 路由、S3、SNS 和 SQS 集成 —— 都部署在你的 AWS 账号里,不能部署到其他云平台或你自己的服务器上。
一个最小的 Chalice 应用长什么样?只需几行 Python 代码:创建一个 Chalice 应用对象,然后用 @app.route('/') 装饰一个函数。该函数返回的字典就会成为通过 API Gateway 提供的 JSON 响应。
除了 HTTP 接口,还有什么能触发函数?可以通过 @app.schedule 设置定时任务(Rate(5, unit=Rate.MINUTES) 表示每 5 分钟触发一次),通过 @app.ons3event(bucket=...) 响应 S3 上传,通过 @app.onsqsmessage(queue=...) 处理 SQS 消息,此外还支持其他 AWS 资源。
我需要自己为函数编写 IAM 策略吗?不需要。Chalice 会根据你的应用实际执行的操作自动生成 IAM 策略,所以你不用手动维护角色文档。
chalice deploy 实际上会创建什么?怎么撤销?它会打包你的代码,创建一个 IAM 角色和一个 Lambda 函数,并配置好 REST API,同时打印它们的 ARN 和公开的 API URL。之后运行 chalice delete 可以删除 deploy 创建的所有资源。
What are similar projects?Zappa, examples, serverless, awesome-aws, encore, apisix, kubespray, faas
Creator
aws
Size
9.1 MB
Stars
11.1K
Updated
CommitOct 15
Age
10y
Commits · 90 Days