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

Avatar
Michael Sheehy
— August 19, 2019

What Exactly Is a Cloud Architect and How Do You Become One?

One of the buzzwords surrounding the cloud that I'm sure you've heard is "Cloud Architect." In this article, I will outline my understanding of what a cloud architect does and I'll analyze the skills and certifications necessary to become one. I will also list some of the types of jobs ...

Read more
  • AWS
  • Cloud Computing
Avatar
Andrew Larkin
— August 13, 2019

Content Roadmap: AZ-500, ITIL 4, MS-100, Google Cloud Associate Engineer, and More

Last month, Cloud Academy joined forces with QA, the UK’s largest B2B skills provider, and it put us in an excellent position to solve a massive skills gap problem. As a result of this collaboration, you will see our training library grow with additions from QA’s massive catalog of 500+...

Read more
  • AWS
  • Azure
  • content roadmap
  • Google Cloud Platform
Avatar
Adam Hawkins
— August 9, 2019

DevSecOps: How to Secure DevOps Environments

Security has been a friction point when discussing DevOps. This stems from the assumption that DevOps teams move too fast to handle security concerns. This makes sense if Information Security (InfoSec) is separate from the DevOps value stream, or if development velocity exceeds the band...

Read more
  • AWS
  • cloud security
  • DevOps
  • DevSecOps
  • Security
Avatar
Stefano Giacone
— August 8, 2019

Test Your Cloud Knowledge on AWS, Azure, or Google Cloud Platform

Cloud skills are in demand | In today's digital era, employers are constantly seeking skilled professionals with working knowledge of AWS, Azure, and Google Cloud Platform. According to the 2019 Trends in Cloud Transformation report by 451 Research: Business and IT transformations re...

Read more
  • AWS
  • Cloud skills
  • Google Cloud
  • Microsoft Azure
Avatar
Andrew Larkin
— August 7, 2019

Disadvantages of Cloud Computing

If you want to deliver digital services of any kind, you’ll need to estimate all types of resources, not the least of which are CPU, memory, storage, and network connectivity. Which resources you choose for your delivery —  cloud-based or local — is up to you. But you’ll definitely want...

Read more
  • AWS
  • Azure
  • Cloud Computing
  • Google Cloud Platform
Joe Nemer
Joe Nemer
— August 6, 2019

Google Cloud vs AWS: A Comparison (or can they be compared?)

The "Google Cloud vs AWS" argument used to be a common discussion among our members, but is this still really a thing? You may already know that there are three major players in the public cloud platforms arena: Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP)...

Read more
  • AWS
  • Google Cloud Platform
  • Kubernetes
Avatar
Stuart Scott
— July 29, 2019

Deployment Orchestration with AWS Elastic Beanstalk

If you're responsible for the development and deployment of web applications within your AWS environment for your organization, then it's likely you've heard of AWS Elastic Beanstalk. If you are new to this service, or simply need to know a bit more about the service and the benefits th...

Read more
  • AWS
  • elastic beanstalk
Avatar
Stuart Scott
— July 26, 2019

How to Use & Install the AWS CLI

What is the AWS CLI? | The AWS Command Line Interface (CLI) is for managing your AWS services from a terminal session on your own client, allowing you to control and configure multiple AWS services and implement a level of automation. If you’ve been using AWS for some time and feel...

Read more
  • AWS
  • AWS CLI
  • Command line interface
Alisha Reyes
Alisha Reyes
— July 22, 2019

Cloud Academy’s Blog Digest: July 2019

July has been a very exciting month for us at Cloud Academy. On July 10, we officially joined forces with QA, the UK’s largest B2B skills provider (read the announcement). Over the coming weeks, you will see additions from QA’s massive catalog of 500+ certification courses and 1500+ ins...

Read more
  • AWS
  • Azure
  • Cloud Academy
  • Cybersecurity
  • DevOps
  • Kubernetes
Avatar
Stuart Scott
— July 18, 2019

AWS Fundamentals: Understanding Compute, Storage, Database, Networking & Security

If you are just starting out on your journey toward mastering AWS cloud computing, then your first stop should be to understand the AWS fundamentals. This will enable you to get a solid foundation to then expand your knowledge across the entire AWS service catalog.   It can be both d...

Read more
  • AWS
  • Compute
  • Database
  • fundamentals
  • networking
  • Security
  • Storage
Avatar
Adam Hawkins
— July 17, 2019

How to Become a DevOps Engineer

The DevOps Handbook introduces DevOps as a framework for improving the process for converting a business hypothesis into a technology-enabled service that delivers value to the customer. This process is called the value stream. Accelerate finds that applying DevOps principles of flow, f...

Read more
  • AWS
  • AWS Certifications
  • DevOps
  • DevOps Foundation Certification
  • Engineer
  • Kubernetes
Avatar
Vineet Badola
— July 15, 2019

AWS AMI Virtualization Types: HVM vs PV (Paravirtual VS Hardware VM)

Amazon Machine Images (AWS AMI) offers two types of virtualization: Paravirtual (PV) and Hardware Virtual Machine (HVM). Each solution offers its own advantages. When we’re using AWS, it’s easy for someone — almost without thinking —  to choose which AMI flavor seems best when spinning...

Read more
  • AWS
  • Hardware Virtual Machine
  • Paravirtual
  • Virtualization