Key Takeaway of Publishing Lambda Function using Serverless Application Repository (SAR)
Tags: awssar, awslambda, samtemplate
Published: 2019-05-26 08:03:06

---
title: Key Takeaway of Publishing Lambda Function using Serverless Application Repository (SAR)
published: true
description: 
tags: aws-sar, aws-lambda, sam-template
---

The Serverless Application Repository (SAR) is an easy way for the developers to find and deploy existing application in the AWS Cloud. For more info, refer to [official guide](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/what-is-serverlessrepo.html).

One of more popular use cases now is to share the Lambda function with the community. After being published on the AWS SAR, anyone can just deploy the Lambda function directly into their account with just a few clicks. 

There are few ways of publishing on SAR:
- AWS Management Console
- AWS SAM command line interface (AWS SAM CLI)
- AWS SDKs to upload the code.

The easy way is to do it through the **Management Console** and here are some key take-aways:

- Only applications created in `us-east-1` or `us-east-2` allows sharing/visible to the public. Applications created in other regions can be shared privately using **accounts number**. `Semantic version` is also required for publicly published application.


- Ensure that you have created a bucket to host your Lambda code artifact (archive.zip) and amended the bucket policy with the following:

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service":  "serverlessrepo.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<your-bucket-name>/*"
        }
    ]
}
```

- Description and License info on the SAR is compulsory.

- The most important one is to create a valid AWS Serverless Application Model (AWS SAM) that define the AWS resources. The full documentation is [here](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md). But, these are some samples on my Github: 
-- [Example 1](https://github.com/mengjiann/aws-lambda-sqs/blob/master/template-sar.yaml) - This contains addition of multiple role policies required for the Lambda. `Parameters` are also used in this examples to take in inputs for the SAR application.
-- [Example 2](https://github.com/mengjiann/aws-lambda-s3/blob/master/template-sar.yaml) - This contains creation of a S3 Bucket and adds listened `Events` to the objects created in the S3 Bucket.
-- [Example 3](https://github.com/mengjiann/ssm-parameters-getter-lambda/blob/master/template/sar-template.yaml) - This contains creation of `Role` and `RolePolicy` resources for attached to the Lambda function. `DependsOn` is also used to ensure the resources are created in correct order.

- Once your have published a new version of the application on SAR, the users need to redeploy the application with latest version. Otherwise, they will remain on the older version.

- Once an application is **deployed** using SAR, you can check the status from the Cloud Formation's Stacks console. You can remove the application from there and check if there is any issues with the deployment.



Reference:
- What is AWS SAR: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/what-is-serverlessrepo.html
- How to publish on SAR: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/serverlessrepo-how-to-publish.html