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
Descriptionattribute specifies the variable description.Required: No
Type: String
EncryptionContext-
The
EncryptionContextsection is an optional mapping of key-value pairs used for decrypting a variable of typeSecret. For all other types, specifyingEncryptionContextwill produce a compilation error.Required: No
Type: Key-Value Pair Mapping
Scope-
The
Scopeattribute specifies which functions need to have access to this item. TheScopeattribute can be a comma-separated list or a YAML list of function names. If all functions need the item, thenallcan be used as a wildcard. In addition,publiccan be used to export the item from the module. Alternatively,stackcan be used to make the item available only in a nested stack.Required: No
Type: Comma-delimited String or List of String
Type-
The
Typeattribute specifies the variable type. When omitted, the type isString. Encrypted values must have typeSecretand can optionally specify anEncryptionContextsection. These values can be shared as is, or decrypted, when using the::Plaintextsuffix on the full name.For example, the decrypted value of a variable called
Passwordwith typeSecretcan be accessed by using!Ref Password::Plaintext. Value-
The
Valueattribute specifies the value for the variable. TheValuecan be a literal value, an expression, or a list of expressions.Required: Yes
Type: Expression
Variable-
The
Variableattribute 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}"