Create and Deploy Your AWS SAM Application
Sanjay Kumar ????
Senior Consultant at EY India | Backend Development Specialist | Solution Architect | Artist | Spiritual
Let's see How to Create and Deploy Your First Lambda Function on AWS SAM (Serverless Application Modal)
You should have your AWS account firstly and Software Requirements are Below:
To install AWS CLI in your system as per your OS you follow the official document here.
To install Serverless Application Model CLI in your system you can follow the official document here as per your operating system.
Docker is optional here, cause if you want to run your lambda locally on your system then only docker requires otherwise if you will run your Lambda in AWS console itself then Docker not required at all. for installing docker in your local machine you can follow the official document here.
Note: When we are taking about Serverless Architecture with Lambda so we have two ways first Serverless.com from in which we can deploy our lambda and second is AWS SAM in which via using AWS Lambda service of AWS we can deploy our lambda. Here we are mainly discussing about AWS SAM only.
Now All Will Happen with Command Line Interface:
Now open Terminal and Go to your directory in which you want to create your project.
$ sam init? #hit enter this command.
~ This will appear 2 option, 1 AWS Quick Start and 2 Custom Template
For making things simple you can 1 at moment.
~ Now you need to select template, at moment you can select Hello World Example.
~ Now it will ask for programming language Python by default, and you can select other as well such as Node.js, Java, etc.
~ Now you can choose ZIP option.
~ Now it will ask starter template, you can select anyone from that Hello World Example.
~ X-Ray tracing you can enable so select Y.
~ Enable Cloud Watch now with select Y.
~ Now it asks for project name, by default sam-app was given you can change it also.
领英推荐
Now Your Application Code Base Created Successfully
Now Make Build of Your SAM Application
Now go to your project directory with?
~ $ cd directory-name
~ $ sam build #this command will generate build source code
~ $ sam validate #to check your template
~ $ sam local invoke #to run your lambda function in local, this will run with Docker.
Congrats, now your Lambda is ready to deploy in your AWS account :)
Firstly now you have to configure your AWS CLI.
~ $ aws configure
~ $ sam deploy --guided
#This above command will deploy your code on AWS S3, Lambda, API Gateway and Cloud Formation.
That's it, you have successfully deployed your SAM Lambda Application in AWS ??
Helpful Commands:
AWS CLI Commands with Description:
~ $ aws configure? #......... default .........
~ $ aws configure --profile new-profile-name #........ configure with profile name .............
~ $ aws configure list-profiles #........ get list of all configured profiles
~ $ export AWS_PROFILE=new-profile-name #........ select specific profile
~ $ aws sts get-caller-identity #....... show the credentials if configured .....
~ $ aws cloudformation delete-stack --stack-name app-name --profile profile-name #.... delete whole application from backend cloud ... Reference
AWS CLI Commands with Description:
~ $ sam deploy --guided? #....... deploy with default configuration ..........
~ $ sam deploy --profile profile-name --guided #........ deploy with specific profile name .......
~ $ sam local start-api #...... we can run apis locally ........
~ $ sam local invoke -e events/event.json #..... invoke application with event parameters ......
~ $ sam local invoke "SpecificFunctionName" -e events/event.json
# ........ invoke locally specific lambda function .......
Happy Coding </>
Follow me for more informative content like this :)