官术网_书友最值得收藏!

Deploying the REST API

Now the fun part, we'll deploy our REST API using the Serverless Framework. At this point, we have not discussed the various configuration options when implementing serverless architectures on AWS. I'll cover different possibilities, and our particular configuration, later on in this chapter.

My pattern of using Docker as a build and deployment tool makes this process a bit easier. You are not required to do this, and there are likely other ways to make the process even simpler.

We will do all package building and deployment from inside a running Docker container, which I start and enter with the following Makefile target:

brianz@gold(master=)$ ENV=dev make shell

This equates to the following Docker command:

docker run --rm -it \
-v `pwd`:/code \
--env ENV=$(ENV) \
--env-file envs/$2 \
--name=coffee-cupping-$(ENV) \
verypossible/serverless:1.20.0-python3 bash

There is nothing magical here. We're starting up a Docker container from an image that contains the Serverless Framework as well as some other Python packages for a Python 3 runtime. The main trick is that, based on the ENV setting upon creation of the container, we pull environment variables from the desired envs files and load them into the running container. Those environment variables can then be referenced from within serverless.yml and injected into the Lambda functions, hence controlling configuration of the final application by starting from files on our local system. Full details are out of scope, but can be reviewed at http://blog.brianz.bz/post/structuring-serverless-applications-with-python/.

The Makefile and commands I'm running here are not very sophisticated; however, they may appear to be so if you are unfamiliar with Docker or make. I encourage those unfamiliar with them to read through the Makefile targets and do a bit of exploration on their own at https://github.com/brianz/serverless-design-patterns/blob/master/ch2/Makefile. Feel free to open a GitHub issue if you get stuck or need more clarity.

Now that we're inside a container with all of our configuration set from environment variables, we can deploy the entire stack. Our first step is to ensure we have our libraries built and installed into the lib directory. In the Python world, the pip command can help us. Take a look at the Makefile in the repository for details. Our steps for doing the initial deployment are, therefore, as follows:

root@091655eda5d0:/code# make libs
......
# packages now installed in libs
....
root@091655eda5d0:/code# make deploy
cd serverless && sls deploy -s dev
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (5.27 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: coffee-cupping
stage: dev
region: us-west-2
api keys:
None
endpoints:
GET - https://2treukfv8j.execute-api.us-west-2.amazonaws.com/dev/session
POST - https://2treukfv8j.execute-api.us-west-2.amazonaws.com/dev/session
GET - https://2treukfv8j.execute-api.us-west-2.amazonaws.com/dev/session/{id}
DELETE - https://2treukfv8j.execute-api.us-west-2.amazonaws.com/dev/session/{id}
functions:
HandleSession: coffee-cupping-dev-HandleSession
主站蜘蛛池模板: 新邵县| 安福县| 景泰县| 格尔木市| 洪江市| 巴林左旗| 陕西省| 高碑店市| 连城县| 晋州市| 湘乡市| 民勤县| 广河县| 达拉特旗| 通江县| 南部县| 宁蒗| 洞口县| 新龙县| 蓬安县| 资兴市| 高陵县| 甘泉县| 绵竹市| 错那县| 西平县| 桑植县| 绍兴县| 遂昌县| 旬阳县| 建德市| 乌鲁木齐县| 望都县| 得荣县| 新乡县| 克什克腾旗| 海门市| 水富县| 荆州市| 富川| 华宁县|