You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature request] Support for YAML anchors in SAM/CF templates #228
Any chance we can get YAML anchor support for SAM?
As it is, YAML anchors are supported in Amazon Swagger S3 files referenced in SAM files via, e.g., DefinitionUri. This is awesome as it lets us very succinctly define new API Gateway endpoints. For example:
I figured out a way to get YAML anchors working in CloudFormation templates. Sweet! Here's how:
You cannot include YAML anchors directly in your primary CF template (the one you are directly deploying). Doing so will yield: "An error occurred (ValidationError) when calling the CreateChangeSet operation: Template error: YAML aliases are not allowed in CloudFormation templates"
However, you can use YAML anchors in imported template snippets. Then, in the deployable primary CF file, you bring in the snippet (with YAML anchors) as follows:
TL;DR - Do aws cloudformation package followed by aws cloudformation deploy to support Anchors :)
So here is the deal. YAML Anchors are not supported in AWS CloudFormation. They are explicitly blacklisted by CloudFormation, which we can't change.
But if you use the aws cloudformation package command before calling deploy, it will read the YAML file using regular YAML parser which will support anchors. The output of package command will be a flat file without anchors, but at least the code you write will support anchors. package is useful to bundle Lambda function code artifacts to S3. Even if you don't use any of the package functionality (ie. set CodeUri to S3 URI yourself), you can run the package command to produce a no-op output but flatten anchors.
Reacted by Handy, Jeshan Giovanni BABOOA, Ilya Sotkov, Braxton Beyer, Peter Springsteen, Khalid Saifullah, João Ricardo, Yves M. and Pat Myron
The suggested workaround by @sanathkr seems not to work for me. The aws cloudformation package command does create a separate and flat yaml but it still contains my anchors.
I use the anchors inside my swagger api gateway doc. Might it be impossbile in that scenario?
I am using YAML anchors without issue in the solution I specified above. I have not tried the solution proposed by @sanathkr as mine is working well as is.
Here's more detail on what I am doing. The core CloudFormation template has next to nothing in it. Besides a few other scaffolding items, all it does is AWS::Include another like so in file BigOnion.yaml (or whatever else you want to call it):
Then, in resources.yaml (or whatever else you call it), I can use YAML to radically reduce CloudFormation templates. Here's an example of a Lambda function definition:
In the referenced bin/update-resources.sh I have the following:
#!/usr/bin/env bash
# Creates the S3 deployment helper bucket if it doesn't already exist.
# Uploads resources.yaml to the bucket.
PROFILE=${1:-BigOnion}
echo "Using AWS profile ${PROFILE}"
MY_REGION=`aws --profile ${PROFILE} configure get region`
echo "AWS region is ${MY_REGION}"
ACCOUNT_ID=`aws --profile ${PROFILE} sts get-caller-identity --output text --query "Account"`
echo "AWS account id is ${ACCOUNT_ID}"
S3="my-s3-location-${ACCOUNT_ID}-${MY_REGION}"
echo "AWS S3 is ${S3}"
aws --profile ${PROFILE} s3 mb s3://$S3
aws --profile ${PROFILE} s3 cp CloudFormation/resources.yaml s3://$S3/resources.yaml
if [ ! $? -eq 0 ]; then
echo "FAILED"
exit
fi
You can probably figure out what is going on here. This script basically creates the S3 bucket if it doesn't already exist, and uploads the new/updates resources.yaml there.
As a result, I can edit resources.yaml which contains all my YAML anchor'd stuff, and when I am ready to deploy, I just type: yarn run cf-deploy. This will create the S3 bucket (in my case I include region and account ID as I am using this across multiple regions and accounts), upload the new resources.yaml file which is referenced in BigOnion.yaml, and then deploy/update a new CF stack using the aforementioned in BigOnion.yaml
YMMV.
Reacted by Manuel Silva, João Ricardo and Gunther Struyf
You can now use this with aws cloudformation deploy or aws cloudformation create-stack. It'll save you having to use Fn::Transform
Reacted by Peter Bray, João Ricardo, Yves M., json singh, Diego Guimarães, tyler-matheson, strannik19, Jonathan del Strother, denzfarid and Andre Nascimento
Reacted by Diego Guimarães, Alexander Smirnoff and Andre Nascimento
@vjeffz thanks for the update... is this officially documented somewhere, or just another "side-effect" (like my original "discovery" of Fn::Transform?
Activity
bluepeter commentedon Dec 9, 2017
I figured out a way to get YAML anchors working in CloudFormation templates. Sweet! Here's how:
You cannot include YAML anchors directly in your primary CF template (the one you are directly deploying). Doing so will yield: "An error occurred (ValidationError) when calling the CreateChangeSet operation: Template error: YAML aliases are not allowed in CloudFormation templates"
However, you can use YAML anchors in imported template snippets. Then, in the deployable primary CF file, you bring in the snippet (with YAML anchors) as follows:
Anchors allow CF templates to become radically smaller.
bluepeter commentedon Dec 9, 2017
The benefit? Radically shrink CF definitions:
Before:
After...
sanathkr commentedon Jan 8, 2018
TL;DR - Do
aws cloudformation packagefollowed byaws cloudformation deployto support Anchors :)So here is the deal. YAML Anchors are not supported in AWS CloudFormation. They are explicitly blacklisted by CloudFormation, which we can't change.
But if you use the
aws cloudformation packagecommand before callingdeploy, it will read the YAML file using regular YAML parser which will support anchors. The output of package command will be a flat file without anchors, but at least the code you write will support anchors.packageis useful to bundle Lambda function code artifacts to S3. Even if you don't use any of the package functionality (ie. set CodeUri to S3 URI yourself), you can run thepackagecommand to produce a no-op output but flatten anchors.sanathkr commentedon Jan 8, 2018
Closing this issue as it is technically not possible to support this in SAM. Feel free to open if you have questions.
mruckli commentedon Feb 20, 2018
The suggested workaround by @sanathkr seems not to work for me. The
aws cloudformation packagecommand does create a separate and flat yaml but it still contains my anchors.I use the anchors inside my swagger api gateway doc. Might it be impossbile in that scenario?
jjmschofield commentedon Feb 24, 2018
@mruckli is correct @sanathkr - package simply replaces &anchorName with something like &id001
jjmschofield commentedon Feb 24, 2018
@bluepeter - did you get any joy combining your technique with
aws cloudformation package?bluepeter commentedon Feb 24, 2018
I am using YAML anchors without issue in the solution I specified above. I have not tried the solution proposed by @sanathkr as mine is working well as is.
Here's more detail on what I am doing. The core CloudFormation template has next to nothing in it. Besides a few other scaffolding items, all it does is
AWS::Includeanother like so in fileBigOnion.yaml(or whatever else you want to call it):Then, in
resources.yaml(or whatever else you call it), I can use YAML to radically reduce CloudFormation templates. Here's an example of a Lambda function definition:Sweet!
And just wait to you can boil down API Gateway endpoints to 3 lines! (Which you can w/ prudent use of YAML anchors.)
Then, (and assuming you're using Node.js) to deploy, I add the following in
package.jsonto simplify the command line CloudFormation deploy step:In the referenced
bin/update-resources.shI have the following:You can probably figure out what is going on here. This script basically creates the S3 bucket if it doesn't already exist, and uploads the new/updates
resources.yamlthere.As a result, I can edit
resources.yamlwhich contains all my YAML anchor'd stuff, and when I am ready to deploy, I just type:yarn run cf-deploy. This will create the S3 bucket (in my case I include region and account ID as I am using this across multiple regions and accounts), upload the new resources.yaml file which is referenced in BigOnion.yaml, and then deploy/update a new CF stack using the aforementioned in BigOnion.yamlYMMV.
vjeffz commentedon Dec 11, 2019
If you call the YAML anchor
Metadatathen it will generate a valid CFN key in the output YAML that can be ignored.For e.g the following cloudformation with anchors:
When run with
aws cloudformation packagethis will generate:You can now use this with
aws cloudformation deployoraws cloudformation create-stack. It'll save you having to useFn::Transformbluepeter commentedon Dec 11, 2019
@vjeffz thanks for the update... is this officially documented somewhere, or just another "side-effect" (like my original "discovery" of
Fn::Transform?vjeffz commentedon Dec 11, 2019
@bluepeter not officially documented anywhere, just a simple workaround I discovered :)