AWS Serverless Application Model (SAM)

handbook

0
116

A. Installing

1. Install AWS CLI:

curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

2. Configure the AWS CLI

aws configure

3. Create S3 Bucket

aws s3 mb s3://bucketname --region region

4. Install Docker

5. Install Homebrew (package manager)

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

6. Install the AWS SAM CLI

brew tap aws/tap
brew install aws-sam-cli

B. Working

1. Init sam project – default nodejs

sam init --name ${app-name}

Note:

template.yaml: Contains the AWS SAM template that defines your application’s AWS resources

2. Build

Build application’s dependencies, copy application’s source code to aws-sam/build to be zipped and uploaded to Lambda

sam build

3. Package for Deployment

zips everything (Lambda handler source code and any third-party dependencies), and uploads to S3. packaged.yaml file is generated to deploy the application

sam package --output-template packaged.yaml --s3-bucket bucketname

4. Deploy to AWS Cloud

Deploys application to the AWS Cloud.

sam deploy --template-file packaged.yaml --region region --capabilities CAPABILITY_IAM --stack-name aws-sam-getting-started

This command explicitly includes both of the following:

  • The AWS Region to deploy to. This Region must match the Region of the Amazon S3 source bucket.
  • The CAPABILITY_IAM parameter, because creating new Lambda functions involves creating new IAM roles.

5. Test Your Application in the AWS Cloud

6: Testing Your Application Locally

Starts up a local endpoint that replicates your REST API endpoint. It downloads an execution container that you can run your function locally in. The end result is the same output that you saw when you called your function in the AWS Cloud.

sam local start-api

Making One-off Invocations

Invokes your Lambda functions directly, and can pass input event payloads that you provide. With this command, you pass the event payload in the file event.json that’s provided by the sample application

sam local invoke "HelloWorldFunction" -e ./events/event.json

Generate a new input event:

sam local generate-event apigateway aws-proxy --body "" --path "hello" --method GET > api-event.json

LEAVE A REPLY

Please enter your comment!
Please enter your name here