Creating Serverless application using SAM
Hi
Welcome to the tutorial on how to create serverless application using AWS. This tutorial will cover the steps required to create one such application.
SAM refers to Serverless Application Model. In simple terms it is a template to define the resources that AWS cloudformation will provision. Documentation for SAM can be found here.
Question: Why do I need SAM when I can create resources cloud formation?
Answer: SAM template has a special type called serverless and it specially supports functions, api and table. However SAM itself supports few more services which can be found here. SAM abstracts lot of the complexities and simpler compared to cloud formation. SAM also has a cli which can be used for local testing and speeds development time.
The basic template of SAM looks like below
Under the resources section we define the resources required for our application with the properties applicable for each resource.
In order to create lambda function as well as API gateway resource we have few choices available. We need to choose the one that best fits the requirement. Let us look at each resource type with their advantages and disadvantages.
The official SAM documentation site has several examples. But if we want to quickly generate SAM template for custom lambda we can use below approach.
Login to the AWS console and choose Lambda from the services. Click create function. A screen similar to below screenshot should appear.
Give a name for the lambda. Default run time is Node js. You can also use Python for inline coding. For other languages like Java or .Net or Go a zip file containing the code needs to be uploaded. For our example let us use python. For role we can create a new lambda role or select an already existing role. Provide a meaningful name for the role. A sample role name is app-lambda-execution-role. Default implementation of lambda in should look like below
def lambda_handler(event, context):
# TODO implement
return 'Hello from Lambda'
To generate the SAM template for this lambda click Actions drop down in the lambda console. Choose the export menu in the drop down. click Download AWS SAM File button
Gotchas
The above snippet shows minimal configuration for SAM template to create lambda function. As discussed earlier we could extract common properties and move them to Global section. Below snippet is a more detailed one with all the global properties listed
In the above snippet, the hello resource (lambda function) is going to inherit values provided in the global section. However if we want we could re-define the same property again within the lambda resource. This would cause the property to either override the value or extend the global value(s) based on the property type. In part 2 of this blog we will go over API gateway creation using SAM.
Welcome to the tutorial on how to create serverless application using AWS. This tutorial will cover the steps required to create one such application.
SAM refers to Serverless Application Model. In simple terms it is a template to define the resources that AWS cloudformation will provision. Documentation for SAM can be found here.
Question: Why do I need SAM when I can create resources cloud formation?
Answer: SAM template has a special type called serverless and it specially supports functions, api and table. However SAM itself supports few more services which can be found here. SAM abstracts lot of the complexities and simpler compared to cloud formation. SAM also has a cli which can be used for local testing and speeds development time.
The basic template of SAM looks like below
AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Resources:
Under the resources section we define the resources required for our application with the properties applicable for each resource.
In order to create lambda function as well as API gateway resource we have few choices available. We need to choose the one that best fits the requirement. Let us look at each resource type with their advantages and disadvantages.
A lambda function get created if a resource is defined as Type: AWS::Serverless::Function or
Type: AWS::Lambda::Function. Similarly an API gateway resource gets created if the resource is defined as Type: AWS::Serverless::Api or Type: AWS::ApiGateway::RestApi.
When we use the first type for definition (containing serverless)we get to use a recently released feature called Globals. The two major types that are available in global section are for
Global section helps to avoid configuration duplication across multiple resources of same type.
But since SAM is a new feature it does not have all the properties supported by vanilla cloudformation syntax which is available in AWS: ApiGateway::RestApi.Type: AWS::Lambda::Function. Similarly an API gateway resource gets created if the resource is defined as Type: AWS::Serverless::Api or Type: AWS::ApiGateway::RestApi.
When we use the first type for definition (containing serverless)we get to use a recently released feature called Globals. The two major types that are available in global section are for
API Gateway (AWS::Serverless::Api)
Lambda Function (AWS::Serverless::Function)Global section helps to avoid configuration duplication across multiple resources of same type.
The official SAM documentation site has several examples. But if we want to quickly generate SAM template for custom lambda we can use below approach.
Login to the AWS console and choose Lambda from the services. Click create function. A screen similar to below screenshot should appear.
Give a name for the lambda. Default run time is Node js. You can also use Python for inline coding. For other languages like Java or .Net or Go a zip file containing the code needs to be uploaded. For our example let us use python. For role we can create a new lambda role or select an already existing role. Provide a meaningful name for the role. A sample role name is app-lambda-execution-role. Default implementation of lambda in should look like below
def lambda_handler(event, context):
# TODO implement
return 'Hello from Lambda'
To generate the SAM template for this lambda click Actions drop down in the lambda console. Choose the export menu in the drop down. click Download AWS SAM File button
Gotchas
- Default settings creates timeout to be 3 sec. This is not ideal for most cases. Increase the value to atleast 30 secs. The maximum value allowed is 300 sec (5 minutes).
- The default memory size is 128MB. Increase it to higher value if required based on the necessity. If the lambda performs connection to a RDS or Dynamo db it would be worth to increase it to 512 MB or 1024 MB. Recently there was an update to the maximum value available and it is now at 3008 MB.
- One key point to keep in consideration is the strong correlation between memory size and cpu power. There is no separate CPU configuration for lambdas. Changing the memory size will scale the cpu automatically.
- Default settings for lambda is no VPC. If the lambdas needs to be launched within a VPC (especially for enterprise) we need to configure that explicity. SAM template exposes these properties which can be used to provide vpc name, security group and the subnets.
AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Description: An AWS Serverless Specification template describing your function. Resources: hello: Type: 'AWS::Serverless::Function' Properties: Handler: lambda_function.lambda_handler Runtime: python2.7 CodeUri: . Description: 'Hello world lambda function in Python' MemorySize: 128 Timeout: 30 Role: 'arn:aws:iam::12345678901234:role/service-role/role-to-run-lambda'
The above snippet shows minimal configuration for SAM template to create lambda function. As discussed earlier we could extract common properties and move them to Global section. Below snippet is a more detailed one with all the global properties listed
AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Description: An AWS Serverless Specification template describing your function. Globals: Function: DeadLetterQueue: VpcConfig: Tags: Tracing: KmsKeyArn: AutoPublishAlias: true DeploymentPreference: Runtime: python2.7 CodeUri: . Timeout: 180 MemorySize: 128 Environment: Variables: TABLE_NAME: data-table Resources: hello: Type: 'AWS::Serverless::Function' Properties: Handler: lambda_function.lambda_handler Description: 'Hello world lambda function in Python' Role: 'arn:aws:iam::12345678901234:role/service-role/role-to-run-lambda'
In the above snippet, the hello resource (lambda function) is going to inherit values provided in the global section. However if we want we could re-define the same property again within the lambda resource. This would cause the property to either override the value or extend the global value(s) based on the property type. In part 2 of this blog we will go over API gateway creation using SAM.
Comments
Post a Comment