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

Amanda Cross
Amanda Cross
— April 9, 2021

New Content: Platforms, Programming, and DevOps – Something for Everyone

This month our team of expert certification specialists released three new or updated learning paths, 16 courses, 13 hands-on labs, and four lab challenges! New content on Cloud Academy You can always visit our Content Roadmap to see what’s just released as well as what’s coming soon....

Read more
  • alibaba
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • programming
  • Security
Luca Casartelli
Luca Casartelli
— March 31, 2021

Mastering AWS Organizations Service Control Policies

Service Control Policies (SCPs) are IAM-like policies to manage permissions in AWS Organizations. SCPs restrict the actions allowed for accounts within the organization making each one of them compliant with your guidelines. SCPs are not meant to grant permissions; you should consider ...

Read more
  • AWS
  • Organizations
  • SCP
Amanda Cross
Amanda Cross
— March 12, 2021

New Content: Focus on DevOps and Programming Content this Month

This month our team of expert certification specialists released 12 new or updated learning paths, 15 courses, 25 hands-on labs, and four lab challenges! New content on Cloud Academy You can always visit our Content Roadmap to see what’s just released as well as what’s coming soon. Ja...

Read more
  • alibaba
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • programming
Amanda Cross
Amanda Cross
— February 12, 2021

New Content: Get Ready for the CISM Cert Exam & Learn About Alibaba, Plus All the AWS, GCP, and Azure Courses You Know You Can Count On

This month our team of intrepid certification specialists released five learning paths, seven courses, 19 hands-on labs, and three lab challenges!  One particularly interesting new learning path is Certified Information Security Manager (CISM) Foundations. After completing this learn...

Read more
  • alibaba
  • AWS
  • Azure
  • cism
  • DevOps
  • Google Cloud Platform
  • programming
Avatar
Cloud Academy Team
— January 31, 2021

Which Certifications Should I Get?

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

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Avatar
Andrew Larkin
— January 31, 2021

The 12 AWS Certifications: Which is Right for You and Your Team?

As companies increasingly shift workloads to the public cloud, cloud computing has moved from a nice-to-have to a core competency in the enterprise. This shift requires a new set of skills to design, deploy, and manage applications in cloud computing. As the market leader and most ma...

Read more
  • AWS
  • AWS Certifications
Avatar
Stuart Scott
— January 29, 2021

AWS Certified Solutions Architect Associate: A Study Guide

Want to take a really impactful step in your technical career? Explore the AWS Solutions Architect Associate certificate. Its new version (SAA-C02) was released on March 23, 2020. The AWS Solutions Architect - Associate Certification (or Sol Arch Associate for short) offers some ...

Read more
  • AWS
  • AWS Certifications
  • AWS Certified Solutions Architect Associate
Amanda Cross
Amanda Cross
— January 7, 2021

New Content: AWS Terraform, Java Programming Lab Challenges, Azure DP-900 & DP-300 Certification Exam Prep, Plus Plenty More Amazon, Google, Microsoft, and Big Data Courses

This month our Content Team continues building the catalog of courses for everyone learning about AWS, GCP, and Microsoft Azure. In addition, this month’s updates include several Java programming lab challenges and a couple of courses on big data. In total, we released five new learning...

Read more
  • AWS
  • Azure
  • DevOps
  • Google Cloud Platform
  • Machine Learning
  • programming
Avatar
Stuart Scott
— December 17, 2020

Where Should You Be Focusing Your AWS Security Efforts?

Another day, another re:Invent session! This time I listened to Stephen Schmidt’s session, “AWS Security: Where we've been, where we're going.” Amongst covering the highlights of AWS security during 2020, a number of newly added AWS features/services were discussed, including: AWS Audit...

Read more
  • AWS
  • AWS re:Invent
  • cloud security
Joe Nemer
Joe Nemer
— December 4, 2020

AWS re:Invent: 2020 Keynote Top Highlights and More

We’ve gotten through the first five days of the special all-virtual 2020 edition of AWS re:Invent. It’s always a really exciting time for practitioners in the field to see what features and services AWS has cooked up for the year ahead.  This year’s conference is a marathon and not a...

Read more
  • AWS
  • AWS Glue Elastic Views
  • AWS re:Invent
Bryony Harrower
Bryony Harrower
— November 6, 2020

WARNING: Great Cloud Content Ahead

At Cloud Academy, content is at the heart of what we do. We work with the world’s leading cloud and operations teams to develop video courses and learning paths that accelerate teams and drive digital transformation. First and foremost, we listen to our customers’ needs and we stay ahea...

Read more
  • AWS
  • Azure
  • content roadmap
  • GCP
Joe Nemer
Joe Nemer
— October 25, 2020

Excelling in AWS, Azure, and Beyond – How Danut Prisacaru Prepares for the Future

Meet Danut Prisacaru. Danut has been a Software Architect for the past 10 years and has been involved in Software Engineering for 30 years. He’s passionate about software and learning, and jokes that coding is basically the only thing he can do well (!). We think his enthusiasm shines t...

Read more
  • AWS
  • careers
  • champions
  • upskilling