AWS CloudFormation is an amazing tool for infrastructure as code.
What used to take weeks to do on-premises, is now possible in a few minutes with some JSON, or better yet, YAML.
Remember that YAML includes a human readable structured format. Rules for Creating YAML file. When you are creating a file in YAML, you should remember the following basic rules −. YAML is case sensitive. The files should have.yaml as the extension. YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead. yaml: VSCode-YAML adds default configuration for all yaml files. More specifically it converts tabs to spaces to ensure valid yaml, sets the tab size, and allows live typing autocompletion and formatting, also allows code lens. YAML Viewer Online is easy to view and navigate to YAML. Copy, Paste and View. This is also called as YAML Viewer tool. Best and Secure Online YAML Viewer works well in Windows, Mac, Linux, Chrome, Firefox, Safari and Edge. This is a sample server Petstore server. You can find out more about Swagger at or on irc.freenode.net, #swagger.For this sample, you can use the.
This doesn’t mean we can’t do better and improve our efficiency even more.
You won’t notice any problems working with a few CloudFormation stacks. A few dozen stacks later, or complicated stacks with many resources though, and we need to start optimizing.
Let’s take a look at how we can up our CloudFormation game with Microsoft’s Visual Studio Code.
This article assumes you are familiar with Visual Studio Code and are using YAML based CloudFormation.
YAML indentation
One thing that always catches out people with editing YAML, is indentation. This is especially true for large CloudFormation files.
Take a look at this snippet. Can you see the error at a quick glance 👀 ?
What about now?
Thanks to the colorization and highlighting of the indent column, it is much easier to see that the Resource
property is incorrectly indented at a quick glance.
To enable this feature, install the following extensions:
Add the following user setting to enable the extra highlighting of the block located at the current cursor position.
Easy 👍.
Tabs, spaces and line endings
Tabs? Spaces? What style of line endings? Use EditorConfig and end all the discussions.
EditorConfig helps maintain consistency across your CloudFormation files by defining rules which the editor will apply on save.
Install the EditorConfig for VSCode extension. Don’t worry, there are plugins for many text editors if the people you work with don’t use VSCode.
Inside your Git repositories that contain your CloudFormation templates, create a .editorconfig
file at the root directory that looks like this:
Every time someone with EditorConfig saves a file, it will update the YAML file according to your rules 💪 .
Sorting things alphabetically
Do you like to keep things sorted alphabetically? It makes it easier when you are looking over massive files or IAM policy statements.
You shouldn’t have to say “A, B, C, D…” in your head every time we want to sort something, though.
Install Sort lines and enjoy the alphabetical awesomeness.
Highlight a block of the CloudFormation template, open the command palette and type > Sort lines
.
Fast access to CloudFormation documentation
If you are like me, you can’t remember the few hundred CloudFormation resource types and properties.
We can use VSCode Tasks to make our lives easier. A task is a simple block of JSON which can execute commands on our machine.
Inside your CloudFormation repositories, create a .vscode
folder in the root. Add a tasks.json
file with the following content:
⚠️ The above will only work on Mac, you will probably need to call Start-Process as a PowerShell Task on Windows.
From the Command Palette, choose > Tasks: Run Task
, and select CF Resource List
. This quick launches the AWS Resource Types Reference page.
For the CF Type Search
command to work, first highlight a CloudFormation resource type and then from the Command Palette, choose > Tasks: Run Task
, and select CF Type Search
.
This will take you to the AWS Documentation search page for the resource:
CloudFormation linting
Recently, AWS created a tool called cfn-python-lint, which checks your CloudFormation templates for errors. This gives early feedback and reduces cycle time when creating or updating CloudFormation templates.
Instead of submitting a bad template and waiting for the CloudFormation service tell you its bad, let VS Code tell you as you type!
To install the linter, you will need Python installed. If you are on Windows, I would recommend installing Python from Chocolatey.
Verify the install worked by running cfn-lint --version
.
Next, install the vscode-cfn-lint plugin.
Now when you are editing a CloudFormation template, you will get issues underlined and Problems listed when you make a mistake.
✅ You should also use cfn-lint as part of your validation of pull requests on your CloudFormation repositories.
Jumping around CloudFormation templates quickly
Got a template with a few thousand lines? Navigating it, and finding the block you are looking for, soon becomes a scroll or search-fest.
First, install the YAML Support by Red Hat extension.
Unfortunately, due to a bug 🐛 we are going to need to disable the YAML validation this extension provides. It currently has issues supporting CloudFormation intrinsic functions. I will update this post when this issue is fixed.
Add the following user setting to disable validation.
Inside your YAML file you will be able to see the logical names of all your resources, search through them and quickly jump to the right section of your CloudFormation template.
Conclusion
Even though CloudFormation frees up a HUGE amount of time for you as an Operations engineer, it doesn’t mean you shouldn’t keep optimizing your processes to be as efficient as possible.
Yaml Editor Online
Hopefully with these tips, you can go from a CloudFormation user to a CloudFormation rock star ⭐!