SAM : The AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications. It provides shorthand syntax to express functions, APIs, databases, and event source mappings. With just a few lines per resource, you can define the application you want and model it using YAML. During deployment, SAM transforms and expands the SAM syntax into AWS CloudFormation syntax, enabling you to build serverless applications faster. ref – AWS doc.
AWS toolkit : The AWS Toolkit is an open source plug-in that makes it easier to create, debug, and deploy Java and Python applications on Amazon Web Services. Apart from this AWS toolkit provides explorer in IDE like: CloudFormation, CloudWatch logs, DynamoDB, ECR, ECS, Lambda, S3, SqS and Schemas etc.
Assuming you have AWS CLI already install on your machine , in case if not installed, please follow this link,
Here is quick command sharing for MacOs.
#brew install awscli #aws --version
Assuming you have already created your AWS account, now here we are going to configure aws account credentail on local machine for later use. here we will create default profile so it will be auto detected by AWS toolkit.
#aws configure ##it will ask you verious input as below AWS Access Key ID [None]: xxxxxxxx AWS Secret Access Key [None]: xxxxxxx Default region name [None]: xxxxx Default output format [None]: Json
Now, AWS CLI is configured after above command successfully executed.
Let’s installed SAM on local machine.
(assuming you have homebrew install on mac machine)
#brew tap aws/tap #brew install aws-sam-cli #sam -version
In case if you are windows or Linux user please visit this link , here you will find detail installation steps.
Now, if you have successfully installed SAM on local machine, I would suggest you to install “AWS TOOLKIT” plugin into your IDE, in my case it is intellij Idea.
Once AWS toolkit successully installed, you will see the “AWS toolkit” section in bottom left corner of intelliJ idea, once you clicked on this you will see similar window.
Note – We require SAM pre-installed before AWS toolkit to generate SAM project.
Let’s jump on IDE to generate sample AWS serverless project.
Intellij Idea -> File -> Projects -> AWS -> Next -> …
after clicking on ‘create’ button you will see “HelloWorld” project generated.
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.
- HelloWorldFunction/src/main – Code for the application’s Lambda function.
- events – Invocation events that you can use to invoke the function.
- HelloWorldFunction/src/test – Unit tests for the application code.
- template.yaml – A template that defines the application’s AWS resources.
The application uses several AWS resources, including Lambda functions and an API Gateway endpoints. These resources are defined in the
template.yaml file. You can update the template to add AWS resources through the same deployment process that updates your application code. more details you can find in README.md file
Let’s build and deploy project –
#sam build output: Build Succeeded Built Artifacts : .aws-sam/build Built Template : .aws-sam/build/template.yaml
Now sample project is successfully bulid , lets deploy this project, currently we don’t know about the further steps and input so we will use below command with –guided option, here you will asked multiple quesiton by SAM related to env.
#sam deploy --guided OutPut: Configuring SAM deploy ====================== Looking for config file [samconfig.toml] : Not found Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: HelloWorld AWS Region [ap-south-1]: ap-south-1 Confirm changes before deploy [y/N]: y Allow SAM CLI IAM role creation [Y/n]: Y Disable rollback [y/N]: N Save arguments to configuration file [Y/n]: y SAM configuration file [samconfig.toml]: SAM configuration environment [default]: Looking for resources needed for deployment: Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-hyha0acabvgp A different default S3 bucket can be set in samconfig.toml Saved arguments to config file Running 'sam deploy' for future deployments will use the parameters saved above. The above parameters can be changed by modifying samconfig.toml Learn more about samconfig.toml syntax at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
After providing all the inputs , it will initiate deployment of serverless application and provide inline status related to resource creation. let’s visit on aws console page to view, if stack is created or not.
Resources create with helloWorld example,
Now we have verified created resource and they are working fine, lets delete these resources to avoid unnsessery cost.
#sam delete --stack-name HelloWorld output- Are you sure you want to delete the stack HelloWorld in the region ap-south-1 ? [y/N]: y Are you sure you want to delete the folder HelloWorld in S3 which contains the artifacts? [y/N]: y - Deleting S3 object with key HelloWorld/6f1511bb62dde9a7dc63aab2fdc56321 - Deleting S3 object with key HelloWorld/70a14d89c5765111d1922ecf99ce00a5.template - Deleting Cloudformation stack HelloWorld Deleted successfully
Here I have tried to quickly present the simple way to kick start serverless project. let me know if you think any specfic steps should be included into this.