Created
September 6, 2016 11:20
-
-
Save ned1313/7d5db23a59a0032e1ae0c9b89275e9c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Description" : "Template for the creation of an AutoScaling group with Domain Join", | |
"Metadata": { | |
"AWS::CloudFormation::Interface": { | |
"ParameterGroups": [ | |
{ | |
"Label": { | |
"default": "Active Directory Information" | |
}, | |
"Parameters": [ | |
"DomainDNSName", | |
"DomainNetBIOSName", | |
"DomainAdminUser", | |
"DomainAdminPassword", | |
"ADServer1PrivateIP", | |
"ADServer2PrivateIP" | |
] | |
}, | |
{ | |
"Label": { | |
"default": "Server Configuration" | |
}, | |
"Parameters": [ | |
"ServerInstanceType", | |
"ServerPrefix", | |
"AutoScaleSubnetId", | |
"SecurityGroup", | |
"KeyPairName", | |
"BucketName" | |
] | |
} | |
], | |
"ParameterLabels": { | |
"ServerInstanceType": { | |
"default": "Server Instance Type" | |
}, | |
"ADServer1PrivateIP": { | |
"default": "Domain Controller 1 Private IP Address" | |
}, | |
"ADServer2PrivateIP": { | |
"default": "Domain Controller 2 Private IP Address" | |
}, | |
"DomainAdminPassword": { | |
"default": "Domain Admin Password" | |
}, | |
"DomainAdminUser": { | |
"default": "Domain Admin User Name" | |
}, | |
"DomainDNSName": { | |
"default": "Domain DNS Name" | |
}, | |
"DomainNetBIOSName": { | |
"default": "Domain NetBIOS Name" | |
}, | |
"KeyPairName": { | |
"default": "Key Pair Name" | |
}, | |
"AutoScaleSubnetId": { | |
"default": "Subnet for Servers" | |
}, | |
"SecurityGroup": { | |
"default": "Security Group for Servers" | |
}, | |
"ServerPrefix": { | |
"default": "Server Naming Prefix" | |
}, | |
"BucketName": { | |
"default": "S3 Bucket with Scripts" | |
} | |
} | |
} | |
}, | |
"Parameters" : { | |
"ServerInstanceType": { | |
"AllowedValues": [ | |
"t2.large", | |
"m3.large", | |
"m3.xlarge", | |
"m3.2xlarge", | |
"m4.large", | |
"m4.xlarge", | |
"m4.2xlarge", | |
"m4.4xlarge" | |
], | |
"Default": "m3.large", | |
"Description": "Amazon EC2 instance type for the server instance", | |
"Type": "String" | |
}, | |
"AutoScaleSubnetId" : { | |
"Description" : "ID of the process server subnet", | |
"Type" : "AWS::EC2::Subnet::Id" | |
}, | |
"KeyPairName": { | |
"Description": "Public/private key pairs allow you to securely connect to your instance after it launches", | |
"Type": "AWS::EC2::KeyPair::KeyName" | |
}, | |
"DomainAdminUserName" : { | |
"AllowedPattern": "[a-zA-Z0-9]*", | |
"Default": "StackAdmin", | |
"Description": "User name for the account that will be added as Domain Administrator. This is separate from the default \"Administrator\" account", | |
"MaxLength": "25", | |
"MinLength": "5", | |
"Type": "String" | |
}, | |
"DomainAdminPassword": { | |
"AllowedPattern": "(?=^.{6,255}$)((?=.*\\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*", | |
"Description": "Password for the domain admin user. Must be at least 8 characters containing letters, numbers and symbols", | |
"MaxLength": "32", | |
"MinLength": "8", | |
"NoEcho": "true", | |
"Type": "String" | |
}, | |
"DomainDNSName": { | |
"AllowedPattern": "[a-zA-Z0-9]+\\..+", | |
"Default": "example.com", | |
"Description": "Fully qualified domain name (FQDN) of the forest root domain e.g. example.com", | |
"MaxLength": "25", | |
"MinLength": "3", | |
"Type": "String" | |
}, | |
"DomainNetBIOSName": { | |
"AllowedPattern": "[a-zA-Z0-9]+", | |
"Default": "example", | |
"Description": "NetBIOS name of the domain (up to 15 characters)", | |
"MaxLength": "15", | |
"MinLength": "1", | |
"Type": "String" | |
}, | |
"SecurityGroup" : { | |
"Description" : "Security Group for AutoScale Servers", | |
"Type" : "AWS::EC2::SecurityGroup::Id" | |
}, | |
"ServerPrefix": { | |
"Description": "Prefix for Servers in AutoScale Group", | |
"Type": "String", | |
"MinLength" : "3", | |
"MaxLength" : "12", | |
"Default": "WebServer", | |
"AllowedPattern" : "[a-zA-Z]{12}" | |
}, | |
"ADServer1PrivateIP": { | |
"AllowedPattern": "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", | |
"Description": "Fixed private IP of first domain controller", | |
"Type": "String" | |
}, | |
"ADServer2PrivateIP": { | |
"AllowedPattern": "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", | |
"Description": "Fixed private IP of second domain controller", | |
"Type": "String" | |
}, | |
"BucketName" : { | |
"Description" : "Name of bucket containing scripts", | |
"Type" : "String" | |
} | |
}, | |
"Mappings": { | |
"AWSAMIRegionMap": { | |
"AMI": { | |
"WS2012R2": "Windows_Server-2012-R2_RTM-English-64Bit-Base-2016.07.26" | |
}, | |
"us-east-1": { | |
"WS2012R2": "ami-74a73263" | |
}, | |
"us-west-1": { | |
"WS2012R2": "ami-6d0b4b0d" | |
}, | |
"us-west-2": { | |
"WS2012R2": "ami-2426e944" | |
} | |
} | |
}, | |
"Resources": { | |
"CfnUser" : { | |
"Type" : "AWS::IAM::User", | |
"Properties" : { | |
"Path": "/", | |
"Policies": [{ | |
"PolicyName": "CfnPolicy", | |
"PolicyDocument": { "Statement":[{ | |
"Effect" : "Allow", | |
"Action" : [ | |
"cloudformation:DescribeStackResource", | |
"s3:GetObject" | |
], | |
"Resource" :"*" | |
}]} | |
}] | |
} | |
}, | |
"CfnKeys" : { | |
"Type" : "AWS::IAM::AccessKey", | |
"Properties" : { | |
"UserName" : {"Ref": "CfnUser"} | |
} | |
}, | |
"MyServerAutoScalingGroup": { | |
"Type": "AWS::AutoScaling::AutoScalingGroup", | |
"Properties": { | |
"LaunchConfigurationName": { | |
"Ref": "MyServerLaunchConfiguration" | |
}, | |
"DesiredCapacity" : "2", | |
"MaxSize" : "5", | |
"MinSize" : "1", | |
"VPCZoneIdentifier" : [{"Ref" : "AutoScaleSubnetId"}] | |
} | |
}, | |
"MyServerLaunchConfiguration": { | |
"Type": "AWS::AutoScaling::LaunchConfiguration", | |
"Metadata" : { | |
"AWS::CloudFormation::Init": { | |
"configSets": { | |
"config": [ | |
"setup", | |
"rename", | |
"join" | |
] | |
}, | |
"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.MyServerLaunchConfiguration.Metadata.AWS::CloudFormation::Init\n", | |
"action=cfn-init.exe -v -c config -s ", | |
{ | |
"Ref": "AWS::StackId" | |
}, | |
" -r MyServerLaunchConfiguration", | |
" --region ", | |
{ | |
"Ref": "AWS::Region" | |
}, | |
"\n" | |
] | |
] | |
} | |
}, | |
"c:\\cfn\\scripts\\Rename-LCComputer.ps1" : { | |
"source" : { "Fn::Join" : ["", [ | |
"https://", { "Ref" : "BucketName" }, | |
".s3.amazonaws.com/ps-scripts/Rename-LCComputer.ps1" | |
]]} | |
} | |
}, | |
"services": { | |
"windows": { | |
"cfn-hup": { | |
"enabled": "true", | |
"ensureRunning": "true", | |
"files": [ | |
"c:\\cfn\\cfn-hup.conf", | |
"c:\\cfn\\hooks.d\\cfn-auto-reloader.conf" | |
] | |
} | |
} | |
}, | |
"commands": { | |
"a-set-execution-policy": { | |
"command": "powershell.exe -command Set-ExecutionPolicy RemoteSigned -Force", | |
"waitAfterCompletion": "0" | |
} | |
} | |
}, | |
"rename": { | |
"commands": { | |
"a-execute-powershell-script-RenameComputer": { | |
"command": { | |
"Fn::Join" : ["", [ | |
"powershell.exe -executionpolicy unrestricted -command ", | |
"c:\\cfn\\scripts\\Rename-LCComputer.ps1 -ServerPrefix ", | |
{"Ref" : "ServerPrefix"}, | |
" -DCIPAddress \"", | |
{ "Ref" : "ADServer1PrivateIP" | |
}, | |
"\" -credentials ", | |
"(New-Object System.Management.Automation.PSCredential('", | |
{ | |
"Ref": "DomainNetBIOSName" | |
}, | |
"\\", | |
{ | |
"Ref" : "DomainAdminUserName" | |
}, | |
"',", | |
"(ConvertTo-SecureString '", | |
{ | |
"Ref": "DomainAdminPassword" | |
}, | |
"' -AsPlainText -Force)))" | |
]] | |
}, | |
"waitAfterCompletion": "forever" | |
} | |
} | |
}, | |
"join": { | |
"commands": { | |
"a-change-dns-servers" : { | |
"command" : { | |
"Fn::Join" : [ | |
"", | |
[ | |
"powershell.exe -Command \"", | |
"Get-NetAdapter | Set-DnsClientServerAddress -ServerAddresses ", | |
{ | |
"Ref" : "ADServer1PrivateIP" | |
}, | |
",", | |
{ | |
"Ref" : "ADServer2PrivateIP" | |
}, | |
"\"" | |
] | |
] | |
}, | |
"waitAfterCompletion": "30" | |
}, | |
"b-join-domain": { | |
"command": { | |
"Fn::Join": [ | |
"", | |
[ | |
"powershell.exe -Command \"", | |
"Add-Computer -DomainName ", | |
{ | |
"Ref" : "DomainDNSName" | |
}, | |
" -Credential ", | |
"(New-Object System.Management.Automation.PSCredential('", | |
{ | |
"Ref": "ClientName" | |
}, | |
"\\", | |
{ | |
"Ref" : "DomainAdminUserName" | |
}, | |
"',", | |
"(ConvertTo-SecureString '", | |
{ | |
"Ref": "DomainAdminPassword" | |
}, | |
"' -AsPlainText -Force))) -Restart" | |
] | |
] | |
}, | |
"waitAfterCompletion": "forever" | |
} | |
} | |
} | |
}, | |
"AWS::CloudFormation::Authentication" : { | |
"S3AccessCreds" : { | |
"type" : "S3", | |
"accessKeyId" : { "Ref" : "CfnKeys" }, | |
"secretKey" : {"Fn::GetAtt" : ["CfnKeys", "SecretAccessKey"]}, | |
"buckets" : [ { "Ref" : "BucketName" } ] | |
} | |
} | |
}, | |
"Properties": { | |
"BlockDeviceMappings" : [ | |
{ | |
"DeviceName" : "/dev/sda1", | |
"Ebs" : { | |
"VolumeSize" : "250", | |
"VolumeType" : "gp2" | |
} | |
} | |
], | |
"ImageId": { | |
"Fn::FindInMap": [ | |
"AWSAMIRegionMap", | |
{ | |
"Ref": "AWS::Region" | |
}, | |
"WS2012R2" | |
] | |
}, | |
"InstanceType" : { | |
"Ref" : "ServerInstanceType" | |
}, | |
"KeyName": { | |
"Ref": "KeyPairName" | |
}, | |
"SecurityGroups" : [ | |
{"Ref" : "SecurityGroup"} | |
], | |
"UserData" : { | |
"Fn::Base64": { | |
"Fn::Join": [ | |
"", | |
[ | |
"<script>\n", | |
"cfn-init.exe -v -c config -s ", | |
{ | |
"Ref": "AWS::StackId" | |
}, | |
" -r MyServerLaunchConfiguration", | |
" --region ", | |
{ | |
"Ref": "AWS::Region" | |
}, | |
"\n", | |
"</script>\n" | |
] | |
] | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment