Permalink
Switch branches/tags
Nothing to show
Find file Copy path
b710a03 Oct 3, 2014
1 contributor

Users who have contributed to this file

219 lines (206 sloc) 5.78 KB
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Launch an EC2 Operator to perform auto-start and auto-stop instances for all regions.",
"Parameters": {
"InstanceType" : {
"Description" : "Instance type for EC2 Operator",
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : [ "t2.micro","t2.small","t2.medium" ],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"KeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "String",
"Default": "schen"
},
"SSHLocation" : {
"Description" : " The IP address range that can be used to SSH to the EC2 instance",
"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."
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-08842d60" },
"us-west-1" : { "AMI" : "ami-cfa8a18a" },
"us-west-2" : { "AMI" : "ami-8786c6b7" },
"eu-west-1" : { "AMI" : "ami-748e2903" },
"sa-east-1" : { "AMI" : "ami-956cc688" },
"ap-southeast-1" : { "AMI" : "ami-d6e1c584" },
"ap-southeast-2" : { "AMI" : "ami-fd4724c7" },
"ap-northeast-1" : { "AMI" : "ami-35072834" }
}
},
"Resources": {
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "EC2 Operator Admin Access",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": {
"Ref": "SSHLocation"
}
}
]
}
},
"OperatorInstance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"config" : {
"packages" : {
"yum" : {
"python26-pip" : [],
"gcc" : []
}
},
"files" : {
"/home/ec2-user/ec2_operator.py" : {
"source" : "https://raw2.github.com/schen1628/ec2/master/ec2_operator.py",
"mode" : "000644",
"owner" : "ec2-user",
"group" : "ec2-user"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionMap",
{
"Ref": "AWS::Region"
},
"AMI"
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"IamInstanceProfile": {
"Ref": "OperatorInstanceProfile"
},
"KeyName": {
"Ref": "KeyName"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -v\n",
"yum update -y aws-cfn-bootstrap\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"# Install python-pip, gcc and ec2 oeprator\n",
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r OperatorInstance ",
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"# Set up ec2 operator\n",
"pip install croniter\n",
"OPERATOR=/home/ec2-user/ec2_operator.py\n",
"echo \"*/5 * * * * ec2-user python $OPERATOR \" >> /etc/crontab\n",
"# If all is well so signal success\n",
"/opt/aws/bin/cfn-signal -e $? -r \"EC2 Operator setup complete\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "OperatorInstance",
"Properties": {
"Handle": {
"Ref": "WaitHandle"
},
"Timeout": "300"
}
},
"OperatorInstanceProfile": {
"Properties": {
"Path": "/",
"Roles": [
{
"Ref": "OperatorRole"
}
]
},
"Type": "AWS::IAM::InstanceProfile"
},
"OperatorRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com"
]
}
}
]
},
"Path": "/",
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"ec2:DescribeInstances",
"ec2:StartInstances",
"ec2:StopInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
},
"PolicyName": "EC2OperatorPolicy"
}
]
},
"Type": "AWS::IAM::Role"
}
},
"Outputs": {
"OperatorInstanceId": {
"Value": {
"Ref": "OperatorInstance"
},
"Description": "Instance Id of the ec2 operator instance"
}
}
}