Skip to content

Cmdlet Conventions

Eugene Bekker edited this page Apr 5, 2016 · 8 revisions

The posh-awscfn PowerShell module contains a number cmdlets that form the vocabulary used to craft CloudFormation (CFN) Templates.

All of the cmdlets have two forms, the normative form which conforms with the PowerShell convention of *<verb>*-*<noun>*, and the diminutive form which is more succinct and helps to define a more natural, declarative convention. The diminutive forms are simply aliases for the normative forms.

Normative Diminutive Description
New-CfnTemplate Template Starts the definition of a template document. more...
Add-CfnParameter Parameter Adds a Parameter to the enclosing parent template document. more...
Add-CfnMapping Mapping Adds a Mapping to the enclosing parent template document. more...
Add-CfnCondition Condition Adds a Condition to the enclosing parent template document. more...
Add-CfnResource Resource Adds a Resource to the enclosing parent template document using the generic resource convention. more...
Add-CfnOutput Output Adds an Output to the enclosing parent template document. more...
Add-Cfn<prd>-<res>Resource Res-<prd>-<res> Adds a Resource to the enclosing parent template document using the strongly-typed, resource-specific convention.
Use-CfnPseudoParameter Pseudo Reference a CloudFormation pseudo-parameter.
Use-Cfn<func>Function Fn-<func> Reference an Intrinsic Function.
Use-Cfn<func>Condition Fn-<func> Reference an Intrinsic Condition Function.
Set-CfnResourceProperty Property Adds a property to the enclosing parent resource. more...
Set-CfnResourceTag Tag Adds a tag entry to the enclosing parent resource. more...

Sample Usage

Here is a sample of these cmdlets in action forming a CFN template.

ipmo AwsCfn

Template -Description "Sample CloudFormation Template" {

  Parameter DBName String -Default "MyDatabase" `
    -MinLength 1 -MaxLength 64 -AllowedPattern "[a-zA-Z][a-zA-Z0-9]*" `
    -Description "The database name" `
    -ConstraintDescription "must begin with a letter and contain only alphanumeric characters."

  Parameter DBPassword String -NoEcho `
    -MinLength 1 -MaxLength 41 -AllowedPattern "[a-zA-Z0-9]+" `
    -Description "The database admin account password" `
    -ConstraintDescription "must contain only alphanumeric characters."

  Condition "Is-EC2-VPC" (Fn-Or @(
    (Fn-Equals (Pseudo Region) "eu-central-1")
    (Fn-Equals (Pseudo Region) "cn-north-1")
  ))

  ## Strongly-typed Resource definition
  Res-RDS-DBInstance MasterDB -DeletionPolicy Snapshot `
    -Engine MySQL -DBName MyDB -AllocatedStorage 5 `
    -DBInstanceClass db.m1.large -MasterUsername dbuser {
      Property MasterUserPassword (Fn-Ref DBPassword)
    }

  Output MasterJDBCConnectionString `
    -Description "JDBC connection string for the master database" `
    -Value (Fn-Join "" @(
      "jdbc:mysql://"
      (Fn-GetAtt MasterDB "Endpoint.Address")
      ":"
      (Fn-GetAtt MasterDB "Endpoint.Port")
      "/"
      (Fn-Ref DBName)
  ))

}

And here is the JSON output it produces:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Sample CloudFormation Template",
  "Parameters": {
    "DBName": {
      "Type": "String",
      "Description": "The database name",
      "ConstraintDescription": "must begin with a letter and contain only alphanumeric characters.",
      "Default": "MyDatabase",
      "AllowedPattern": "[a-zA-Z][a-zA-Z0-9]*",
      "MinLength": "1",
      "MaxLength": "64"
    },
    "DBPassword": {
      "Type": "String",
      "Description": "The database admin account password",
      "ConstraintDescription": "must contain only alphanumeric characters.",
      "NoEcho": "True",
      "AllowedPattern": "[a-zA-Z0-9]+",
      "MinLength": "1",
      "MaxLength": "41"
    }
  },
  "Conditions": {
    "Is-EC2-VPC": {
      "Fn::Or": [
        {
          "Fn::Equals": [
            {
              "Ref": "AWS::Region"
            },
            "eu-central-1"
          ]
        },
        {
          "Fn::Equals": [
            {
              "Ref": "AWS::Region"
            },
            "cn-north-1"
          ]
        }
      ]
    }
  },
  "Resources": {
    "MasterDB": {
      "Type": "AWS::RDS::DBInstance",
      "DeletionPolicy": "Snapshot",
      "Properties": {
        "AllocatedStorage": "5",
        "DBInstanceClass": "db.m1.large",
        "DBName": "MyDB",
        "Engine": "MySQL",
        "MasterUsername": "dbuser",
        "MasterUserPassword": {
          "Ref": "DBPassword"
        }
      }
    }
  },
  "Outputs": {
    "MasterJDBCConnectionString": {
      "Value": {
        "Fn::Join": [
          "",
          [
            "jdbc:mysql://",
            {
              "Fn::GetAtt": [
                "MasterDB",
                "Endpoint.Address"
              ]
            },
            ":",
            {
              "Fn::GetAtt": [
                "MasterDB",
                "Endpoint.Port"
              ]
            },
            "/",
            {
              "Ref": "DBName"
            }
          ]
        ]
      },
      "Description": "JDBC connection string for the master database"
    }
  }
}