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

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
主站蜘蛛池模板: 奎屯市| 防城港市| 苍南县| 玛纳斯县| 仁寿县| 阿图什市| 灵台县| 晋江市| 张家川| 江油市| 永安市| 银川市| 万山特区| 紫金县| 天气| 萍乡市| 镇赉县| 临武县| 平陆县| 陆河县| 新源县| 桐柏县| 南阳市| 乌什县| 江阴市| 治县。| 吉首市| 灵川县| 开封县| 夏津县| 鲁山县| 巩义市| 广东省| 富阳市| 磐石市| 汶上县| 万安县| 昌都县| 全州县| 无为县| 阳曲县|