The AWS Serverless Application Model CLI

In this section, we will walk through different features of SAM Local with fully-working examples. For local testing, you can use Python and bash like I have shown or you can also use the SAM CLI (https://github.com/awslabs/aws-sam-cli), which at the time of writing is still in beta. It uses Docker and is based on open source docker-lambda (https://github.com/lambci/docker-lambda) Docker images. If you are using Windows 10 Home, I recommend you upgrade to Pro or Enterprise as it's harder to get Docker working on the Home edition. There are also some hardware requirements, such as virtualization, to be aware of. We need to perform the following steps:

  1. Install the AWS CLI (https://docs.aws.amazon.com/cli/latest/userguide/installing.html).
  2. Install Docker CE (https://docs.docker.com/install/).
  3. Install the AWS SAM CLI (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html).
  4. For Linux, you can run the following:
$sudo pip install --user aws-sam-cli 
  1. For Windows, you can install AWS SAM CLI using an MSI.
  1. Create a new SAM Python 3.6 project, sam-app, and docker pull the images (this should happen automatically but I needed to do pull to get it to work):
$ sam init --runtime python3.6
$ docker pull lambci/lambda-base:build
$ docker pull lambci/lambda:python3.6
  1. Invoke the following function:
$ cd sam-app
$ sam local invoke "HelloWorldFunction" -e event.json --region eu-west-1

You will get the following:

Duration: 8 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
{"statusCode": 200, "body": "{"message": "hello world"}"}

This can be used to add automated testing.

  1. Start the local Lambda endpoint:
$ sam local start-lambda --region eu-west-1
# in new shell window
$ aws lambda invoke --function-name "HelloWorldFunction"
--endpoint-url "http://127.0.0.1:3001" --no-verify-ssl out.txt

This starts a Docker container that emulates AWS Lambda with an HTTP server locally, which you can use to automate the testing of your Lambda functions from the AWS CLI or Boto3.

  1. Start an API and test it using the following:
$ sam local start-api --region eu-west-1
# in new shell window
$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://127.0.0.1:3000/hello

This starts a Docker container with an HTTP server locally, which you can use to automate the testing of the API you can use with curl, Postman, or your web browser.

  1. One way to generate sample events is to print out the event from a Lambda, and copy it from CloudWatch logs (my preference). Another way is to use sam local, which can generate some examples events. For example, you can run the following:
$ sam local generate-event apigateway aws-proxy 

Personally, I haven't used the SAM CLI extensively as it is very new, needs Docker installed, and is still in beta. But it does look promising, and as another tool to test your serverless stack, it's useful that it can simulate a Lambda in Docker container that exposes an endpoint, and I expect more features to be added in the future.

Perhaps less usefully, it also wraps some of the existing command's serverless package and deployment commands as an alias for the CloudFormation ones. I think this is done to keep them all in one place.

  1. Here is an example of the SAM CLI package and deploy commands:
$ sam package 
--template-file $template.yaml
--output-template-file ../../package/$template-output.yaml
--s3-bucket
$bucket $ sam deploy
--template-file ../../package/$template-output.yaml
--stack-name $template
--capabilities CAPABILITY_IAM

CloudFormation with SAM to package and deploy commands:

$ aws cloudformation package 
--template-file $template.yaml
--output-template-file ../../package/$template-output.yaml
--s3-bucket $bucket
--s3-prefix $prefix
$ aws cloudformation deploy
--template-file ../../package/$template-output.yaml
--stack-name $template
--capabilities CAPABILITY_IAM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset