Parameter Ovverrides in SAM template
Every time we need to create something on AWS, it is so important to have as much automation as possible: this means that we want to have templates everywhere and just change only few things to have as much standard as we can.
When we build a Stack with SAM, we use a template.yaml which purpose is to have an easy way to create resources without "fighting" with CloudFormation. As you know CF is a complex language and has lot of features that give us the real power of Infrastructure as Code.
The template written for SAM is then converted into CloudFormation and we can destroy and recreate resources with few commands, or we can "maintain" a stack by just changes for example an Api endpoint (thus is a Stack update).
To have the maximum of flexibility, SAM supports the concept of "parameters" (inherited of course from CF), where there is the possibility to declare some variables we can pass during the deploy phase and let update the stack.
AN EXAMPLE
We can start with the easiest example we can: a lambda function which is exposed through an API. In this case we are supposing that the API is always the same and we wont (no matter whay) just to change the stage for the API
As you know, the stage is just one of the elements that composed the Path of the API.
DECLARATION
First of all we need to declare the parameter, because the framework have a specific section for that.
Parameters are declared just after "Global" section. It is very easy to understand how to insert a new Parameter: first declare his name, then the type (if you want to have safety), then a Default value and if you want also the AllowedValues (which is of course a list of element)
In this case the Parameter name is StageNameParameter, the type is String, the default is dev and the allowed values are dev, stag and prod. The allowed values is a nice to have because during deploy phase, SAM will correct you if you insert a wrong value.
REFERENCE
To reference the value in the resource, we can use two ways:
There is no difference. I prefer the second because I like indentation, it gives me a feel of order.
To reference in the output, you can use the Sub function, both in inline or Map substitution. The variable is used with ${xxx} in the inline, and with Ref in the map.
For example
RUNTIME
After executing SAM build, you can now execute SAM deploy passing the value you want to use with the parameter-overrides option
sam deploy simple-lambda-deploy --parameter-overrides StageNameParameter=stag
simple-lambda-deploy is the name of the stack you have created and which is saved in your samconfig.toml (because we are supposing it is not the first time you are working with this stack..which means you already did the "deploy guided")
The parameter-overrides accepts different key/value elements, separated by comma.
In this case we are passing StageNameParameter with value as "stag"
In the output we have
Which is exactly what we expect
COMPLICATED FUNCTION
Let's suppose to have complicated function in which we have a function that perform a join and then we want to have a reference to our value, we can use the !Ref way.
In our case, suppose we want to build with function the endpoint of our function, where an element of the path is the parameter we are passing at runtime. The function can concatenate different values, using "." for site name and "/" for resources path.
We can have an expression like this:
As you see we are creating the complete path by referencing a parameter and also "HelloWorldApi" which is - in our case - one of the resources we have created
CONCLUSION
Template and CloudFormation are very extensible and offers a lot of way to create easy way to build your infrastructure.
Probably you will not work with them because usually IT companies have DevOps which are very skilled on that and they are the owner of creating resources.
But knowing how it works behind the scene is always a nice to have
Commenti
Posta un commento