134 lines
3.5 KiB
YAML
134 lines
3.5 KiB
YAML
heat_template_version: '2013-05-23'
|
|
description: AWS CloudFormation Sample Template
|
|
parameters:
|
|
InstanceType:
|
|
type: string
|
|
ImageId:
|
|
type: string
|
|
KeyName:
|
|
type: string
|
|
SecurityGroup:
|
|
type: string
|
|
resources:
|
|
CfnUser:
|
|
type: AWS::IAM::User
|
|
AKeys:
|
|
type: AWS::IAM::AccessKey
|
|
properties:
|
|
UserName: {Ref: CfnUser}
|
|
AGroup:
|
|
type: AWS::AutoScaling::AutoScalingGroup
|
|
properties:
|
|
AvailabilityZones: {'Fn::GetAZs': ""}
|
|
LaunchConfigurationName: {Ref: LaunchConfig}
|
|
MinSize: '1'
|
|
MaxSize: '2'
|
|
VPCZoneIdentifier: []
|
|
AScaleUpPolicy:
|
|
type: AWS::AutoScaling::ScalingPolicy
|
|
properties:
|
|
AdjustmentType: ChangeInCapacity
|
|
AutoScalingGroupName: {Ref: AGroup}
|
|
Cooldown: '60'
|
|
ScalingAdjustment: '1'
|
|
AScaleDownPolicy:
|
|
type: AWS::AutoScaling::ScalingPolicy
|
|
properties:
|
|
AdjustmentType: ChangeInCapacity
|
|
AutoScalingGroupName: {Ref: AGroup}
|
|
Cooldown: '60'
|
|
ScalingAdjustment: '-1'
|
|
CPUAlarmHigh:
|
|
type: OS::Heat::CWLiteAlarm
|
|
properties:
|
|
AlarmDescription: Scale-up if CPU > 50% for 1 minute
|
|
MetricName: CPUUtilization
|
|
Namespace: system/linux
|
|
Statistic: Average
|
|
Period: '60'
|
|
EvaluationPeriods: '1'
|
|
Threshold: '50'
|
|
AlarmActions:
|
|
- {Ref: AScaleUpPolicy}
|
|
Dimensions:
|
|
- Name: AutoScalingGroupName
|
|
Value: {Ref: AGroup}
|
|
ComparisonOperator: GreaterThanThreshold
|
|
CPUAlarmLow:
|
|
type: OS::Heat::CWLiteAlarm
|
|
properties:
|
|
AlarmDescription: Scale-down if CPU < 30% for 1 minute
|
|
MetricName: CPUUtilization
|
|
Namespace: system/linux
|
|
Statistic: Average
|
|
Period: '60'
|
|
EvaluationPeriods: '1'
|
|
Threshold: '30'
|
|
AlarmActions:
|
|
- {Ref: AScaleDownPolicy}
|
|
Dimensions:
|
|
- Name: AutoScalingGroupName
|
|
Value: {Ref: AGroup}
|
|
ComparisonOperator: LessThanThreshold
|
|
LaunchConfig:
|
|
type: AWS::AutoScaling::LaunchConfiguration
|
|
metadata:
|
|
AWS::CloudFormation::Init:
|
|
config:
|
|
files:
|
|
/etc/cfn/cfn-credentials:
|
|
content:
|
|
Fn::Join:
|
|
- ''
|
|
- - AWSAccessKeyId=
|
|
- {Ref: AKeys}
|
|
- '
|
|
|
|
'
|
|
- AWSSecretKey=
|
|
- get_attr: [AKeys, SecretAccessKey]
|
|
- '
|
|
|
|
'
|
|
mode: '000400'
|
|
owner: root
|
|
group: root
|
|
/tmp/stats-crontab.txt:
|
|
content:
|
|
Fn::Join:
|
|
- ''
|
|
- - 'MAIL=""
|
|
|
|
'
|
|
- '
|
|
|
|
'
|
|
- '* * * * * /opt/aws/bin/cfn-push-stats --watch '
|
|
- {Ref: CPUAlarmHigh}
|
|
- ' --cpu-util
|
|
|
|
'
|
|
- '* * * * * /opt/aws/bin/cfn-push-stats --watch '
|
|
- {Ref: CPUAlarmLow}
|
|
- ' --cpu-util
|
|
|
|
'
|
|
mode: '000600'
|
|
owner: root
|
|
group: root
|
|
properties:
|
|
ImageId: {Ref: ImageId}
|
|
InstanceType: {Ref: InstanceType}
|
|
KeyName: {Ref: KeyName}
|
|
SecurityGroups: [{Ref: SecurityGroup}]
|
|
UserData:
|
|
str_replace:
|
|
template: |
|
|
#!/bin/bash -v
|
|
/opt/aws/bin/cfn-init -s $stack_id -r LaunchConfig
|
|
# install crontab
|
|
crontab /tmp/stats-crontab.txt
|
|
touch /tmp/vm_ready.txt
|
|
params:
|
|
$stack_id: {get_param: "OS::stack_id"}
|