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

Alisha Reyes
Alisha Reyes
— January 6, 2020

New on Cloud Academy: Red Hat, Agile, OWASP Labs, Amazon SageMaker Lab, Linux Command Line Lab, SQL, Git Labs, Scrum Master, Azure Architects Lab, and Much More

Happy New Year! We hope you're ready to kick your training in overdrive in 2020 because we have a ton of new content for you. Not only do we have a bunch of new courses, hands-on labs, and lab challenges on AWS, Azure, and Google Cloud, but we also have three new courses on Red Hat, th...

Read more
  • agile
  • AWS
  • Azure
  • Google Cloud Platform
  • Linux
  • OWASP
  • programming
  • red hat
  • scrum
Alisha Reyes
Alisha Reyes
— December 24, 2019

Cloud Academy’s Blog Digest: Azure Best Practices, 6 Reasons You Should Get AWS Certified, Google Cloud Certification Prep, and more

Happy Holidays from Cloud Academy We hope you have a wonderful holiday season filled with family, friends, and plenty of food. Here at Cloud Academy, we are thankful for our amazing customer like you.  Since this time of year can be stressful, we’re sharing a few of our latest article...

Read more
  • AWS
  • azure best practices
  • blog digest
  • Cloud Academy
  • Google Cloud
Avatar
Guy Hummel
— December 12, 2019

Google Cloud Platform Certification: Preparation and Prerequisites

Google Cloud Platform (GCP) has evolved from being a niche player to a serious competitor to Amazon Web Services and Microsoft Azure. In 2019, research firm Gartner placed Google in the Leaders quadrant in its Magic Quadrant for Cloud Infrastructure as a Service for the second consecuti...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
Alisha Reyes
Alisha Reyes
— December 10, 2019

New Lab Challenges: Push Your Skills to the Next Level

Build hands-on experience using real accounts on AWS, Azure, Google Cloud Platform, and more Meaningful cloud skills require more than book knowledge. Hands-on experience is required to translate knowledge into real-world results. We see this time and time again in studies about how pe...

Read more
  • AWS
  • Azure
  • Google Cloud
  • hands-on
  • labs
Alisha Reyes
Alisha Reyes
— December 5, 2019

New on Cloud Academy: AWS Solution Architect Lab Challenge, Azure Hands-on Labs, Foundation Certificate in Cyber Security, and Much More

Now that Thanksgiving is over and the craziness of Black Friday has died down, it's now time for the busiest season of the year. Whether you're a last-minute shopper or you already have your shopping done, the holidays bring so much more excitement than any other time of year. Since our...

Read more
  • AWS
  • AWS solution architect
  • AZ-203
  • Azure
  • cyber security
  • FCCS
  • Foundation Certificate in Cyber Security
  • Google Cloud Platform
  • Kubernetes
Avatar
Cloud Academy Team
— December 4, 2019

Understanding Enterprise Cloud Migration

What is enterprise cloud migration? Cloud migration is about moving your data, applications, and even infrastructure from your on-premises computers or infrastructure to a virtual pool of on-demand, shared resources that offer compute, storage, and network services at scale. Why d...

Read more
  • AWS
  • Azure
  • Data Migration
Wendy Dessler
Wendy Dessler
— November 27, 2019

6 Reasons Why You Should Get an AWS Certification This Year

In the past decade, the rise of cloud computing has been undeniable. Businesses of all sizes are moving their infrastructure and applications to the cloud. This is partly because the cloud allows businesses and their employees to access important information from just about anywhere. ...

Read more
  • AWS
  • Certifications
  • certified
Avatar
Andrea Colangelo
— November 26, 2019

AWS Regions and Availability Zones: The Simplest Explanation You Will Ever Find Around

The basics of AWS Regions and Availability Zones We’re going to treat this article as a sort of AWS 101 — it’ll be a quick primer on AWS Regions and Availability Zones that will be useful for understanding the basics of how AWS infrastructure is organized. We’ll define each section,...

Read more
  • AWS
Avatar
Dzenan Dzevlan
— November 20, 2019

Application Load Balancer vs. Classic Load Balancer

What is an Elastic Load Balancer? This post covers basics of what an Elastic Load Balancer is, and two of its examples: Application Load Balancers and Classic Load Balancers. For additional information — including a comparison that explains Network Load Balancers — check out our post o...

Read more
  • ALB
  • Application Load Balancer
  • AWS
  • Elastic Load Balancer
  • ELB
Albert Qian
Albert Qian
— November 13, 2019

Advantages and Disadvantages of Microservices Architecture

What are microservices? Let's start our discussion by setting a foundation of what microservices are. Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs). ...

Read more
  • AWS
  • Docker
  • Kubernetes
  • Microservices
Nisar Ahmad
Nisar Ahmad
— November 12, 2019

Kubernetes Services: AWS vs. Azure vs. Google Cloud

Kubernetes is a popular open-source container orchestration platform that allows us to deploy and manage multi-container applications at scale. Businesses are rapidly adopting this revolutionary technology to modernize their applications. Cloud service providers — such as Amazon Web Ser...

Read more
  • AWS
  • Azure
  • Google Cloud
  • Kubernetes
Avatar
Stuart Scott
— October 31, 2019

AWS Internet of Things (IoT): The 3 Services You Need to Know

The Internet of Things (IoT) embeds technology into any physical thing to enable never-before-seen levels of connectivity. IoT is revolutionizing industries and creating many new market opportunities. Cloud services play an important role in enabling deployment of IoT solutions that min...

Read more
  • AWS
  • AWS IoT Events
  • AWS IoT SiteWise
  • AWS IoT Things Graph
  • IoT