Math and Collection and String Manipulation in YAML
A Pulumi YAML program that demonstrates how to do math and manipulate objects in YAML.
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set yaml-math-and-objectscd pulumi-examples/yaml-math-and-objectsIn addition to programming languages such as Python, Go, Typescript, C#, Pulumi also supports YAML.
However, YAML is limited when it comes to constructs such as loops and conditionals and it is not always obvious how one can do math in a Pulumi YAML program.
Similarly, using the pulumi-std library can be a little tricky.
This project is meant to provide some YAML program examples for these use-cases.
References#
- Pulumi YAML Docs Page
- Pulumi YAML Language Reference
- pulum-std Function List
- These are functions that are available to YAML programs.
Tips and Tricks for Using the pulumi-std Functions#
Start with the functions list page in the pulumi-std repo, pulum-std Function List
Here you will find the list of functions that are available.
To understand how to use a given function, click on the link from the function list page. This will take you to the go code for the function. From here you can learn two important things:
- What the function does.
- What the function’s input parameters are.
To understand what the function does, look for the Annotate function code block. This will provide an explanation of what the function does.
To understand the function’s inputs, look for the ...Args structure code block. This will list the inputs’ names and types (e.g. string, array, etc).
For example, look at std-merge.
You’ll see it expects a single parameter named input that is an array of map of strings.
So something like: [goo:"foo", moo:"boo"] and if you look at the Pulumi YAML program in this folder you’ll see how that is represented in YAML.
Now, look at std-split and you’ll see it wants two parameters: separator and text.
Using the Pulumi Program#
Read the Pulumi.yaml file to see what the program does and how it does it.
Then, from the folder containing the program:
pulumi stack init devpulumi upThis creates a random string and outputs some values used in the code.
Now, tinker with the stack config values in Pulumi.yaml or via pulumi config set commands to see what happens when you:
- Change the length of the string.
- Change
lengthto, say,32 - Run
pulumi upand you should see thatminLoweris now set to one-fourth(ish) of whatever value you set for the length and there are at least that many lowercase letters in the string.
- Change
- Disable using lowercase characters.
- Change
useLowerCasetofalse - Run
pulumi upand you should see that there are no lowercase characters and the stack output showminLowerset to 0.
- Change
- Change one of the key-value pairs in
configKeepersArray.- Run
pulumi upand see that the array map is updated and that a new string is generated since that’s what thekeepersproperty is for - to trigger a regeneration of the key even though no property directly related to the resource like length, etc is changed..
- Run