{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "Runs a VNC server fleet that binds instances to domain names.", "Parameters" : { "KeyName" : { "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances", "Type" : "String" }, "InstanceType" : { "Description" : "VNC host instance type", "Type" : "String", "Default" : "t1.micro", "AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","m3.xlarge","m3.2xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge"], "ConstraintDescription" : "must be a valid EC2 instance type." }, "MinSize" : { "Description" : "Minimum number of VNC hosts", "Type" : "Number", "MinValue" : "1", "Default" : "1", "ConstraintDescription" : "Must have at least one host" }, "MaxSize" : { "Description" : "Maximum number of VNC hosts", "Type" : "Number", "MinValue" : "1", "Default" : "1", "ConstraintDescription" : "Must have at least one host" }, "SSHLocation" : { "Description" : "The IP address range that can be used to SSH into hosts", "Type": "String", "MinLength": "9", "MaxLength": "18", "Default": "0.0.0.0/0", "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})", "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x." }, "HostedZoneId" : { "Description" : "The Hosted Zone Id to use when assigning DNS mappings", "Type" : "String" }, "DNSProcessorServiceToken" : { "Description" : "ServiceToken of DNS Processor Custom Resource", "Type" : "String", "AllowedPattern" : "arn:aws:sns:.*", "ConstraintDescription" : "must be an SNS topic ARN" } }, "Mappings" : { "AwsRegionToAMI" : { "ap-northeast-1" : { "id" : "ami-6dea746c" }, "ap-southeast-1" : { "id" : "ami-141d5746" }, "ap-southeast-2" : { "id" : "ami-f377eac9" }, "eu-west-1" : { "id" : "ami-5e03e429" }, "us-east-1" : { "id" : "ami-ad83d7c4" }, "us-west-1" : { "id" : "ami-7e37033b" }, "us-west-2" : { "id" : "ami-12fe6022" }, "sa-east-1" : { "id" : "ami-ed47e0f0" } } }, "Resources" : { "DNSProcessor" : { "Type" : "Custom::DNSProcessor", "Version" : "1.0", "Properties" : { "ServiceToken" : { "Ref" : "DNSProcessorServiceToken" }, "DNSPattern" : { "Fn::Join" : [".", ["{{simpsons_name}}", { "Ref" : "AWS::Region" }, "{{hosted_zone_name}}"]] }, "HostedZoneId" : { "Ref" : "HostedZoneId" } } }, "VncLaunchConfig" : { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Properties" : { "ImageId" : { "Fn::FindInMap" : ["AwsRegionToAMI", { "Ref" : "AWS::Region" }, "id"] }, "InstanceType" : { "Ref" : "InstanceType" }, "KeyName" : { "Ref" : "KeyName" }, "SecurityGroups" : [ { "Ref" : "VncSecurityGroup" } ], "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "#!/bin/bash -x\n", "exec &> /home/ubuntu/userdata.log\n", "function error_exit\n", "{\n", " cfn-signal -e 1 -r \"$1\" '", { "Ref" : "VncWaitConditionHandle" }, "'\n", " exit 1\n", "}\n", "apt-get update || error_exit 'Failed to update package repository'\n", "apt-get -y install python-pip || error_exit 'Failed to install python-pip'\n", "pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n", "cfn-init --region ", { "Ref" : "AWS::Region" }, " -s ", { "Ref" : "AWS::StackName" }, " -r VncLaunchConfig || error_exit 'Failed to run cfn-init'\n", "/etc/init.d/vncserver start || error_exit 'Failed to start vnc server'\n", "cfn-signal -e $? ", { "Fn::Base64" : { "Ref" : "VncWaitConditionHandle" }}, "\n" ]] } } }, "Metadata" : { "AWS::CloudFormation::Init" : { "config" : { "packages" : { "apt" : { "tightvncserver" : [], "fluxbox" : [] } }, "files" : { "/etc/init.d/vncserver" : { "content" : { "Fn::Join" : ["\n", [ "#!/bin/sh -e", "### BEGIN INIT INFO", "# Provides: vncserver", "# Required-Start: networking", "# Default-Start: S", "# Default-Stop: 0 6", "### END INIT INFO", "export USER=\"ubuntu\"", "OPTIONS=\"-depth 16 -geometry 1024x768 :1\"", ". /lib/lsb/init-functions", "case \"$1\" in", "start)", "su ${USER} -c \"/usr/bin/vncserver ${OPTIONS}\"", ";;", "stop)", "su ${USER} -c \"/usr/bin/vncserver -kill :1\"", ";;", "restart)", "su ${USER} -c \"/usr/bin/vncserver -kill :1\"", "su ${USER} -c \"/usr/bin/vncserver ${OPTIONS}\"", ";;", "esac" ]]}, "owner" : "root", "group" : "root", "mode" : "000755" }, "/home/ubuntu/.vnc/passwd" : { "content" : "X6670O8KJBM=", "encoding" : "base64", "mode" : "000600", "owner" : "ubuntu", "group" : "ubuntu" }, "/home/ubuntu/.vnc/xstartup" : { "content" : { "Fn::Join" : ["\n", [ "#!/bin/sh", "xrdb $HOME/.Xresources", "xsetroot -solid grey", "/usr/bin/startfluxbox &", "# Fix to make GNOME work", "export XKL_XMODMAP_DISABLE=1", "/etc/X11/Xsession" ]]}, "mode" : "000755", "owner" : "ubuntu", "group" : "ubuntu" } }, "commands" : { "01-start-vnc-on-boot": { "command" : "update-rc.d vncserver defaults" }, "02-change-ownership-of-.vnc": { "command" : "chown ubuntu:ubuntu /home/ubuntu/.vnc" }, "03-set-permissions-of-.vnc": { "command" : "chmod 700 /home/ubuntu/.vnc" } } } } } }, "VncAutoScalingGroup" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : { "Fn::GetAZs" : ""}, "LaunchConfigurationName" : { "Ref" : "VncLaunchConfig" }, "MinSize" : { "Ref" : "MinSize" }, "MaxSize" : { "Ref" : "MaxSize" }, "NotificationConfiguration" : { "TopicARN" : { "Fn::GetAtt" : ["DNSProcessor", "Topic"] }, "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH","autoscaling:EC2_INSTANCE_TERMINATE"] }, "Tags" : [ { "Key" : "ProcessorId", "Value" : { "Ref" : "DNSProcessor" }, "PropagateAtLaunch" : false } ] } }, "VncSecurityGroup" : { "Type" : "AWS::EC2::SecurityGroup", "Properties" : { "GroupDescription" : "SSH to the vnc instances", "SecurityGroupIngress" : [ { "CidrIp" : { "Ref" : "SSHLocation" }, "FromPort" : "22", "ToPort" : "22", "IpProtocol" : "tcp" } ] } }, "VncWaitConditionHandle" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" }, "VncWaitCondition" : { "Type" : "AWS::CloudFormation::WaitCondition", "DependsOn" : "VncAutoScalingGroup", "Properties" : { "Count" : { "Ref" : "MinSize"}, "Handle" : { "Ref" : "VncWaitConditionHandle" }, "Timeout" : "600" } } }, "Outputs" : { "SecurityGroup" : { "Description" : "Security group of the vnc hosts", "Value" : { "Ref" : "VncSecurityGroup" } } } }