Girish Bhatia: 2024 CommunityDay AWS Lambda develop locally with SAM, Docker & VSCode

awschicago 53 views 30 slides Jun 26, 2024
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

AWS Community Day Midwest 2024
Girish Bhatia Getting Started with AWS SAM: Developing Lambda Functions Locally with Docker, Python, and VSCode


Slide Content

MIDWEST | OHIO

AWS Lambda Develop Locally
With
SAM, Docker, Python & VSCode
Girish Bhatia | 2024
AWS cloud technology enthusiast
Technology Program Manager

About Girish Bhatia
I am an AWS Cloud Technology Enthusiast & Technology Program Delivery Leader. I love to
deliver cloud-based software solution in alignment with strategic goals for business &
technology modernization.
Qualifications:
❏Master of Science in Computer Information Systems
❏AWS Certified Developer Associate | Amazon Web Services
❏AWS Certified Solution Architect Associate | Amazon Web Services
❏AWS Certified Cloud Practitioner | Amazon Web Services
❏AWS Badges : Serverless, Solution Architecture
❏Microsoft Certified: Azure Fundamental
❏Microsoft Certified professional: Solution Architecture
❏Microsoft Certified .NET Solution Developer
❏Project Management Professional (PMP)
❏Certified Scrum Master (Scrum Alliance)
IT Experience:
❏20+ years in IT
❏Tech Program Delivery -Tech Mod/Cloud Transformation
❏Software Development Python, Java, .NET, Oracle, APIs, BI tools
❏in telecom, public/state system, banking/finance domain.
❏Onshore/offshore software delivery
❏Tech Program/Project Management
❏Journey to Cloud, Tech Modernization
❏On Prim to Cloud Migration
❏Cloud/API Integration B2B
❏Data Center planning/Mainframe relocation/upgrades
https://cloudwithgirish.com@CloudWithGirish

What we are building today?
Develop, Test and Debug Lambda function locally
Let's learn how to make Lambda functions using AWS SAM in VSCode. We'll start with a demo, then create a
function that gives back a state name when you input a state code. We'll set up debugging in VSCode, test the
function locally, and once it works as expected, deploy it to AWS using SAM deploy.

Tools & Services
•Environment Setup
•VSCode+ AWS Toolkit + SAM demo
•Build getState function
•Use Python dictionary
•Build
•Invoke locally
•Debug
•Deploy the function
•SAM uses CloudFormation to create a stack.
•S3 bucket is created for the artifacts
•SAMConfig file is created, can be customized
by developer

Architecture Pattern

Know your budget and watch your cost!
•Setup Budget
•Familiarize yourself with cost explorer

LambdaAWS BudgetAWS Cost Explorer

•sam init
ØVS Code AWS Toolkit
ØBuild template.yaml

Setup Environment
oInstall VSCode
oInstall AWS toolkit
oInstall Docker
oInstall SAM CLIobrew install aws/tap/aws-sam-cliobrew upgrade aws-sam-cliosam --version
oInstall Python

Quick Demo
In VSCode, Navigate to AWS Toolkit
Right Click on Lambda

Quick Demo
Select Python 3.12

Quick Demo
Select AWS SAM Hello World

Quick Demo
Select a Folder to save your code
Name your app

Quick Demo
Open the app folder and look at few files (event.json. Template.yaml. App.py)

Quick Demo
Build the code using SAM Build
sam Build
Invoke the function
sam local invoke HelloWorldFunction-e events/event.json
Review output.
Make change to the function, build, and run again!
Let's invoke now as API
Run the API locally
sam local start-api
Open postman or a browser and test the function!

Serverless Application Model
&
Lambda Function

Steps to create & invoke Lambda function locally
oCreate src and events folder
oCreate Lambda function
oCreate template.yaml
oCreate event.json
osam build
osam local invoke

Lambda Function (python)Partial code!
Full src code in git repo

Template.yaml

Event.json

SAM Commands
# build your lambda
sam build
# invoke lambda locally
sam local invoke <functionName> -e <location of the event.json>
sam local invoke getStateFn-e events/event.json

Build Function
Function Output

VSCode Debug
Debug does not work!
Add debug configuration
–launch.json

Debug in action

Lambda function developed locally, what’s next?
oDeploy it using SAMoSAM uses CloudFormation to create a stack.oS3 bucket is created for the artifactsoa SAMConfig file is created
oCommand is –> sam deploy --guided

AWS SAM Commands Reference
osam buildosam local generate-event apigatewayaws-proxyosam local invoke <function > -e <event json >osam local start-apiosam deploy –guidedosam deploy ( update stack )osam deleteoaws cloudformation delete-stack --stack-name your-stack-name

Delete the stack so that you don’t incur charges!
osam deleteoaws cloudformation delete-stack --stack-name getStateApp

AWS Services used:
oAWS BudgetoAWS Cost ExploreroAWS SAMoAWS LambdaoAWS S3oAWS CloudFormationoAWS IAM
oVS CodeoAWS ToolkitoGitHuboDocker

Thank you!
LinkedIn: https://www.linkedin.com/in/girishbhatia/
Website: https://cloudwithgirish.com
GitHub: Code/slide available: