Skip to content

srcbrew/aws-lambda-python3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-lambda-python3

这个项目,是在 AWS 官方提供的 Lambda Hello World (Python 3) 工程模板基础上,额外添加了依赖层管理,发测试环境,上线,打版本,回滚等工程实践。

  • src - Code for the application's Lambda function.
  • events - Invocation events that you can use to invoke the function.
  • tests - Unit tests for the application code.
  • template.yaml - A template that defines the application's AWS resources.

Development the sample application

和普通 python3 项目一样开发,测试。

Deploy the sample application

发布相关操作需要先安装 AWS 和 SAM (The Serverless Application Model) CLI。SAM 是 AWS CLI 的扩展,其使用 Docker 模拟线上 Lambda 运行环境,以便开发调试。

第一次发布前,需要配置以下参数:

  • Stack Name: AWS CloudFormation 堆栈名,用于实际发布 Lambda 及相关资源。可以配置为项目名字。
  • AWS Region: 想要发布到那个 AWS region。

构建依赖层: 官方自带的例子会把 src/requirements.txt 里包含的所有依赖包文件和 src 放在一起,显得很杂乱,且容易超过 50 M 的源码目录大小限制。依赖层的作用是把依赖打成一个单独的公共镜像,供 src 文件使用,且可以多项目共用同一个依赖层。

make layer

发测试环境:(发到测试环境的最新版代码,会自动打上 $latest 版本号)

make testing
#
make latest

打版本号:(为目前的 $latest 版本打一个版本号,供生产环境选用)

make version

上线/回滚:(选择一个版本号,打上 stable 版本号,供线上使用)

make setup   # 第一次上线需先 setup 调用 lambda function 所需的事件
make production

Use the SAM CLI to build and test locally

Build your application with the sam build --use-container command.

aws-lambda-python3$ sam build --use-container

The SAM CLI installs dependencies defined in hello_world/requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder.

Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the events folder in this project.

Run functions locally and invoke them with the sam local invoke command.

aws-lambda-python3$ sam local invoke HelloWorldFunction --event events/event.json

The SAM CLI can also emulate your application's API. Use the sam local start-api to run the API locally on port 3000.

aws-lambda-python3$ sam local start-api
aws-lambda-python3$ curl http://localhost:3000/

The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The Events property on each function's definition includes the route and method for each path.

      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

Add a resource to your application

The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in the SAM specification, you can use standard AWS CloudFormation resource types.

Fetch, tail, and filter Lambda function logs

To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.

NOTE: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.

aws-lambda-python3$ sam logs -n HelloWorldFunction --stack-name aws-lambda-python3 --tail

You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.

Tests

Tests are defined in the tests folder in this project. Use PIP to install the test dependencies and run tests.

aws-lambda-python3$ pip install -r tests/requirements.txt --user
# unit test
aws-lambda-python3$ python -m pytest tests/unit -v
# integration test, requiring deploying the stack first.
# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing
aws-lambda-python3$ AWS_SAM_STACK_NAME=<stack-name> python -m pytest tests/integration -v

Cleanup

To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name aws-lambda-python3

Resources

See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.

Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: AWS Serverless Application Repository main page

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published