• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense KeysVerifying Downloads
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
Layers
OverviewTroubleshooting
Managed InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. Usage
  2. Variables
  3. External YAML JSON Files

File Variables

A ${file(...)} variable inserts the content of another file, or one property from it, into serverless.yml. Use it to share settings between services, to keep per-stage settings in separate files, or to move long blocks such as CloudFormation resources out of serverless.yml.

The following config.yml holds a schedule and a list of events:

# config.yml
globalSchedule: rate(10 minutes)
myevents:
  - schedule:
      rate: rate(1 minute)

The following serverless.yml, in the same directory, reads one property, a list and the whole file from config.yml:

# serverless.yml
service: my-service

provider:
  name: aws
  runtime: nodejs24.x

custom: ${file(./config.yml)}

functions:
  hello:
    handler: handler.hello
    events:
      - schedule: ${file(./config.yml):globalSchedule}
  world:
    handler: handler.world
    events: ${file(./config.yml):myevents}

custom receives the whole file, so ${self:custom.globalSchedule} returns the same value as ${file(./config.yml):globalSchedule}.

Syntax of the file variable

${file(FILE_PATH)}
${file(FILE_PATH):PROPERTY_PATH}
${file(FILE_PATH):PROPERTY_PATH, DEFAULT_VALUE}
  • FILE_PATH (required): the path of the file, relative to the directory that contains serverless.yml, or an absolute path. The path can contain other variables, for example ${file(./config.${sls:stage}.json)} reads config.prod.json when the stage is prod. A symbolic link is followed, and the target file is read.
  • PROPERTY_PATH (optional): the property to read, with keys separated by dots, to any depth (database.connection.host). An array item is a numeric segment: myevents.0 is the first item of myevents. Without a property path, the variable returns the whole file.
  • DEFAULT_VALUE (optional): the value to use when the file or the property doesn't exist; see Default value for a missing file or property.

Supported file types

The file's extension decides how the Framework reads it:

ExtensionRead asValue returned
.yml, .yamlYAMLThe parsed file, or the property at PROPERTY_PATH
.json, .tfstateJSONThe parsed file, or the property at PROPERTY_PATH
.js, .cjs, .mjsJavaScript moduleThe module's export, or the result of the exported function; see JavaScript and TypeScript variables
.ts, .mts, .ctsTypeScript moduleSame as JavaScript, compiled on the fly; see JavaScript and TypeScript variables
Any other extensionPlain textThe whole file content as a string (UTF-8)

Give each file the extension of its format: a JSON document saved as config.txt is returned as a string, not parsed.

YAML files can use the CloudFormation short-form tags, such as !Ref, !GetAtt and !Sub, so a file of CloudFormation resources reads the same way as the resources section of serverless.yml.

To read Terraform outputs from remote state rather than a local .tfstate file, use Terraform variables.

Read a whole file or one property

The following config.json holds settings for two stages:

{
  "dev": { "memorySize": 512, "logLevel": "debug" },
  "prod": { "memorySize": 1024, "logLevel": "info" }
}

The following serverless.yml reads the memorySize of the current stage and the whole settings object of the current stage:

# serverless.yml
service: my-service

provider:
  name: aws
  runtime: nodejs24.x
  memorySize: ${file(./config.json):${sls:stage}.memorySize}

custom:
  settings: ${file(./config.json):${sls:stage}}

functions:
  hello:
    handler: handler.hello

When the variable is the whole value of a property, the file's value keeps its type: a number stays a number, and an object or a list is inserted as an object or a list. When the variable is part of a longer string (name: api-${file(./config.json):dev.memorySize}), the value is converted to a string, and an object can't be embedded.

A YAML or JSON file whose whole content is 0, false or an empty string resolves to no value when it's read without a property path: the variable takes its default value, or stops the command when it has none. A property with one of these values, such as { "count": 0 } read with :count, keeps its value.

Variables inside a referenced file

A referenced YAML or JSON file can contain variables. The Framework inserts the file's content into serverless.yml and then resolves the variables in it, so inside the file:

  • ${self:...} refers to properties of serverless.yml, not of the referenced file.
  • A nested ${file(...)} path is relative to the directory that contains serverless.yml, not to the referenced file.

The following functions/common.yml reads the VPC settings from serverless.yml:

# functions/common.yml
api:
  handler: api.handler
  vpc:
    securityGroupIds: ${self:custom.vpc.securityGroupIds}
    subnetIds: ${self:custom.vpc.subnetIds}

Split CloudFormation resources into files

To keep serverless.yml short, reference files of CloudFormation resources from the resources section. Each file holds a Resources (or Outputs) block, and resources takes a list of them:

# serverless.yml
resources:
  - ${file(./resources/dynamodb.yml)}
  - ${file(./resources/s3.yml)}
# resources/s3.yml
Resources:
  UploadsBucket:
    Type: AWS::S3::Bucket

For more on this pattern, see Multiple configuration files.

Default value for a missing file or property

For a YAML, JSON or plain-text file, when the file doesn't exist, or the property at PROPERTY_PATH doesn't exist or is null, the variable uses the default value after the comma. A missing JavaScript or TypeScript file stops the command even with a default; see Missing values and errors.

For example:

custom:
  settings: ${file(./config.${sls:stage}.yml), file(./config.default.yml)}
  logLevel: ${file(./config.yml):logLevel, 'info'}

The default can be a quoted string, a number, true, false, or another variable written without the ${}. The Framework tries each source from left to right and uses the first one that has a value.

Without a default, a missing file or property stops the command with an error that names the variable and where it's used:

Cannot resolve '${file(./config.yml):logLevel}' variable at 'custom.logLevel'. No value is available for this variable, and no default value was provided. Please check your variable definitions or provide a default value.

A file that exists but can't be parsed stops the command even when the variable has a default, with an error such as Cannot parse YAML from "/path/to/config.yml": ... or Cannot parse JSON from "/path/to/config.json": ....

Related variable sources

  • Variables: how variables work, and the list of all variable sources.
  • JavaScript and TypeScript variables: compute values with code in a .mjs or .ts file.
  • Self-reference variables: reuse values from the same serverless.yml.
  • Parameters: stage-specific values set with params.
Edit this page
Prev CLI OptionsNextJavascript properties

Contents

  • File Variables
  • Syntax of the file variable
  • Supported file types
  • Read a whole file or one property
  • Variables inside a referenced file
  • Split CloudFormation resources into files
  • Default value for a missing file or property
  • Related variable sources

Related

GuidesPluginsExamplesSlack CommunitySupport