- Serverless Design Patterns and Best Practices
- Brian Zambrano
- 460字
- 2021-08-27 19:12:06
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/.
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