Serverless Framework: Variables

Somebody who likes to code
Search for a command to run...

Somebody who likes to code
No comments yet. Be the first to comment.
Google processes tens of thousands of code changes every day across a codebase of hundreds of millions of lines. The secret isn't magic tooling — it's a documented, principled approach to code review

Amazon Simple Notification Service (SNS) looks straightforward on the surface — publish a message, deliver it to subscribers — but the gap between a topic that "works in dev" and one that is productio

Amazon Simple Queue Service (SQS) is deceptively simple to get started with, but surprisingly easy to misconfigure in ways that cause silent message loss, runaway costs, duplicate processing storms, o

HTTP API is the right default choice for the majority of .NET serverless workloads. At 70% less cost than REST API, lower latency, and native JWT authorization, it covers most production requirements

AWS Lambda has become a cornerstone of modern serverless architectures, but writing a function that "just works" is very different from writing one that is performant, cost-effective, secure, and read

In our last post, we introduced Serverless Framework as a great tool to deploy our AWS Lambda functions. Today we will review Serverless Variables as a way to dynamically replace values in the serverless.yml file.
Through the ${} syntax we can reference several values from different sources.
SomeProperty: ${someVariable}
We can define new properties under the custom section:
service: new-service
provider:
name: aws
custom:
properyA: 1
properyB: 'value'
functions:
hello:
handler: handler.hello
We can reference multiple variables as a fallback strategy in case one of the variables is missing:
propertyA: ${someVariable, otherVariable}
propertyB: ${someVariable, 'value'}
propertyC: ${someVariable, false}
propertyD: ${someVariable, 1024}
We can reference any property in the serverless.yml file, using the ${self:someProperty} syntax:
service: new-service
provider:
name: aws
custom:
prefix: 'abc'
functions:
hello:
name: ${self:custom.prefix}-hello
handler: handler.hello
To reference environment variables, use the ${env:someVariable} syntax:
service: new-service
provider:
name: aws
functions:
hello:
name: ${env:PREFIX}-hello
handler: handler.hello
To reference parameters, use the ${param:someParameter} syntax:
service: new-service
provider:
name: aws
functions:
hello:
name: ${param:prefix}-hello
handler: handler.hello
Parameters can be passed directly via CLI --param flag:
serverless deploy --param="prefix=abc" --param="otherParameter=otherValue"
To reference properties in other files, use the ${file(./someFile.xyz):someProperty} syntax.
# file.yml file
prefix: 'abc'
service: new-service
provider:
name: aws
functions:
hello:
name: ${file(./file.yml):prefix}-hello
handler: handler.hello
# file.json file
{
prefix: 'abc'
}
service: new-service
provider:
name: aws
functions:
hello:
name: ${file(./file.yml):prefix}-hello
handler: handler.hello
// file.js file
module.exports.prefix= 'abc';
service: new-service
provider:
name: aws
functions:
hello:
name: ${file(./file.js):prefix}-hello
handler: handler.hello
To reference CLI options, use the ${opt:someOption} syntax. Common options are:
${opt:stage}: The stage in your service you want to deploy.${opt:stage}: The region in your service you want to deploy.Options can be passed directly via CLI --stage and --region flags:
serverless deploy --stage production --region eu-central-1
Serverless variables are used internally by the Framework itself. The following variables are available:
${sls:instanceId}: A random id generated when the CLI runs.${sls:stage}: The stage used by the CLI.${cf:someStackName.someOutputKey} syntax.${s3:someBucketName/someKey} syntax.${ssm:/path/to/param} syntax.${ssm:/aws/reference/secretsmanager/secret_ID_in_Secrets_Manager} syntax.${aws:accountId} and ${aws:region}.We can add entire Resources sections from multiple files:
resources:
- ${file(resources/first-cf-resources.yml)}
- ${file(resources/second-cf-resources.yml)}
Each of your CloudFormation files has to start with a Resources entity:
Resources:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: some-bucket-name
We can nest variable references within each other. So you can reference certain variables based on another variable:
service: new-service
provider:
name: aws
functions:
hello:
name: ${param:${env:PARAM_NAME}}-hello
handler: handler.hello
Thanks, and happy coding.