Resources and Properties for AWS SAM
Tags: aws, serverless, sam, iac
Published: 2024-04-17 12:55:54

## Introduction
> The `AWS Serverless Application Model` (AWS SAM) is a comprehensive toolkit crafted to streamline developers' efforts in building and overseeing serverless applications, encompassing components like `Lambda functions`, `API Gateway`, `DynamoDB`, and more, on AWS. 

Within this toolkit, a wide array of resources and properties are available for integration within templates, simplifying the process of creating and managing serverless resources effectively.


`AWS Serverless Application Model` (AWS SAM) offers a comprehensive suite of features to streamline serverless application development on AWS:

**Efficient Infrastructure Definition:** Quickly write less code to define your application's infrastructure using AWS SAM templates. These templates can be directly deployed to AWS CloudFormation to provision resources.

**Lifecycle Management:** From authoring to monitoring, manage your serverless applications throughout their development lifecycle with the AWS SAM CLI. For details on usage, refer to the guide on using the AWS SAM CLI.

**Seamless Permission Provisioning:** Incorporate AWS SAM connectors in your templates to establish permissions between AWS resources automatically, translating your configurations into necessary IAM permissions. For further information, check out the section on managing resource permissions with AWS SAM connectors.

**Terraform Integration:** Manage and debug your Terraform-based serverless applications locally with support from the AWS SAM CLI, which now includes features for testing Lambda functions and layers. Additional details are available in the AWS SAM CLI Terraform support documentation.

Here are the types of resources you can specify:
## **AWS::Serverless::Api**
This resource type is used to define an API Gateway in a serverless application. API Gateway acts as a front door to manage all the API calls, ensuring smooth data transmission between AWS services and client applications through HTTPS requests.
```yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionUri: swagger.yaml
...
```

## **AWS::Serverless::Application**
This resource allows you to manage and deploy applications composed of multiple serverless resources or other nested applications.
- MyApplication utilizes a LocationObject accessed via ARN.
- MyApplication2 utilizes an S3 URL.
```yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyApplication:
    Type: AWS::Serverless::Application
    Properties:
      Location: 
        ApplicationId: 'arn:aws:serverlessrepo:eu-west-3:123456789012:applications/MyApplication'
        SemanticVersion: 1.0.0
      Parameters:
        StringParameter: param-value
        IntegerParameter: 2
  MyApplication2:
    Type: AWS::Serverless::Application
    Properties:
      Location: https://s3.amazonaws.com/bucketName/template.yaml
...  
```
## **AWS::Serverless::Connector**
It provides simple and well-scoped permissions between your serverless application resources.
```yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyTable:
    Type: AWS::Serverless::SimpleTable
  MyFunction:
    Type: AWS::Serverless::Function
    Connectors:
      MyConn:
        Properties:
          Destination:
            Id: MyTable
          Permissions:
            - Write
...
```
## **AWS::Serverless::Function**
This resource creates an AWS Lambda function along with an AWS Identity and Access Management (IAM) execution role. Additionally, it establishes event source mappings that trigger the function.
```yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyS3Function:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ProductFunction/target/ProductFunction-1.0.jar
      Handler: ProductFunction.App::handleRequest
      Runtime: java21
      Description: Create ProductFunction
      MemorySize: 1024
      Timeout: 15
      Policies: 
        - S3CrudPolicy:
            BucketName: !Ref S3Bucket
      Events:
        MyS3Event:
          Type: S3
          Properties: 
            Bucket: !Ref S3Bucket
            Events:
              - s3:ObjectCreated:*
...
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    DeletionPolicy: Retain
    Properties:
      BucketName: DOC-EXAMPLE-BUCKET
...
```
## **AWS::Serverless::GraphQLApi**
This resource used to effortlessly establish and customize an AWS AppSync GraphQL API within your serverless application.
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyLambdaFunction:
    Type: AWS::Serverless::Function
  Properties:
    CodeUri: HelloWorldFunction/target/HelloWorld-1.0.jar
    Handler: helloworld.App::handleRequest    
    Runtime: java21

  MyGraphQLAPI:
    Type: AWS::Serverless::GraphQLApi
    Properties:
      Name: MyApi
      SchemaUri: ./gql/schema.gql
      Auth:
        Type: API_KEY
      ApiKeys:
        MyApiKey:
          Description: my api key
      DataSources:
        Lambda:
          MyLambdaDataSource:
            FunctionArn: !GetAtt MyLambdaFunction.Arn
      Functions:
        lambdaInvoker:
          Runtime:
            Name: APPSYNC_JS
            Version: 1.0.0
          DataSource: MyLambdaDataSource
          CodeUri: ./gql/invoker.js
      Resolvers:
        Mutation:
          addPost:
            Runtime:
              Name: APPSYNC_JS
              Version: 1.0.0
            Pipeline:
            - lambdaInvoker
        Query:
          getPost:
            Runtime:
              Name: APPSYNC_JS
              Version: 1.0.0
            Pipeline:
            - lambdaInvoker
...
```
## **AWS::Serverless::HttpApi**
A resource type used to define HTTP APIs in a serverless application context. 
These APIs can serve as lightweight and cost-effective alternatives to traditional REST APIs managed by API Gateway.
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
  HttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      DefinitionBody:
        info:
          version: '1.0'
          title:
            Ref: AWS::StackName
        paths:
          "/":
            get:
              responses: {}
      openapi: 3.0.1
      CorsConfiguration:
        AllowMethods:
          - GET
        AllowOrigins:
          - '*'
        AllowCredentials: true
...
```
## **AWS::Serverless::LayerVersion**
A resource which allows you to define and manage Lambda layers within your serverless application. 
A Lambda layer is a .zip file archive that contains supplementary code or data
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: MyLayer
      Description: static data layer for app
      ContentUri: mylayer.zip
      CompatibleRuntimes:
        - java21
      LicenseInfo: Apache 2.0
      RetentionPolicy: Retain
...
```
## **AWS::Serverless::SimpleTable**
A resource type which simplifies the process of defining and provisioning an Amazon DynamoDB table within your serverless application.This resource is particularly handy when data retrieval is solely based on a primary key.
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
  ProductTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      TableName: myTable
      PrimaryKey:
        Name: id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
      Tags:
        Department: Engineering
        AppType: Serverless
...
```
## **AWS::Serverless::StateMachine**
It enables you to define and deploy AWS Step Functions state machines within your serverless applications.
StateMachine uses a definition written in Amazon States Language (ASL).

```yaml
{
  "Comment": "A simple state machine example",
  "StartAt": "HelloState",
  "States": {
    "HelloState": {
      "Type": "Pass",
      "Result": "Hello, World!",
      "End": true
    }
  }
}
```
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
  MyStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: sfn/MyStateMachine.asl.json
      Role: 'arn:aws:iam:::role/service/MyRole'
      DefinitionSubstitutions:
        MyFunctionArn: !GetAtt MyFunctionServerLess.Arn
        MyDDBTable: !Ref ProductTableFromTemplate
...
```

## Go further
For a deeper dive into maximizing the potential of all available resources, consider exploring the [Developer Guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) on AWS.

## Thank you for reading
If you have any questions, feedback, or suggestions, please feel free to leave them in the comments below. I'm eager to hear from you and respond to your thoughts!