The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
The AWS Serverless Application Model (SAM) is licensed under The Apache License, Version 2.0.
AWS SAM is a model used to define serverless applications on AWS.
Serverless applications are applications composed of functions triggered by events. A typical serverless application consists of one or more AWS Lambda functions triggered by events such as object uploads to Amazon S3, Amazon SNS notifications, and API actions. Those functions can stand alone or leverage other resources such as Amazon DynamoDB tables or S3 buckets. The most basic serverless application is simply a function.
AWS SAM is based on AWS CloudFormation. A serverless application is defined in a CloudFormation template and deployed as a CloudFormation stack. An AWS SAM template is a CloudFormation template.
AWS SAM defines a set of objects which can be included in a CloudFormation template to describe common components of serverless applications easily.
The files describing a serverless application in accordance with AWS SAM are JSON or YAML formatted text files. These files are CloudFormation templates.
AWS SAM introduces several new resources and property types that can be embedded into the Resources section of the template. The templates may include all other template sections and use CloudFormation intrinsic functions to access properties available only at runtime.
In order to include objects defined by AWS SAM within a CloudFormation template, the template must include a Transform section in the document root with a value of AWS::Serverless-2016-10-31.
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs4.3
CodeUri: 's3://my-bucket/function.zip'All property names in AWS SAM are case sensitive.
Creates a Lambda function, IAM execution role, and event source mappings which trigger the function.
| Property Name | Type | Description |
|---|---|---|
| Handler | string |
Required. Function within your code that is called to begin execution. |
| Runtime | string |
Required. The runtime environment. |
| CodeUri | string |
Required. S3 Uri to the function code. The S3 object this Uri references MUST be a Lambda deployment package. |
| Description | string |
Description of the function. |
| MemorySize | integer |
Size of the memory allocated per invocation of the function in MB. Defaults to 128. |
| Timeout | integer |
Maximum time that the function can run before it is killed in seconds. Defaults to 3. |
| Role | string |
ARN of an IAM role to use as this function's execution role. If omitted, a default role is created for this function. |
| Policies | string | List of string | IAM policy document object | List of IAM policy document object |
Names of AWS managed IAM policies or IAM policy documents that this function needs, which should be appended to the default role for this function. If the Role property is set, this property has no meaning. |
| Environment | Function environment object | Configuration for the runtime environment. |
| VpcConfig | VPC config object | Configuration to enable this function to access private resources within your VPC. |
| Events | Map of string to Event source object |
A map (string to Event source object) that defines the events that trigger this function. Keys are limited to alphanumeric characters. |
When the logical ID of this resource is provided to the Ref intrinsic function, it returns the resource name of the underlying Lambda function.
When the logical ID of this resource is provided to the Fn::GetAtt intrinsic function, it returns a value for a specified attribute of this type. This section lists the available attributes.
| Attribute Name | Description |
|---|---|
| Arn | The ARN of the Lambda function. |
Handler: index.js
Runtime: nodejs4.3
CodeUri: 's3://my-code-bucket/my-function.zip'
Description: Creates thumbnails of uploaded images
MemorySize: 1024
Timeout: 15
Policies: AmazonS3FullAccess
Environment:
Variables:
TABLE_NAME: my-table
Events:
PhotoUpload:
Type: S3
Properties:
Bucket: my-photo-bucketCreates a collection of Amazon API Gateway resources and methods that can be invoked through HTTPS endpoints.
An AWS::Serverless::Api resource need not be explicitly added to a AWS Serverless Application Definition template. A resource of this type is implicitly created from the union of Api events defined on AWS::Serverless::Function resources defined in the template that do not refer to an AWS::Serverless::Api resource. An AWS::Serverless::Api resource should be used to define and document the API using Swagger, which provides more ability to configure the underlying Amazon API Gateway resources.
| Property Name | Type | Description |
|---|---|---|
| StageName | string |
Required The name of the stage, which API Gateway uses as the first path segment in the invoke Uniform Resource Identifier (URI). |
| DefinitionUri | string |
Required S3 URI to the Swagger document describing the API. |
| CacheClusterEnabled | boolean |
Indicates whether cache clustering is enabled for the stage. |
| CacheClusterSize | string |
The stage's cache cluster size. |
| Variables | Map of string to string |
A map (string to string map) that defines the stage variables, where the variable name is the key and the variable value is the value. Variable names are limited to alphanumeric characters. Values must match the following regular expression: [A-Za-z0-9._~:/?#&=,-]+. |
When the logical ID of this resource is provided to the Ref intrinsic function, it returns the resource name of the underlying API Gateway RestApi.
StageName: prod
DefinitionUri: swagger.ymlThe AWS::Serverless::SimpleTable resource creates a DynamoDB table with a single attribute primary key. It is useful when data only needs to be accessed via a primary key. To use the more advanced functionality of DynamoDB, use an AWS::DynamoDB::Table resource instead.
| Property Name | Type | Description |
|---|---|---|
| PrimaryKey | Primary Key Object | Attribute name and type to be used as the table's primary key. This cannot be modified without replacing the resource. Defaults to String attribute named ID. |
| ProvisionedThroughput | Provisioned Throughput Object | Read and write throughput provisioning information. Defaults to 5 read and 5 write capacity units per second. |
When the logical ID of this resource is provided to the Ref intrinsic function, it returns the resource name of the underlying DynamoDB table.
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5The object describing an event source with type S3.
| Property Name | Type | Description |
|---|---|---|
| Bucket | string |
Required. S3 bucket name. |
| Events | string | List of string |
Required. See Amazon S3 supported event types for valid values. |
| Filter | Amazon S3 notification filter | Rules to filter events on. |
NOTE: To specify an S3 bucket as an event source for a Lambda function, both resources have to be declared in the same template. AWS SAM does not support specifying an existing bucket as an event source.
Bucket: my-photo-bucket
Events: s3:ObjectCreated:*The object describing an event source with type SNS.
| Property Name | Type | Description |
|---|---|---|
| Topic | string |
Required. Topic ARN. |
Topic: arn:aws:sns:us-east-1:123456789012:my_topicThe object describing an event source with type Kinesis.
| Property Name | Type | Description |
|---|---|---|
| Stream | string |
Required. ARN of the Amazon Kinesis stream. |
| StartingPosition | string |
Required. One of TRIM_HORIZON or LATEST. |
| BatchSize | integer |
Maximum number of stream records to process per function invocation. |
Stream: arn:aws:kinesis:us-east-1:123456789012:stream/my-stream
StartingPosition: TRIM_HORIZON
BatchSize: 10The object describing an event source with type DynamoDB.
| Property Name | Type | Description |
|---|---|---|
| Stream | string |
Required. ARN of the DynamoDB stream. |
| StartingPosition | string |
Required. One of TRIM_HORIZON or LATEST. |
| BatchSize | integer |
Maximum number of stream records to process per function invocation. |
Stream: arn:aws:dynamodb:us-east-1:123456789012:table/TestTable/stream/2016-08-11T21:21:33.291
StartingPosition: TRIM_HORIZON
BatchSize: 10The object describing an event source with type Api.
If an AWS::Serverless::Api resource is defined, the path and method values MUST correspond to an operation in the Swagger definition of the API. If no AWS::Serverless::Api is defined, the function input and output are a representation of the HTTP request and HTTP response.
| Property Name | Type | Description |
|---|---|---|
| Path | string |
Required. Uri path for which this function is invoked. MUST start with /. |
| Method | string |
Required. HTTP method for which this function is invoked. |
| RestApiId | string |
Identifier of a RestApi resource which MUST contain an operation with the given path and method. Typically, this is set to reference an AWS::Serverless::Api resource defined in this template. If not defined, a default AWS::Serverless::Api resource is created using a generated Swagger document contains a union of all paths and methods defined by Api events defined in this template that do not specify a RestApiId. |
Path: /photos
Method: postThe object describing an event source with type Schedule.
| Property Name | Type | Description |
|---|---|---|
| Schedule | string |
Required. Schedule expression, which MUST follow the schedule expression syntax rules. |
| Input | string |
JSON-formatted string to pass to the function as the event body. |
Schedule: rate(5 minutes)The object describing an event source with type CloudWatchEvent.
| Property Name | Type | Description |
|---|---|---|
| Pattern | Event Pattern Object | Required. Pattern describing which CloudWatch events trigger the function. Only matching events trigger the function. |
| Input | string |
JSON-formatted string to pass to the function as the event body. This value overrides the matched event. |
| InputPath | string |
JSONPath describing the part of the event to pass to the function. |
Pattern:
detail:
state:
- terminatedThe object describing an event source with type IoTRule.
| Property Name | Type | Description |
|---|---|---|
| Sql | string |
Required. The SQL statement that queries the topic. For more information, see Rules for AWS IoT in the AWS IoT Developer Guide. |
| AwsIotSqlVersion | string |
The version of the SQL rules engine to use when evaluating the rule. |
Sql: "SELECT * FROM 'iot/test'"The object describing an event source with type AlexaSkill.
Specifying AlexaSkill event creates a resource policy that allows the Amazon Alexa service to call your Lambda function. To configure the Alexa service to work with your Lambda function, go to the Alexa Developer portal.
The object describing the environment properties of a function.
| Property Name | Type | Description |
|---|---|---|
| Variables | Map of string to string |
A map (string to string map) that defines the environment variables, where the variable name is the key and the variable value is the value. Variable names are limited to alphanumeric characters and the first character must be a letter. Values are limited to alphanumeric characters and the following special characters _(){}[]$*+-\/"#',;.@!?. |
Variables:
TABLE_NAME: my-table
STAGE: prodThe object describing the source of events which trigger the function.
| Property Name | Type | Description |
|---|---|---|
| Type | string |
Required. Event type. Event source types include 'S3, 'Api', 'SNS', 'Kinesis', 'DynamoDB'. For more information about all types, see Event source types. |
| Properties | * | Required. Object describing properties of this event mapping. Must conform to the defined Type. For more information about all types, see Event source types. |
Type: S3
Properties:
Bucket: my-photo-bucketType: AlexaSkillThe object describing the properties of a primary key.
| Property Name | Type | Description |
|---|---|---|
| Name | string |
Attribute name of the primary key. Defaults to id. |
| Type | string |
Attribute type of the primary key. MUST be one of String, Number, or Binary. |
Name: id
Type: String