AWS SAM for Serverless

AWS SAM for Serverless

#Getting Started with AWS SAM

To get started with AWS SAM, use the AWS SAM CLI to create a serverless application that you can package and deploy in the AWS Cloud. You can run the application both in the AWS Cloud or locally on your development host.

Setup AWS SAM CLI to deploy locally - Installing the AWS SAM CLI

AWS SAM provides you with a command line tool, the AWS SAM CLI, that makes it easy for you to create and manage serverless applications. You need to install and configure a few things in order to use the AWS SAM CLI.

To install the AWS SAM CLI, follow below steps (These steps are for macOS) for other OS please check the documentation here ( linux, windows)

The following steps help you to install and configure the required prerequisites for using the AWS SAM CLI on your macOS host: * Create an AWS account.

  • Configure IAM permissions

  • Install Docker. Note: Docker is only a prerequisite for testing your application locally.

  • Install Homebrew.

  • Install the AWS SAM CLI.

I am directly jumping to last step ** Install the AWS SAM CLI **

We can install AWS SAM CLI using either ** brew ** or ** pip **

Installation using brew :

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

Installation using pip :

$mkdir aws_lambda; cd aws_lambda; mkdir sam_test; cd sam_test;
$python3.8 -m venv venv_sam
$source venv_sam/bin/activate
$pip install aws-sam-cli

To check whether installation is fine, execute below command.


#if installed from brew
$sam --version

#if installed from pip
$source  venvironment_dir/bin/activate
$sam --version

#you should get output as below
SAM CLI, version 0.41.0

#Configuring AWS Credentials with a profile name

$aws configure --profile dev_profile_us_east
AWS Access Key ID [None]: your_access_key_id
AWS Secret Access Key [None]: your_secret_access_key
Default region name [None]:us-east-1
Default output format [None]: json

To check the setup, try below command to list all instance details in the region

$ aws ec2 describe-instances --profile dev_profile_us_east

#output if you don't have any instances
{
    "Reservations": []
}

Creating a HelloWorld App and testing it locally

$sam init

	SAM CLI now collects telemetry to better understand customer needs.

	You can OPT OUT and disable telemetry collection by setting the
	environment variable SAM_CLI_TELEMETRY=0 in your shell.
	Thanks for your help!

	Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html

Which template source would you like to use?
	1 - AWS Quick Start Templates
	2 - Custom Template Location
Choice: 1

Which runtime would you like to use?
	1 - nodejs12.x
	2 - python3.8
	3 - ruby2.5
	4 - go1.x
	5 - java11
	6 - dotnetcore2.1
	7 - nodejs10.x
	8 - python3.7
	9 - python3.6
	10 - python2.7
	11 - java8
	12 - dotnetcore2.0
	13 - dotnetcore1.0
Runtime: 8

Project name [sam-app]: 

Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git

AWS quick start application templates:
	1 - Hello World Example
	2 - EventBridge Hello World
	3 - EventBridge App from scratch (100+ Event Schemas)
Template selection: 1

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.7
Dependency Manager: pip
Application Template: hello-world
Output Directory: .

Next steps can be found in the README file at ./sam-app/README.md

#What AWS SAM is doing:

This command creates a directory with the name you provided as the project name. The contents of the project directory are similar to the following (these contents are created when one of the Python runtimes and the Hello World Example are chose):

sam-app/
	### README.md
	### events/
	# 	### event.json
	### hello_world/
	# 	### __init__.py
	# 	### app.py #Contains your AWS Lambda handler logic.
	# 	### requirements.txt #Contains any Python dependencies the application requires,
	used for sam build

	### template.yaml #Contains the AWS SAM template defining your application's AWS
 resources.

 ### tests/
 	 ### unit/
 	 ### __init__.py
 	 ### test_handler.py

There are three especially important files:

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

  • hello_world/app.py: Contains your actual Lambda handler logic.

  • hello_world/requirements.txt: Contains any Python dependencies that the application requires, and is used for sam build.

#Now testing the simple HelloWorld API locally

$ sam local start-api

#output

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-02-20 14:47:46  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking app.lambda_handler (python3.7)

Fetching lambci/lambda:python3.7 Docker container image................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Mounting /Users/leeladharan/willow/aws_lambda/wca_sam/sam-app/hello_world as /var/task:ro,delegated inside runtime container
START RequestId: ab462a1c-e20f-1896-edac-756d75e455e0 Version: $LATEST
END RequestId: ab462a1c-e20f-1896-edac-756d75e455e0
REPORT RequestId: ab462a1c-e20f-1896-edac-756d75e455e0	Init Duration: 280.13 ms	Duration: 6.23 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 23 MB	
No Content-Type given. Defaulting to 'application/json'.
2020-02-20 14:49:40 127.0.0.1 - - [20/Feb/2020 14:49:40] "GET /hello HTTP/1.1" 200 -
2020-02-20 14:49:41 127.0.0.1 - - [20/Feb/2020 14:49:41] "GET /favicon.ico HTTP/1.1" 403 -

#In browser on visiting http://127.0.0.1:3000/hello Output is as below

{"message": "hello world"}
Published:
comments powered by Disqus