{
	"AWSTemplateFormatVersion": "2010-09-09",
	"Description": "Hello World",
	"Parameters": {
		"Prefix": {
			"Type": "String",
			"Description": "Prefix of the domain name.  ie: test -> test-<subdomain>.<domain>.com"
		},

		"Subdomain": {
			"Type": "String",
			"Default": "hello",
			"Description": "Subdomain.  ie: hello -> <prefix>-hello.<domain>.com"
		},

		"HostedZoneName": {
			"Type": "String",
			"Description": "Domain name, ie: doapps.com - nameservers must be pointing to AWS: http://aws.amazon.com/route53/"
		},

		"AMI": {
			"Type": "String",
			"Default": "ami-98aa1cf0",
			"Description": "Amazon Machine Image - http://cloud-images.ubuntu.com/locator/ec2/"
		},

		"KeyName": {
			"Type": "String",
			"Description": "Name of key pair"
		},

		"UserData": {
			"Type": "String",
			"Description": "The user data available to the launched EC2 instances. Base64 encoded"
		},

		"InstanceType": {
			"Type": "String",
			"Default": "t1.micro",
			"Description": "The instance type: http://www.ec2instances.info/"
		},

		"MinSize": {
			"Type": "String",
			"Default": "1",
			"Description": "The minimum size of the autoscale group"
		},

		"MaxSize": {
			"Type": "String",
			"Default": "1",
			"Description": "The maximum size of the autoscale group"
		}
	},

	"Resources": {
		"ELB": {
			"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
			"Properties": {
				"ConnectionDrainingPolicy": {
					"Enabled": true,
					"Timeout": 5
				},

				"CrossZone": true,
				"AvailabilityZones": [
					"us-east-1a",
					"us-east-1b"
				],

				"HealthCheck": {
					"HealthyThreshold": "2",
					"Interval": "30",
					"Target": "HTTP:81/elb_status.html",
					"Timeout": "5",
					"UnhealthyThreshold": "4"
				},

				"Listeners": [
					{
						"InstancePort": "80",
						"LoadBalancerPort": "80",
						"Protocol": "HTTP",
						"PolicyNames": [
							
						]
					}
				]
			}
		},

		"DNS": {
			"Type": "AWS::Route53::RecordSet",
			"Properties": {
				"Type": "A",
				"HostedZoneName": {
					"Fn::Join": [
						"",
						[
							{
								"Ref": "HostedZoneName"
							},

							"."
						]
					]
				},

				"AliasTarget": {
					"DNSName": {
						"Fn::GetAtt": [
							"ELB",
							"DNSName"
						]
					},

					"EvaluateTargetHealth": true,
					"HostedZoneId": {
						"Fn::GetAtt": [
							"ELB",
							"CanonicalHostedZoneNameID"
						]
					}
				},

				"Name": {
					"Fn::Join": [
						"-",
						[
							{
								"Ref": "Prefix"
							},

							{
								"Fn::Join": [
									".",
									[
										{
											"Ref": "Subdomain"
										},

										{
											"Ref": "HostedZoneName"
										}
									]
								]
							}
						]
					]
				}
			}
		},

		"Role": {
			"Type": "AWS::IAM::Role",
			"Properties": {
				"AssumeRolePolicyDocument": {
					"Version": "2012-10-17",
					"Statement": [
						{
							"Effect": "Allow",
							"Principal": {
								"Service": [
									"ec2.amazonaws.com"
								]
							},

							"Action": [
								"sts:AssumeRole"
							]
						}
					]
				},

				"Path": "/",
				"Policies": [
					{
						"PolicyName": "DynamoPolicy",
						"PolicyDocument": {
							"Version": "2012-10-17",
							"Statement": [
								{
									"Effect": "Allow",
									"Action": [
										"*"
									],

									"Resource": [
										{
											"Fn::Join": [
												"",
												[
													"arn:aws:dynamodb:us-east-1:",
													{
														"Ref": "AWS::AccountId"
													},

													":table/",
													{
														"Ref": "Prefix"
													},

													"-*"
												]
											]
										}
									]
								}
							]
						}
					}
				]
			}
		},

		"Profile": {
			"Type": "AWS::IAM::InstanceProfile",
			"Properties": {
				"Path": "/",
				"Roles": [
					{
						"Ref": "Role"
					}
				]
			}
		},

		"SecurityGroup": {
			"Type": "AWS::EC2::SecurityGroup",
			"Properties": {
				"GroupDescription": {
					"Fn::Join": [
						" ",
						[
							{
								"Ref": "Prefix"
							},

							" Hello AWS"
						]
					]
				},

				"SecurityGroupIngress": [
					{
						"IpProtocol": "tcp",
						"FromPort": "22",
						"ToPort": "22",
						"CidrIp": "0.0.0.0/0"
					},

					{
						"IpProtocol": "tcp",
						"FromPort": "80",
						"ToPort": "81",
						"SourceSecurityGroupName": "amazon-elb-sg",
						"SourceSecurityGroupOwnerId": "amazon-elb"
					}
				]
			}
		},

		"LaunchConfig": {
			"Type": "AWS::AutoScaling::LaunchConfiguration",
			"Properties": {
				"ImageId": {
					"Ref": "AMI"
				},

				"InstanceType": {
					"Ref": "InstanceType"
				},

				"KeyName": {
					"Ref": "KeyName"
				},

				"UserData": {
					"Ref": "UserData"
				},

				"IamInstanceProfile": {
					"Ref": "Profile"
				},

				"SecurityGroups": [
					{
						"Ref": "SecurityGroup"
					}
				]
			}
		},

		"AutoScaleGroup": {
			"Type": "AWS::AutoScaling::AutoScalingGroup",
			"Properties": {
				"HealthCheckType": "EC2",
				"HealthCheckGracePeriod": "450",
				"AvailabilityZones": [
					"us-east-1a",
					"us-east-1b"
				],

				"Cooldown": "300",
				"DesiredCapacity": {
					"Ref": "MinSize"
				},

				"MaxSize": {
					"Ref": "MaxSize"
				},

				"MinSize": {
					"Ref": "MinSize"
				},

				"LaunchConfigurationName": {
					"Ref": "LaunchConfig"
				},

				"LoadBalancerNames": [
					{
						"Ref": "ELB"
					}
				]
			}
		},

		"Dynamo": {
			"Type": "AWS::DynamoDB::Table",
			"Properties": {
				"TableName": {
					"Fn::Join": [
						"-",
						[
							{
								"Ref": "Prefix"
							},

							"hello-aws"
						]
					]
				},

				"AttributeDefinitions": [
					{
						"AttributeName": "my_hash_key",
						"AttributeType": "S"
					},

					{
						"AttributeName": "my_range_key",
						"AttributeType": "S"
					}
				],

				"KeySchema": [
					{
						"AttributeName": "my_hash_key",
						"KeyType": "HASH"
					},

					{
						"AttributeName": "my_range_key",
						"KeyType": "RANGE"
					}
				],

				"ProvisionedThroughput": {
					"ReadCapacityUnits": "1",
					"WriteCapacityUnits": "1"
				}
			}
		}
	}
}