On the joy of writing a mix of code with PowerShell, YAML, .NET, XML, and XPath. It was a lot of fun.
The task at hand is to write a simple preliminary version of a PowerShell script to create some AWS CloudWatch LogGroups and related LogStreams. The input is an AWS CloudFormation template YAML file. This template file helped before to create the required CloudWatch resources, but now the need is to create the same resources by calls to the AWS CloudWatch Logs API. At execution time, at an AWS EC2 instance, the required PowerShell 7 script will call AWS CloudWatch Logs API by means of the AWS Tools for PowerShell already installed at such EC2 instance, which has attached an AWS IAM Instance Profile with the required permissions.
Curious thing, though, the PowerShell Cmdlets to parse and query the YAML template data were not found as quickly as the Cmdlets to parse and query other common file formats (JSON, XML, CSV, etc.).
PowerShell 7 can load and execute code in .NET Assemblies (given the proper match on targeted .NET CLR). There is, e.g., YamlDotNet package to parse YAML files, but an initial try of it directly from PowerShell was not as straightforward as expected. Nevertheless, that same package could be used from a custom .NET Assembly that would render the YAML content transposed as another serialization format already known to familiar PowerShell Cmdlets. The first use case for such custom .NET Assembly would be like the following test case in an executable functional specification:
The System Under Test (SUT) is the .NET class named yaml
, which encapsulates the access to a YAML parser (deserial
method) and to the rendition of the CloudWatch Logs resources data transposed as XML serialization format version 1.0 (AsXml_1dot0
method). This AsXml_1dot0
method can be wrapped by a .NET Console host and invoked from a PowerShell script. The output of that call to the AsXml_1dot0
method can be parsed and queried by Select-Xml Cmdlet.
Next is a general idea for the .NET Console host wrapper for the AsXml_1dot0
method:
Before getting into the general layout for the required PowerShell script (Create-CloudWatchLogs.ps1
), first let’s see next a basic layout for its invocation and its output:
Also, next is the related input file CloudWatchLogs.yml
(an AWS CloudFormation template YAML file) with the data for the resources to be created:
Now, the executable functional specification, so far, includes the case to transpose from a YAML map data type to XML. The YAML file in this case contains only map type instances, but for the case of YAML seq data type, next is an initial test case into the executable functional specification:
And, next is an initial test case for the case of YAML str data type:
Furthermore, next is a test case for YAML map and YAML seq data types combined:
Next is the general layout of the required PowerShell script Create-CloudWatchLogs.ps1
:
I especially enjoyed the craft of those XPath expressions; it has been a while since my last time with XPath.
Finally, next is the current initial implementation of the yaml
class with the parsing and transposition of YAML into XML: