Skip to content

Instantly share code, notes, and snippets.

@sorrison
Created March 12, 2015 00:15
Show Gist options
  • Save sorrison/99660f1f447abb9bdf77 to your computer and use it in GitHub Desktop.
Save sorrison/99660f1f447abb9bdf77 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Built in HAProxy server'
Outputs:
PublicIp:
Description: 'instance IP'
Value:
Fn::GetAtt:
- 'LB_instance'
- 'PublicIp'
Parameters:
KeyName:
Type: 'String'
Resources:
CfnLBAccessKey:
Properties:
UserName:
Ref: 'CfnLBUser'
Type: 'AWS::IAM::AccessKey'
CfnLBUser:
Type: 'AWS::IAM::User'
LB_instance:
Metadata:
AWS::CloudFormation::Init:
config:
files:
/etc/cfn/cfn-credentials:
content:
Fn::Join:
- ''
- - 'AWSAccessKeyId='
- Ref: 'CfnLBAccessKey'
- '
'
- 'AWSSecretKey='
- Fn::GetAtt:
- 'CfnLBAccessKey'
- 'SecretAccessKey'
- '
'
group: 'root'
mode: '000400'
owner: 'root'
/etc/cfn/cfn-hup.conf:
content:
Fn::Join:
- ''
- - '[main]
'
- 'stack='
- Ref: 'AWS::StackId'
- '
'
- 'credential-file=/etc/cfn/cfn-credentials
'
- 'region='
- Ref: 'AWS::Region'
- '
'
- 'interval=60
'
group: 'root'
mode: '000400'
owner: 'root'
/etc/cfn/hooks.conf:
content:
Fn::Join:
- ''
- - '[cfn-init]
'
- 'triggers=post.update
'
- 'path=Resources.LB_instance.Metadata
'
- 'action=/opt/aws/bin/cfn-init -s '
- Ref: 'AWS::StackId'
- ' -r LB_instance '
- ' --region '
- Ref: 'AWS::Region'
- '
'
- 'runas=root
'
- '
'
- '[reload]
'
- 'triggers=post.update
'
- 'path=Resources.LB_instance.Metadata
'
- 'action=reload haproxy
'
- 'runas=root
'
group: 'root'
mode: '000400'
owner: 'root'
/etc/haproxy/haproxy.cfg:
content: ''
group: 'root'
mode: '000644'
owner: 'root'
/tmp/cfn-hup-crontab.txt:
content:
Fn::Join:
- ''
- - 'MAIL=""
'
- '
'
- '* * * * * /opt/aws/bin/cfn-hup -f
'
- '* * * * * /opt/aws/bin/cfn-push-stats '
- ' --watch '
- Ref: 'latency_watcher'
- ' --haproxy
'
group: 'root'
mode: '000600'
owner: 'root'
packages:
debian:
haproxy: []
python-psutil: []
socat: []
Properties:
ImageId: 'a84903e8-1b4e-45e8-ad4a-886fae9393a3'
InstanceType: 'm1.small'
KeyName:
Ref: 'KeyName'
UserData:
Fn::Base64:
Fn::Join:
- ''
- - '#!/bin/bash -v
'
- '# Helper function
'
- 'function error_exit
'
- '{
'
- ' /opt/aws/bin/cfn-signal -e 1 -r "$1" '''
- Ref: 'WaitHandle'
- '''
'
- ' exit 1
'
- '}
'
- '/opt/aws/bin/cfn-init -s '
- Ref: 'AWS::StackId'
- ' -r LB_instance '
- ' --region '
- Ref: 'AWS::Region'
- '
'
- '# install cfn-hup crontab
'
- 'crontab /tmp/cfn-hup-crontab.txt
'
- '# LB setup completed, signal success
'
- '/opt/aws/bin/cfn-signal -e 0 -r "LB server setup complete"
'''
- Ref: 'WaitHandle'
- '''
'
Type: 'AWS::EC2::Instance'
WaitCondition:
DependsOn: 'LB_instance'
Properties:
Handle:
Ref: 'WaitHandle'
Timeout: '600'
Type: 'AWS::CloudFormation::WaitCondition'
WaitHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'
latency_watcher:
Properties:
AlarmActions: []
ComparisonOperator: 'GreaterThanThreshold'
EvaluationPeriods: '1'
MetricName: 'Latency'
Namespace: 'AWS/ELB'
Period: '60'
Statistic: 'Average'
Threshold: '2'
Type: 'AWS::CloudWatch::Alarm'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment