OFF	THE	CLOUD	WITH
SERVERLESS	APPLICATION	
MODEL	(SAM)
AWS	MEETUP	– FLASH	PRESENTATION
ALEX
SPICYKEY LLC
@SPICYKEY
OFF	THE	CLOUD
• Problem:	We	love	lambdas	in	the	cloud	to	a	failing.		
• Symptoms:	
• Our	favorite	debugging	tools	are	on	our	laptops
• Local	integration	testing	is	easier	than	on	the	cloud
• We	have	to	meet	a	sprint	deadline	and	are	coding	on	our	laptops	during	a	long	cross	country	flight	without	
internet	access	to	meet	that	deadline
• You	are	on	a	NASA	space	station	and	are	preparing	for	your	AWS	Certification	Exam	without	internet	access
@spicykey
EXAMPLE:	A	
SERVERLESS
APPLICATION	
MODEL	TEMPLATE	
1	LAMBDA
1	API
@spicykey
START-API		….	HTTP://LOCALHOST:3000
@spicykey
HOW	DO	I	RUN	A	LAMBDA	FUNCTION	LOCALLY?
• Install	aws-sam-local
• Create	a	Serverless Application	Model	(SAM)	template	file	in	YAML	or	JSON
• In	your	favorite	shell,	execute	‘sam local	invoke’
@spicykey
WHY	IS	THIS	HARD
• Software	Prerequisites
• Go
• Docker
• AWS	command	line	tools
• AWS	SAM	local	tools
• A	debugger	that	can	easily	connect	to	a	remote	process	by	port
• IAM	Prerequisites
• A	profile	with	IAM	rights	on	your	laptop
• Configuration
• Docker	networks
• Capturing	lambda	logs	locally
• Must	learn	template	language	and	write	templates	by	hand
@spicykey
LOCAL	DYNAMODB +	LOCAL	LAMBDA
• Run	DynamoDB on	your	laptop,	http://localhost:8000
• Use	Docker	18.3+	on	a	Mac	OS	X	or	Windows	laptop	for	easy	Docker	Network	routing	to	the	host	
(http://host.docker.internal:8000)
• Route	the	lambda	function’s	dynamodb client	to	your	laptop’s	DynamoDB endpoint	(javascript/nodejs
example)
• const dynamodb =	new	AWS.DynamoDB({endpoint:	'http://host.docker.internal:8000'});
@spicykey
SCENARIO	– WEBSITE	DEVELOPMENT
• Example:	A	react	developer	creates	a	progressive	website	that	accesses	APIs	via	fetch().		The	developer	
starts	the	web	application	locally.	Two	commands:	‘npm start’	and	‘sam local	start-api’.		The	website	is	
accessible	at	http://localhost:3000 and	the	APIs	are	accessible	at	http://localhost:3001.		The	AWS	
Lambda	function	uses	aws-serverless-express	to	route	APIs	to	code	within	the	lambda	function.		The	
lambda	function	authorizes	http://localhost:3000 with	CORS.
@spicykey
SCENARIO	– LAMBDA	DEVELOPMENT
• Example:	A	developer	wants	to	enhance	a	lambda	function	by	improving	the	code.	The	developer	pulls	
the	source	code	for	the	lambda	on	to	their	laptop.		Then,	write	a	SAM	template	for	the	lambda	
function.		After	that,	the	developer	creates	a	JSON	document	with	an	example	input	event.	The	
developer	invokes	the	lambda	with	‘sam local	invoke	–e	sample-event.json’
@spicykey
SCENARIO	– S3	EVENT	DEVELOPMENT
• Example:	A	developer	has	a	javascript based	lambda	function	that	responds	to	S3	events	that	is	buggy.			
The	developer	creates	a	project	in	visual	studio	for	the	lambda	function.		The	developer	then	creates	a	
SAM	template	for	the	function.		First,	the	developer	runs	visual	studio	and	sets	a	breakpoint.		Then,	
invokes	the	function	with	‘sam local	invoke	–d	5858.’	
@spicykey
THREE	IMPORTANT	LINKS
• Serverless Application	Model	(SAM)	Templates	- https://github.com/awslabs/serverless-application-
model
• SAM	Local	Command	Line	Interface	(CLI)	- https://github.com/awslabs/aws-sam-local
• Local	DynamoDB -
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
@spicykey
THANK	YOU	– ABOUT	ME
• #Serverless
• #Alexa
• #Code
@spicykey
SpicyKey	LLC	develops	apps	for	clients
https://spicykey.com https://youtube.com/spicykey

Serverless Application Model - Executing Lambdas Locally