Automate a Standalone SharePoint Farm with CFN-INIT: Part 3

Today we are going learn to fully automate an entire SharePoint standalone Farm, which means you are going to bootstrap all your software using EC2’s Metadata property.

There is a lot of useful code here that you should find useful in automating a standalone SharePoint farm.

I wrote a post, Publish a SharePoint Farm Using CloudFormation, back in January. It links to this post and you might want to have a look at it if you haven’t done that yet.

By now, you’ve probably heard enough about the capabilities of CloudFormation, and how to create complex environments using it. Maybe you are trying to automate a whole environment, where CloudFormation is just part of it. Let me guess, once you have your O.S. just the way you want it, you simply create a new AMI from it, make a reference to it in your CloudFormation script, and you are ready to go. Did you do that?
What happens to your AMI after 180 days if you have any TRIAL software installed on it? It doesn’t matter if your EC2 Instance has booted up only twice in the first 180 days after its installation. The Microsoft trial licenses only care WHEN your software was installed and not for how many days it has been actually running.

Today we are going learn to fully automate an entire SharePoint standalone Farm, which means you are going to bootstrap all your software using EC2’s Metadata property. This means that your software will always be valid for the next 180 days starting when your stack begins running. This is a great solution for people looking for disposable full-featured environments, without any license (though legal) commitment.

Let’s go straight to our CloudFormation script and go through its most important features:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "AdminUser": {
            "Type": "String",
            "Description": "This will be the Admin user used to install all the binaries and initial SharePoint configs"
        },
        "PasswordAdminUser": {
            "Type": "String",
            "MinLength": "6",
            "Description": "This will be the Admin user used to install all the binaries and initial SharePoint configs"
        },
        "URLSQLInstaller": {
            "Type": "String",
            "Description": "Location of the ZIP file with SQL Server 2012 Installer (zip)"
        },
        "URLSHPInstaller": {
            "Type": "String",
            "Description": "Location of the ZIP file with SharePoint Server 2013 Installer (zip)"
        },
        "URLShPConfigScriptInstaller": {
            "Type": "String",
            "Description": "Location of the PowerShell config script"
        },
        "URLAutoSPInstaller": {
            "Type": "String",
            "Description": "Location of the AutoSPInstaller ZIP file"
        },
        "URLSHPInstallSPFiles": {
            "Type": "String",
            "Description": "Location of the bat file which will call a PowerShell script and run the prerequisiteinstaller.exe"
        },
        "URLConfigXML": {
            "Type": "String",
            "Description": "Location of the Config.xml for the unattended SharePoint installation"
        },
        "SharePointKEY": {
            "Type": "String",
            "Description": "License KEY for the corresponding SharePoint version being used by the Template"
        },
        "LocalDomain": {
            "Type": "String",
            "Description": "Example: sharepointtests.com"
        },
        "NETBIOSName": {
            "Type": "String",
            "Description": "Should be the same as the LocalDomain, without the .com, .local etc"
        },
        "ComputerName": {
            "Type": "String",
            "Description": "Name of the Single Server"
        },
        "SUBNETID": {
            "Type": "AWS::EC2::Subnet::Id",
            "Description": "ID of the existing Subnet"
        },
        "SECURITYGROUPID": {
            "Type": "AWS::EC2::SecurityGroup::Id",
            "Description": "ID of an existing Security Group"
        },
        "KEYPAIR": {
            "Type": "AWS::EC2::KeyPair::KeyName",
            "Description": "EC2 KeyPair"
        },
        "INSTANCETYPE": {
            "Type": "String",
            "Description": "EC2 KeyPair",
            "Default": "t2.medium"
        }
    },
    "Resources": {
        "TestDrivePublicIP": {
            "Type": "AWS::EC2::EIP",
            "Properties": {
                "InstanceId": {
                    "Ref": "Instance"
                }
            }
        },
        "Instance": {
            "Type": "AWS::EC2::Instance",
            "Metadata": {
                "AWS::CloudFormation::Init": {
                    "configSets": {
                        "config": [
                            "1-setup",
                            "2-rename",
                            "3-installADDS",
                            "4-extractInstallers",
                            "5-installSQL",
                            "6-installPreReqs1",
                            "7-installPreReqs2",
                            "8-installSP2013"
                        ]
                    },
                    "1-setup": {
                        "files": {
                            "c:\\cfn\\cfn-hup.conf": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "[main]\n",
                                            "stack=",
                                            {
                                                "Ref": "AWS::StackName"
                                            },
                                            "\n",
                                            "region=",
                                            {
                                                "Ref": "AWS::Region"
                                            },
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "[cfn-auto-reloader-hook]\n",
                                            "triggers=post.update\n",
                                            "path=Resources.Instance.Metadata.AWS::CloudFormation::Init\n",
                                            "action=cfn-init.exe -v -c config -s ",
                                            {
                                                "Ref": "AWS::StackId"
                                            },
                                            " -r Instance",
                                            " --region ",
                                            {
                                                "Ref": "AWS::Region"
                                            },
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\installers\\sqlinstaller.zip": {
                                "source": {
                                    "Ref": "URLSQLInstaller"
                                }
                            },
                            "c:\\installers\\AutoSPInstaller.zip": {
                                "source": {
                                    "Ref": "URLAutoSPInstaller"
                                }
                            },
                            "c:\\installers\\sharepointinstaller.zip": {
                                "source": {
                                    "Ref": "URLSHPInstaller"
                                }
                            },
                            "c:\\cfn\\scripts\\Install-SPFiles.ps1": {
                                "source": {
                                    "Ref": "URLSHPInstallSPFiles"
                                }
                            },
                            "c:\\cfn\\scripts\\InstallSPFiles.bat": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-SPFiles.ps1",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\scripts\\Install-SP2013.ps1": {
                                "source": {
                                    "Ref": "URLShPConfigScriptInstaller"
                                }
                            },
                            "c:\\cfn\\scripts\\InstallSP2013.bat": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-SP2013.ps1 -SQLServer ",
                                            {
                                                "Ref": "ComputerName"
                                            },
                                            "\\SHAREPOINT",
                                            " -SPFarmAccount ",
                                            {
                                                "Ref": "NETBIOSName"
                                            },
                                            "\\",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            " -Password ",
                                            {
                                                "Ref": "PasswordAdminUser"
                                            },
                                            " -Key ",
                                            {
                                                "Ref": "SharePointKEY"
                                            },
                                            " -CreateFarm",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\scripts\\config.xml": {
                                "source": {
                                    "Ref": "URLConfigXML"
                                }
                            }
                        },
                        "services": {
                            "windows": {
                                "cfn-hup": {
                                    "enabled": "true",
                                    "ensureRunning": "true",
                                    "files": [
                                        "c:\\cfn\\cfn-hup.conf",
                                        "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"
                                    ]
                                }
                            }
                        },
                        "commands": {
                            "a-disable-win-fw": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "b-set-execution-policy": {
                                "command": "powershell.exe -command Set-ExecutionPolicy Unrestricted -Force",
                                "waitAfterCompletion": "0"
                            }
                        }
                    },
                    "2-rename": {
                        "commands": {
                            "a-execute-powershell-script-RenameComputer": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe Rename-Computer -NewName ",
                                            {
                                                "Ref": "ComputerName"
                                            },
                                            " -Restart"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "forever"
                            }
                        }
                    },
                    "3-installADDS": {
                        "commands": {
                            "1-install-prereqs": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Install-WindowsFeature AD-Domain-Services, rsat-adds -IncludeAllSubFeature\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "2-install-adds": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command ",
                                            "\"Install-ADDSForest -DomainName ",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            " ",
                                            "-SafeModeAdministratorPassword (convertto-securestring ",
                                            {
                                                "Ref": "PasswordAdminUser"
                                            },
                                            " -asplaintext -force) ",
                                            "-DomainMode Win2012 ",
                                            "-DomainNetbiosName ",
                                            {
                                                "Ref": "NETBIOSName"
                                            },
                                            " ",
                                            "-ForestMode Win2012 ",
                                            "-Confirm:$false ",
                                            "-Force\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "forever"
                            },
                            "3-restart-service": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command Restart-Service NetLogon -EA 0"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "180"
                            },
                            "4-create-adminuser": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command ",
                                            "\"New-ADUser ",
                                            "-Name ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            " ",
                                            "-UserPrincipalName ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            "@",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            " ",
                                            "-AccountPassword (ConvertTo-SecureString ",
                                            {
                                                "Ref": "PasswordAdminUser"
                                            },
                                            " -AsPlainText -Force) ",
                                            "-Enabled $true ",
                                            "-PasswordNeverExpires $true\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "5-update-adminuser": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy Unrestricted -Command \"Add-ADGroupMember -Identity 'Domain Admins' -Members ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            ";Add-ADGroupMember -Identity 'Schema Admins' -Members ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            ";Add-ADGroupMember -Identity 'Enterprise Admins' -Members ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            ";\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            }
                        }
                    },
                    "4-extractInstallers": {
                        "commands": {
                            "1-extractSQLInstaller": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Add-Type -assembly 'system.io.compression.filesystem'; [io.compression.zipfile]::ExtractToDirectory('c:\\installers\\sqlinstaller.zip','c:\\installers\\sqlinstaller')\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "10"
                            },
                            "2-extractAutoSPInstaller": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Add-Type -assembly 'system.io.compression.filesystem'; [io.compression.zipfile]::ExtractToDirectory('c:\\installers\\AutoSPInstaller.zip','c:\\')\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "10"
                            },
                            "3-extractSharePointInstaller": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Add-Type -assembly 'system.io.compression.filesystem'; [io.compression.zipfile]::ExtractToDirectory('c:\\installers\\sharepointinstaller.zip','c:\\sp\\2013\\sharepoint')\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "10"
                            }
                        }
                    },
                    "5-installSQL": {
                        "commands": {
                            "1-runSetup": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Set-ExecutionPolicy Unrestricted -Force;c:\\installers\\sqlinstaller\\Setup.exe /q /Action=Install /Hideconsole /Features=SQL,Tools /InstanceName=SHAREPOINT /SQLCOLLATION='Latin1_General_CI_AS_KS_WS' /SQLSYSADMINACCOUNTS='Builtin\\Administrators' /SQLSVCACCOUNT='",
                                            {
                                                "Ref": "NETBIOSName"
                                            },
                                            "\\",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            "' /IAcceptSQLServerLicenseTerms=$true /SQLSVCPASSWORD='",
                                            {
                                                "Ref": "PasswordAdminUser"
                                            },
                                            "' /UpdateEnabled=$false\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "2-restartServer": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command \"Restart-Computer\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "forever"
                            }
                        }
                    },
                    "6-installPreReqs1": {
                        "commands": {
                            "a-install-rds": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command Install-WindowsFeature Net-Framework-Features,Web-Server,Web-WebServer,Web-Common-Http,Web-Static-Content,Web-Default-Doc,Web-Dir-Browsing,Web-Http-Errors,Web-App-Dev,Web-Asp-Net,Web-Net-Ext,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Health,Web-Http-Logging,Web-Log-Libraries,Web-Request-Monitor,Web-Http-Tracing,Web-Security,Web-Basic-Auth,Web-Windows-Auth,Web-Filtering,Web-Digest-Auth,Web-Performance,Web-Stat-Compression,Web-Dyn-Compression,Web-Mgmt-Tools,Web-Mgmt-Console,Web-Mgmt-Compat,Web-Metabase,Application-Server,AS-Web-Support,AS-TCP-Port-Sharing,AS-WAS-Support,AS-HTTP-Activation,AS-TCP-Activation,AS-Named-Pipes,AS-Net-Framework,WAS,WAS-Process-Model,WAS-NET-Environment,WAS-Config-APIs,Web-Lgcy-Scripting,Windows-Identity-Foundation,Server-Media-Foundation,Xps-Viewer -Restart"
                                        ]
                                    ]
                                }
                            }
                        }
                    },
                    "7-installPreReqs2": {
                        "commands": {
                            "a-enable-autologon": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command ",
                                            "\"New-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name AutoAdminLogon -Value 1;",
                                            "New-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultUserName -Value ",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            "@",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            ";",
                                            "New-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon' -Name DefaultPassword -Value ",
                                            {
                                                "Ref": "PasswordAdminUser"
                                            },
                                            "\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "b-set-startup-script": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command ",
                                            "\"New-ItemProperty -Path HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce -Name InstallSPFiles -Value C:\\cfn\\scripts\\InstallSPFiles.bat",
                                            "\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "c-reboot": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command Restart-Computer -Force"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "forever"
                            }
                        }
                    },
                    "8-installSP2013": {
                        "commands": {
                            "a-wait": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command Start-Sleep -Seconds 180"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "b-set-startup-script": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command ",
                                            "\"New-ItemProperty -Path HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce -Name InstallSPFiles2 -Value C:\\cfn\\scripts\\InstallSP2013.bat",
                                            "\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "c-reboot": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command Restart-Computer -Force"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "forever"
                            },
                            "d-disableLoopbackCheck": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command ",
                                            "\"New-ItemProperty -Path HKLM:\\System\\CurrentControlSet\\Control\\Lsa -Name DisableLoopbackCheck -value 1 -PropertyType dword -Force\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "e-wait": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -Command Start-Sleep -Seconds 1200"
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            },
                            "f-createNewWebApplication": {
                                "command": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "powershell.exe -ExecutionPolicy RemoteSigned -Command \"",
                                            "Add-PSSnapin 'microsoft.sharepoint.powershell';",
                                            "$ap = New-SPAuthenticationProvider;",
                                            "New-SPWebApplication -Name 'SharePoint Test Drive' -URL 'http://",
                                            {
                                                "Ref": "ComputerName"
                                            },
                                            ".",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            "' -Port 80 -ApplicationPool 'PortalAppPool' -ApplicationPoolAccount (Get-SPManagedAccount '",
                                            {
                                                "Ref": "NETBIOSName"
                                            },
                                            "\\",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            "') -AuthenticationProvider $ap;",
                                            "New-SPSite -URL 'http://",
                                            {
                                                "Ref": "ComputerName"
                                            },
                                            ".",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            "' -Name 'SharePoint Test Drive' -Template 'STS#0' -OwnerAlias '",
                                            {
                                                "Ref": "NETBIOSName"
                                            },
                                            "\\",
                                            {
                                                "Ref": "AdminUser"
                                            },
                                            "';",
                                            "$publicIP = invoke-restmethod -uri 'http://169.254.169.254/latest/meta-data/public-ipv4';",
                                            "$publicIPAddress = 'http://' + $publicIP;",
                                            "New-SPAlternateURL $publicIPAddress -Zone Internet -WebApplication 'http://",
                                            {
                                                "Ref": "ComputerName"
                                            },
                                            ".",
                                            {
                                                "Ref": "LocalDomain"
                                            },
                                            "';",
                                            "\""
                                        ]
                                    ]
                                },
                                "waitAfterCompletion": "0"
                            }
                        }
                    }
                }
            },
            "Properties": {
                "InstanceType": {
                    "Ref": "INSTANCETYPE"
                },
                "SubnetId": {
                    "Ref": "SUBNETID"
                },
                "ImageId": "ami-90d7ccfc",
                "EbsOptimized": "false",
                "SecurityGroupIds": [
                    {
                        "Ref": "SECURITYGROUPID"
                    }
                ],
                "KeyName": {
                    "Ref": "KEYPAIR"
                },
                "BlockDeviceMappings": [
                    {
                        "DeviceName": "/dev/sda1",
                        "Ebs": {
                            "VolumeSize": "45",
                            "VolumeType": "gp2"
                        }
                    }
                ],
                "UserData": {
                    "Fn::Base64": {
                        "Fn::Join": [
                            "",
                            [
                                "<powershell>\n",
                                "Set-ExecutionPolicy Unrestricted -Force\n",
                                "cfn-init.exe -v -c config -s ",
                                {
                                    "Ref": "AWS::StackId"
                                },
                                " -r Instance",
                                " --region ",
                                {
                                    "Ref": "AWS::Region"
                                },
                                "\n",
                                "cfn-signal.exe -e 0 '",
                                {
                                    "Ref": "InstanceWaitHandle"
                                },
                                "'\n",
                                "</powershell>\n"
                            ]
                        ]
                    }
                }
            }
        },
        "InstanceWaitCondition": {
            "Type": "AWS::CloudFormation::WaitCondition",
            "DependsOn": "Instance",
            "Properties": {
                "Handle": {
                    "Ref": "InstanceWaitHandle"
                },
                "Timeout": "5400"
            }
        },
        "InstanceWaitHandle": {
            "Type": "AWS::CloudFormation::WaitConditionHandle"
        }
    },
    "Outputs": {}
}

Check the index in case you want to skip any topic:

AWS::CloudFormation::Init

We used CFN-INIT along with UserData instead of just UserData. In our previous post, Publish a SharePoint Farm with CloudFormation, we employed only UserData because we had a fairly simple script. In this case, we are automating a full environment with a Domain Controller, SharePoint, SQL Server, several installer scripts, and restarts throughout the whole process.
Here are the main reasons why choosing CFN-INIT in addition to UserData was a good choice:

  1. ConfigSets: You can split all the necessary commands into several batches called configSets. This offers you the power of setting the order of execution for the different commands.
  2. Restarts during installation: This may not be noticeable at first, but CFN-INIT allows easy restarts during silent/scripted installations with the waitAfterCompletion property. The big advantage here is that when the waitAfterCompletion is set to FOREVER, and that current command requests the instance to restart, the next configSet of the CloudFormation script only runs when the instance is back from the restart.

Bootstrapping with CFN-INIT is more automated when comparing to the traditional UserData. One example is reboot handling. Windows reboots are not automatically detected when bootstrapping just using UserData, but it actually has to be manually handled either using the Windows registry, scheduled tasks or PowerShell workflows when you execute any cmdlet which requires a reboot.

Parameters

Parameters are well known with CloudFormation enthusiasts like us. However, a bit more than 1 year ago, new types of Parameters were launched and introduced by Lee Kemp from AWS. You can check all the AWS Resource parameters here.

The advantage of this type  of parameter is that you no longer need track the AWS ID resources. Instead, AWS ClodFormation’s page offers a DropDown list like this one down below:
Standalone SharePointConfigSets
"configSets": {
    "config": [
        "1-setup",
        "2-rename",
        "3-installADDS",
        "4-extractInstallers",
        "5-installSQL",
        "6-installPreReqs1",
        "7-installPreReqs2",
        "8-installSP2013"
    ]
}

ConfigSets gives your the ability to have more than one section of code containing Packages, Groups, Users, Sources, Files, Commands and Services. This means that for every ConfigSet, the entire previous code structure will repeat itself. In our case, above, we have 1 configSet, named “config” and within it 8 functions which are executed alphabetically. This means that you’ll want to use prefixes (I.E. 1-SetupSQL, 2-SetupSharePoint) just to be sure you run it in the order you want, and also keep the names intuitive.

Files

"setup": {
    "files": {
        "c:\\cfn\\cfn-hup.conf": {
            "content": {
                "Fn::Join": [
                    "",
                    [
                        "[main]\n",
                        "stack=",
                        {
                            "Ref": "AWS::StackName"
                        },
                        "\n",
                        "region=",
                        {
                            "Ref": "AWS::Region"
                        },
                        "\n"
                    ]
                ]
            }
        }
    }
}

In the files section, you can create new files by just setting a path and using the “content” property, where you write your commands. This saves you S3 costs, and, maybe more importantly, administration tasks, such as permissions management.

{
    "c:\\installers\\sqlinstaller.zip": {
        "source": {
            "Ref": "URLSQLInstaller"
        }
    }
}

The example above shows it is possible to create local files using S3 URL’s as a source, coming as an input from the parameters.

Services

{
    "services": {
        "windows": {
            "cfn-hup": {
                "enabled": "true",
                "ensureRunning": "true",
                "files": [
                    "c:\\cfn\\cfn-hup.conf",
                    "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"
                ]
            }
        }
    }
}

CFN-HUP OS services can be started upon boot process. Here we are making sure that the very CFN-HUP services are being started, which means the CloudFormation script will just continue from where it’s stopped before the system restarts.

Commands

{
    "commands": {
        "a-disable-win-fw": {
            "command": {
                "Fn::Join": [
                    "",
                    [
                        "powershell.exe -Command \"Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False\""
                    ]
                ]
            },
            "waitAfterCompletion": "0"
        }
    }
}

In Commands, you can brake your code into sections and call them separately in whatever order you prefer, like Functions in a Javascript code. For example, the command in the code snippet above is called “a-disable-win-fw”, which means a powershell cmdlet will disable Windows’ firewall and go to the next command right away, since “waitAfterCompletion” is set to 0.
In cases where you need a restart after a command is executed, just set “waitAfterCondition” to “FOREVER”, meaning that CFN-HUP will take over the script where it left off after the restart.
Here’s another example where the command waits “forever” until the restart is done:

{
    "rename": {
        "commands": {
            "a-execute-powershell-script-RenameComputer": {
                "command": {
                    "Fn::Join": [
                        "",
                        [
                            "powershell.exe Rename-Computer -NewName ",
                            {
                                "Ref": "ComputerName"
                            },
                            " -Restart"
                        ]
                    ]
                },
                "waitAfterCompletion": "forever"
            }
        }
    }
}

UserData

{
    "UserData": {
        "Fn::Base64": {
            "Fn::Join": [
                "",
                [
                    "<powershell>\n",
                    "Set-ExecutionPolicy Unrestricted -Force\n",
                    "cfn-init.exe -v -c config -s ",
                    {
                        "Ref": "AWS::StackId"
                    },
                    " -r Instance",
                    " --region ",
                    {
                        "Ref": "AWS::Region"
                    },
                    "\n",
                    "cfn-signal.exe -e 0 '",
                    {
                        "Ref": "InstanceWaitHandle"
                    },
                    "'\n",
                    "</powershell>\n"
                ]
            ]
        }
    }
}

At the end we employ UserData to make sure the CFN-INIT will start, and that it will send a signal to the CloudFormation script.
Once this Stack is up and running, you’ll have a full SharePoint Server Standard with SQL Server 2014 Express running in a local Domain.
Let’s make this script even more complete. We should add the option to restore Site Collections backups. This way,  during the boot process, a .BKP file will be downloaded from S3 and restored as a new Site Collection on SharePoint.

This is perfect for testing and debugging custom .wsp solutions or even SharePoint’s OOTB web parts and services where you need real data from productive servers.
For that, you need to make sure both SharePoint installations have the same version in order to simply restore Site Collections via PowerShell — but that’s another topic.

I hope you enjoyed our Metadata overview. We dipped into ways of automating your SharePoint provision too. I am passionate about SharePoint and love feedback from readers and fellow developers. I learn by teaching and one of the greatest rewards is hearing how others have innovated or iterated on a technique I have presented. Please share the knowledge with others and comment if you have time. Cloud Academy is growing and user feedback remains the most valuable data we can find. Cloud Academy offers a 7-day trial where anyone can use the course, labs, quizzes, and learning paths. Try out a lab and put what you read to work.

Avatar

Written by

Rafael Nunes

I am Brazilian, 27 years old and currently living in Munich, Germany, working as SharePoint and AWS consultant. I have 9 years working with I.T. where last 4 years have been focused on SharePoint and Cloud technologies.


Related Posts

Joe Nemer
Joe Nemer
— September 15, 2020

New Content: Azure DP-100 Certification, Alibaba Cloud Certified Associate Prep, 13 Security Labs, and Much More

This past month our Content Team served up a heaping spoonful of new and updated content. Not only did our experts release the brand new Azure DP-100 Certification Learning Path, but they also created 18 new hands-on labs — and so much more! New content on Cloud Academy At any time, y...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Joe Nemer
Joe Nemer
— August 28, 2020

AWS Certification Practice Exam: What to Expect from Test Questions

If you’re building applications on the AWS cloud or looking to get started in cloud computing, certification is a way to build deep knowledge in key services unique to the AWS platform. AWS currently offers 12 certifications that cover major cloud roles including Solutions Architect, De...

Read more
  • AWS
  • AWS Certifications
Patrick Navarro
Patrick Navarro
— August 25, 2020

Overcoming Unprecedented Business Challenges with AWS

From auto-scaling applications with high availability to video conferencing that’s used by everyone, every day —  cloud technology has never been more popular or in-demand. But what does this mean for experienced cloud professionals and the challenges they face as they carve out a new p...

Read more
  • AWS
  • Cloud Adoption
  • digital transformation
Avatar
Andrew Larkin
— August 18, 2020

Constant Content: Cloud Academy’s Q3 2020 Roadmap

Hello —  Andy Larkin here, VP of Content at Cloud Academy. I am pleased to release our roadmap for the next three months of 2020 — August through October. Let me walk you through the content we have planned for you and how this content can help you gain skills, get certified, and...

Read more
  • alibaba
  • AWS
  • Azure
  • content roadmap
  • Content updates
  • DevOps
  • GCP
  • Google Cloud
  • New content
Alisha Reyes
Alisha Reyes
— August 5, 2020

New Content: Alibaba, Azure AZ-303 and AZ-304, Site Reliability Engineering (SRE) Foundation, Python 3 Programming, 16 Hands-on Labs, and Much More

This month our Content Team did an amazing job at publishing and updating a ton of new content. Not only did our experts release the brand new AZ-303 and AZ-304 Certification Learning Paths, but they also created 16 new hands-on labs — and so much more! New content on Cloud Academy At...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Alisha Reyes
Alisha Reyes
— July 16, 2020

Blog Digest: Which Certifications Should I Get?, The 12 Microsoft Azure Certifications, 6 Ways to Prevent a Data Breach, and More

This month, we were excited to announce that Cloud Academy was recognized in the G2 Summer 2020 reports! These reports highlight the top-rated solutions in the industry, as chosen by the source that matters most: customers. We're grateful to have been nominated as a High Performer in se...

Read more
  • AWS
  • Azure
  • blog digest
  • Certifications
  • Cloud Academy
  • OWASP
  • OWASP Top 10
  • Security
  • VPCs
Avatar
Cloud Academy Team
— July 9, 2020

Which Certifications Should I Get?

The old AWS slogan, “Cloud is the new normal” is indeed a reality today. Really, cloud has been the new normal for a while now and getting credentials has become an increasingly effective way to quickly showcase your abilities to recruiters and companies. With all that in mind, the s...

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Alisha Reyes
Alisha Reyes
— July 2, 2020

New Content: AWS, Azure, Typescript, Java, Docker, 13 New Labs, and Much More

This month, our Content Team released a whopping 13 new labs in real cloud environments! If you haven't tried out our labs, you might not understand why we think that number is so impressive. Our labs are not “simulated” experiences — they are real cloud environments using accounts on A...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Joe Nemer
Joe Nemer
— June 19, 2020

Kickstart Your Tech Training With a Free Week on Cloud Academy

Are you looking to make a jump in your technical career? Want to get trained or certified on AWS, Azure, Google Cloud Platform, DevOps, Kubernetes, Python, or another in-demand skill? Then you'll want to mark your calendar. Starting Monday, June 22 at 12:00 a.m. PDT (3:00 a.m. EDT), ...

Read more
  • AWS
  • Azure
  • cloud academy content
  • complimentary access
  • GCP
  • on the house
Alisha Reyes
Alisha Reyes
— June 11, 2020

New Content: AZ-500 and AZ-400 Updates, 3 Google Professional Exam Preps, Practical ML Learning Path, C# Programming, and More

This month, our Content Team released tons of new content and labs in real cloud environments. Not only that, but we introduced our very first highly interactive "Office Hours" webinar. This webinar, Acing the AWS Solutions Architect Associate Certification, started with a quick overvie...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Rebecca Willis
Rebecca Willis
— June 3, 2020

Azure vs. AWS: Which Certification Provides the Brighter Future?

More and more companies are using cloud services, prompting more and more people to switch their current IT position to something cloud-related. The problem is most people only have that much time after work to learn new technologies, and there are plenty of cloud services that you can ...

Read more
  • AWS
  • Azure
  • certification
Alisha Reyes
Alisha Reyes
— June 2, 2020

Blog Digest: 5 Reasons to Get AWS Certified, OWASP Top 10, Getting Started with VPCs, Top 10 Soft Skills, and More

Thank you for being a valued member of our community! We recently sent out a short survey to understand what type of content you would like us to add to Cloud Academy, and we want to thank everyone who gave us their input. If you would like to complete the survey, it's not too late. It ...

Read more
  • AWS
  • Azure
  • blog digest
  • Certifications
  • Cloud Academy
  • OWASP
  • OWASP Top 10
  • Security
  • VPCs