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

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
主站蜘蛛池模板: 库尔勒市| 曲靖市| 巴楚县| 河津市| 吉木乃县| 光山县| 太仆寺旗| 贞丰县| 葫芦岛市| 麻城市| 眉山市| 武山县| 夏津县| 武宣县| 青海省| 汾西县| 招远市| 安徽省| 德州市| 长汀县| 泗水县| 贵港市| 平乐县| 新泰市| 嘉义市| 栾城县| 阜平县| 河西区| 长兴县| 土默特左旗| 卢湾区| 屯留县| 亚东县| 荆门市| 醴陵市| 北安市| 察雅县| 浮梁县| 馆陶县| 攀枝花市| 临泉县|