Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Python Serverless Microframework for AWS

License

NotificationsYou must be signed in to change notification settings

aws/chalice

GitterDocumentation StatusChalice Logo

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

  • A command line tool for creating, deploying, and managing your app
  • A decorator based API for integrating with Amazon API Gateway, Amazon S3,Amazon SNS, Amazon SQS, and other AWS services.
  • Automatic IAM policy generation

You can create Rest APIs:

fromchaliceimportChaliceapp=Chalice(app_name="helloworld")@app.route("/")defindex():return {"hello":"world"}

Tasks that run on a periodic basis:

fromchaliceimportChalice,Rateapp=Chalice(app_name="helloworld")# Automatically runs every 5 minutes@app.schedule(Rate(5,unit=Rate.MINUTES))defperiodic_task(event):return {"hello":"world"}

You can connect a lambda function to an S3 event:

fromchaliceimportChaliceapp=Chalice(app_name="helloworld")# Whenever an object is uploaded to 'mybucket'# this lambda function will be invoked.@app.on_s3_event(bucket='mybucket')defhandler(event):print("Object uploaded for bucket: %s, key: %s"% (event.bucket,event.key))

As well as an SQS queue:

fromchaliceimportChaliceapp=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')defhandler(event):forrecordinevent:print("Message body: %s"%record.body)

And several other AWS resources.

Once you've written your code, you just runchalice deployand Chalice takes care of deploying your app.

$ chalice deploy...https://endpoint/dev$ 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 availablehere.

Quickstart

In this tutorial, you'll use thechalice command line utilityto create and deploy a basic REST API. This quickstart uses Python 3.7,but AWS Chalice supports all versions of python supported by AWS Lambda,which includes Python 3.7 through python 3.12.

You can find the latest versions of python on thePython download page.

To install Chalice, we'll first create and activate a virtual environmentin python3.7:

$ python3 --versionPython 3.7.3$ python3 -m venv venv37$ . venv37/bin/activate

Next we'll install Chalice usingpip:

$ python3 -m pip install chalice

You can verify you have chalice installed by running:

$ chalice --helpUsage: chalice [OPTIONS] COMMAND [ARGS]......

Credentials

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

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

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

If you want more information on all the supported methods forconfiguring credentials, see theboto3 docs.

Creating Your Project

The next thing we'll do is use thechalice command to create a newproject:

$ chalice new-project helloworld

This will create ahelloworld directory. Cd into thisdirectory. You'll see several files have been created for you:

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

You can ignore the.chalice directory for now, the two main fileswe'll focus on isapp.py andrequirements.txt.

Let's take a look at theapp.py file:

fromchaliceimportChaliceapp=Chalice(app_name='helloworld')@app.route('/')defindex():return {'hello':'world'}

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

Deploying

Let's deploy this app. Make sure you're in thehelloworlddirectory and runchalice deploy:

$ chalice deployCreating deployment package.Creating IAM role: helloworld-devCreating lambda function: helloworld-devCreating Rest APIResources deployed:  - Lambda ARN: arn:aws:lambda:us-west-2:12345: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://qxea58oupc.execute-api.us-west-2.amazonaws.com/api/{"hello": "world"}

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

Next Steps

You've now created your first app usingchalice. You can makemodifications to yourapp.py file and rerunchalice deploy toredeploy your changes.

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

  • Tutorials- Choose from among several guided tutorials that willgive you step-by-step examples of various features of Chalice.
  • Topics - Deepdive into documentation on specific areas of Chalice.This contains more detailed documentation than the tutorials.
  • API Reference - Low levelreference documentation on all the classes and methods that are part of thepublic API of Chalice.

If you're done experimenting with Chalice and you'd like to cleanup, you canuse thechalice delete command, and Chalice will delete all the resourcesit created when running thechalice deploy command.

$ chalice deleteDeleting Rest API: abcd4kwyl4Deleting function aws:arn:lambda:region:123456789:helloworld-devDeleting IAM Role helloworld-dev

Feedback

We'd also love to hear from you. Please create any Github issues foradditional features you'd like to see over athttps://github.com/aws/chalice/issues. You can also chat with uson gitter:https://gitter.im/awslabs/chalice


[8]ページ先頭

©2009-2025 Movatter.jp