[AWS SAM] Read a value from Secrets Manager in a template

Created on November 29, 2026

A Secrets Manager dynamic reference lets CloudFormation read a secret value when it creates or updates a resource. In this template.yaml example, CloudFormation reads the JSON key API_TOKEN from the secret named example-app-secret and passes its value to the Lambda function as the API_TOKEN environment variable.

Transform: AWS::Serverless-2016-10-31
Resources:
  ExampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: example-function
      Handler: app.lambda_handler
      Runtime: python3.12
      CodeUri: src/
      Environment:
        Variables:
          API_TOKEN: '{{resolve:secretsmanager:example-app-secret:SecretString:API_TOKEN}}'

The deployment needs permission to read the secret. Changing the secret by itself leaves the deployed environment variable at its previous value until a stack update changes the function resource.

AWS SAM