A Guide to Using AWS Intrinsic Functions in Your SAM Templates

Rahul LokurteRahul Lokurte
6 min read

AWS Serverless Application Model (SAM) is a framework for building serverless applications on AWS. It provides a simplified way to define and deploy AWS Lambda functions, Amazon API Gateway APIs, and other serverless resources, using AWS CloudFormation. One of the critical components of AWS SAM is the AWS SAM template, which describes your serverless application and its resources.

AWS intrinsic functions in AWS SAM templates are a powerful feature that allows you to dynamically generate values at runtime based on the context of your application. These functions are provided by AWS CloudFormation and can be used in your AWS SAM templates as well.

In this blog post, we will explore the basics of AWS intrinsic functions in AWS SAM templates and how you can use them to build dynamic and flexible serverless applications on AWS.

What are AWS Intrinsic Functions in AWS SAM templates?

AWS Intrinsic Functions in AWS SAM templates are a set of special functions that allow you to generate values dynamically at runtime based on the context of your serverless application. These functions are provided by AWS CloudFormation, which is the underlying service on which AWS SAM templates are built. It can be used to simplify the creation and management of serverless resources in your AWS SAM templates.

Why use AWS Intrinsic Functions in AWS SAM templates?

AWS Intrinsic Functions in AWS SAM templates allow you to create dynamic and flexible serverless applications on AWS. They allow you to reference other resources, perform string manipulation, and even generate random numbers, among other things. By leveraging these functions, you can reduce duplication, increase reusability, and simplify the management of your serverless resources.

Let's discuss some of the commonly used AWS intrinsic functions in AWS SAM templates.

Ref

Ref is an intrinsic function in AWS SAM templates that are used to reference other resources in your template. It returns the value of the named resource or parameter.

When you use Ref in your AWS SAM template, AWS CloudFormation automatically resolves the reference to the value of the named resource or parameter at deployment time. This means that you can use Ref to get the ARN, ID, or other attributes of a resource in your template.

Here's an example of how you can use Ref in your AWS SAM template:

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./my-lambda-function
      Handler: index.handler
      Runtime: nodejs14.x
      Environment:
        Variables:
          BucketName: !Ref MyBucket

In this example, we have defined an S3 bucket named "MyBucket" and a Lambda function named "MyLambdaFunction". The Lambda function has an environment variable named "BucketName" that references the S3 bucket using Ref. This allows us to dynamically set the bucket name in our Lambda function based on the name of the S3 bucket resource defined in our AWS SAM template.

Fn::Sub

Fn::Sub is an intrinsic function in AWS SAM templates that are used to substitute variables in a string with their corresponding values at deployment time. It allows you to create more flexible and dynamic templates that can adapt to different environments and configurations. The syntax for Fn::Sub is as follows:

!Sub string [ variablesMap ]

The string parameter is the string that you want to substitute variables in. The variablesMap parameter is an optional map that defines the variables and their values that you want to substitute. Here's an example of how you can use Fn::Sub in your AWS SAM template:

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub ${EnvironmentName}-my-bucket-${AWS::Region}

In this example, we have defined an S3 bucket named "MyBucket" with a dynamic bucket name that includes the environment name and AWS region. The environment name is a variable defined in the variables map using the !Sub intrinsic function. The AWS::Region variable is automatically provided by AWS CloudFormation.

Condition functions

In some cases, you may want to conditionally create resources based on certain criteria. AWS Intrinsic Functions such as Fn::If and Fn::Equals allow you to define conditions and control the creation of resources accordingly. For example, you can conditionally create an S3 bucket only if a specific parameter is set to true, or you can enable different features based on the environment or deployment stage.

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Condition: CreateBucketCondition
    Properties:
      ...
Conditions:
  CreateBucketCondition: !Equals [!Ref CreateBucket, "true"]

In this example, the Fn::Equals function is used to conditionally create an S3 bucket based on the value of the CreateBucket parameter.

Input and output data manipulation:

SAM templates often require exchanging data between resources or accessing outputs from other AWS CloudFormation stacks. AWS Intrinsic Functions provide powerful capabilities for manipulating input and output data within your templates.

Cross-stack references:

When you have multiple AWS CloudFormation stacks that need to communicate with each other, you can use the Fn::ImportValue function to reference outputs from other stacks. This function allows you to retrieve values exported from another stack and use them as input for your resources. For example:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          API_URL: !ImportValue MyApiStack-ApiUrl-${Stage}
          ...

In this example, the Fn::ImportValue function is used to retrieve the API URL from the MyApiStack stack's output and assign it to the API_URL environment variable. This enables your serverless function to interact with the API exposed by the MyApiStack.

Selecting specific elements from arrays:

In some cases, you may have an array of values and only need to extract specific elements. The Fn::Select function allows you to retrieve a single element from a list based on its index. For example:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          Region: !Select [0, !Split ["-", !Ref AWS::Region]]
          ...

In this example, the Fn::Select function is used to split the AWS::Region value using the dash ("-") as the separator and then select the first element from the resulting array. This can be useful when you need to extract a specific portion of a value for configuration purposes.

Getting attribute values from resources:

SAM templates often require accessing specific attribute values from resources. The Fn::GetAtt function allows you to retrieve specific attributes of a resource. For example:

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      ...
  MyBucketName:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: !GetAtt MyBucket.Name
      ...

n this example, the Fn::GetAtt function is used to retrieve the Name attribute of the MyBucket resource and assign it as the value for an AWS Systems Manager Parameter (AWS::SSM::Parameter). This enables you to store and reference the bucket name in a parameter that can be used by other resources.

By leveraging these AWS Intrinsic Functions for input and output data manipulation, you can create more dynamic and interconnected serverless applications within your SAM templates. These functions enable you to exchange data between resources, retrieve specific attribute values, and perform transformations to adapt the data for your application's needs.

Conclusion

AWS Intrinsic Functions provide powerful capabilities for manipulating input and output data within SAM templates. Whether you need to reference outputs from other stacks, select specific elements from arrays, or retrieve attribute values from resources, these functions allow you to effectively exchange and transform data within your serverless applications. By incorporating input and output data manipulation using AWS Intrinsic Functions, you can enhance the flexibility and interoperability of your SAM templates, leading to more robust and scalable serverless applications on AWS.

11
Subscribe to my newsletter

Read articles from Rahul Lokurte directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Lokurte
Rahul Lokurte

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.