Variable
The Variable
declaration specifies a literal value or an expression. Variables are inlined during compilation. They can be used by other variables, resources, and functions. Variables, like resources, can be scoped to functions, the stack, or to be public.
Syntax
Variable: String
Description: String
Type: String
Scope: ScopeDefinition
Value: Expression
EncryptionContext:
Key-Value Mapping
Properties
Description
-
The
Description
attribute specifies the variable description.Required: No
Type: String
EncryptionContext
-
The
EncryptionContext
section is an optional mapping of key-value pairs used for decrypting a variable of typeSecret
. For all other types, specifyingEncryptionContext
will produce a compilation error.Required: No
Type: Key-Value Pair Mapping
Scope
-
The
Scope
attribute specifies which functions need to have access to this item. TheScope
attribute can be a comma-separated list or a YAML list of function names. If all functions need the item, thenall
can be used as a wildcard. In addition,public
can be used to export the item from the module. Alternatively,stack
can be used to make the item available only in a nested stack.Required: No
Type: Comma-delimited String or List of String
Type
-
The
Type
attribute specifies the variable type. When omitted, the type isString
. Encrypted values must have typeSecret
and can optionally specify anEncryptionContext
section. These values can be shared as is, or decrypted, when using the::Plaintext
suffix on the full name.For example, the decrypted value of a variable called
Password
with typeSecret
can be accessed by using!Ref Password::Plaintext
. Value
-
The
Value
attribute specifies the value for the variable. TheValue
can be a literal value, an expression, or a list of expressions.Required: Yes
Type: Expression
Variable
-
The
Variable
attribute specifies the item name. The name must start with a letter and followed only by letters or digits. Punctuation marks are not allowed. All names are case-sensitive.Required: Yes
Type: String
Return Values
Ref
When the logical ID of this item is provided to the Ref
intrinsic function, Ref
returns the value of the variable. If the value of the variable is a list, the expressions in the list are concatenated into a comma-separated string.
Examples
Literal value
- Variable: MyValue
Value: Hello World!
List of literal values
- Variable: MyList
Value:
- First
- Second
Expression value
- Variable: MyExpression
Value: !Sub "${AWS::StackName}-${AWS::Region}"
List of expressions
- Variable: MyExpressionList
Value:
- !Sub "${AWS::StackName}-${Module::FullName}"
- !Sub "${AWS::StackName}-${AWS::Region}"