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

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
主站蜘蛛池模板: 鹿泉市| 湾仔区| 呼和浩特市| 武冈市| 莎车县| 绿春县| 柏乡县| 公主岭市| 洪湖市| 利辛县| 二连浩特市| 平罗县| 淮滨县| 拉萨市| 滨州市| 关岭| 繁昌县| 乐东| 桐乡市| 涪陵区| 北宁市| 孟州市| 和硕县| 沁水县| 博罗县| 乌鲁木齐市| 汤阴县| 江永县| 托克托县| 泗水县| 曲麻莱县| 大同市| 桦南县| 石台县| 临安市| 周口市| 随州市| 柳江县| 乐昌市| 隆德县| 淮北市|