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

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
Avatar
Cloud Academy Team
— October 23, 2019

Which Certifications Should I Get?

As we mentioned in an earlier post, 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 compan...

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Valery Calderón Briz
Valery Calderón Briz
— October 22, 2019

How to Go Serverless Like a Pro

So, no servers? Yeah, I checked and there are definitely no servers. Well...the cloud service providers do need servers to host and run the code, but we don’t have to worry about it. Which operating system to use, how and when to run the instances, the scalability, and all the arch...

Read more
  • AWS
  • Lambda
  • Serverless
Avatar
Stuart Scott
— October 16, 2019

AWS Security: Bastion Hosts, NAT instances and VPC Peering

Effective security requires close control over your data and resources. Bastion hosts, NAT instances, and VPC peering can help you secure your AWS infrastructure. Welcome to part four of my AWS Security overview. In part three, we looked at network security at the subnet level. This ti...

Read more
  • AWS
Avatar
Sudhi Seshachala
— October 9, 2019

Top 13 Amazon Virtual Private Cloud (VPC) Best Practices

Amazon Virtual Private Cloud (VPC) brings a host of advantages to the table, including static private IP addresses, Elastic Network Interfaces, secure bastion host setup, DHCP options, Advanced Network Access Control, predictable internal IP ranges, VPN connectivity, movement of interna...

Read more
  • AWS
  • best practices
  • VPC
Avatar
Stuart Scott
— October 2, 2019

Big Changes to the AWS Certification Exams

With AWS re:Invent 2019 just around the corner, we can expect some early announcements to trickle through with upcoming features and services. However, AWS has just announced some big changes to their certification exams. So what’s changing and what’s new? There is a brand NEW ...

Read more
  • AWS
  • Certifications
Alisha Reyes
Alisha Reyes
— October 1, 2019

New on Cloud Academy: ITIL® 4, Microsoft 365 Tenant, Jenkins, TOGAF® 9.1, and more

At Cloud Academy, we're always striving to make improvements to our training platform. Based on your feedback, we released some new features to help make it easier for you to continue studying. These new features allow you to: Remove content from “Continue Studying” section Disc...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
  • ITIL® 4
  • Jenkins
  • Microsoft 365 Tenant
  • New content
  • Product Feature
  • Python programming
  • TOGAF® 9.1
Avatar
Stuart Scott
— September 27, 2019

AWS Security Groups: Instance Level Security

Instance security requires that you fully understand AWS security groups, along with patching responsibility, key pairs, and various tenancy options. As a precursor to this post, you should have a thorough understanding of the AWS Shared Responsibility Model before moving onto discussi...

Read more
  • AWS
  • instance security
  • Security
  • security groups
Avatar
Jeremy Cook
— September 17, 2019

Cloud Migration Risks & Benefits

If you’re like most businesses, you already have at least one workload running in the cloud. However, that doesn’t mean that cloud migration is right for everyone. While cloud environments are generally scalable, reliable, and highly available, those won’t be the only considerations dri...

Read more
  • AWS
  • Azure
  • Cloud Migration
Joe Nemer
Joe Nemer
— September 12, 2019

Real-Time Application Monitoring with Amazon Kinesis

Amazon Kinesis is a real-time data streaming service that makes it easy to collect, process, and analyze data so you can get quick insights and react as fast as possible to new information.  With Amazon Kinesis you can ingest real-time data such as application logs, website clickstre...

Read more
  • amazon kinesis
  • AWS
  • Stream Analytics
  • Streaming data
Joe Nemer
Joe Nemer
— September 6, 2019

Google Cloud Functions vs. AWS Lambda: The Fight for Serverless Cloud Domination

Serverless computing: What is it and why is it important? A quick background The general concept of serverless computing was introduced to the market by Amazon Web Services (AWS) around 2014 with the release of AWS Lambda. As we know, cloud computing has made it possible for users to ...

Read more
  • AWS
  • Azure
  • Google Cloud Platform