4 Variables
Adam Pippin edited this page 3 years ago

Variables

cfnpp supports definining and referencing variables and parameters within stacks.

Defining

Variables and parameters can be declared in the cfnpp map under variables and parameters.

cfnpp:
    parameters:
        UseMultiAz: True
    variables:
        A: 1
        B: 2
        Foo: "Bar"
        Azs:
            - us-east-1a
            - us-east-1b
            - us-east-1c

When multiple stacks are loaded, the variable and parameter values are resolved before the first stack is processed. That is, a base stack can define a variable or parameter and a dependent stack can redefine the values and the base stack will see the new value. Values are merged and overwritten the same way the rest of the document is (scalars overwrite, arrays append, etc) and normal merge functions can be used to control that process.

Definitions support the full range of functions normally available. They may also reference other variables. (Though creating a circular reference will result in an error.) cfnpp will build a dependency graph of all your variables and parameters then determine the correct order to resolve them in.

Referencing Variables

You can reference variables with the !var function.

For example, given:

cfnpp:
    variables:
        A: 1
        B: 2
        Foo: "Bar"
        Azs:
            - us-east-1a
            - us-east-1b
            - us-east-1c

Resources:
    MyResource:
        DesiredCapacity: !var A
        MaximumCapacity: !var B
        Name: !var Foo
        AvailabilityZones: !var Azs

will result in:

Resources:
    MyResource:
        DesiredCapacity: 1
        MaximumCapacity: 2
        Name: Bar
        AvailabilityZones:
            - us-east-1a
            - us-east-1b
            - us-east-1c

Referencing Parameters

You can reference parameters with the !param function.

This function will use the CloudFormation instrinsic Ref to compute the value during provisioning.

For example, given:


Resources:
    MyResource:
        Name: My Resource
        Tags:
            -
                Key: Environment
                Value: !param Environment

will result in:

Resources:
    MyResource:
        Name: My Resource
        Tags:
            -
                Key: Environment
                Value: { Ref: Environment }