<% @app,@role,@env = name.sub('.yml','').split('-') -%> --- AWSTemplateFormatVersion: '2010-09-09' Description: <%= @app.capitalize %> Stack Outputs: ELBHostname: Description: The URL of the website Value: Fn::Join: - '' - - http:// - Fn::GetAtt: - elb - DNSName Parameters: Ami: Default: <%= @ami %> Description: Ami id Type: String Application: Default: <%= @app %> Description: Application name Type: String Environment: Default: <%= @env %> Description: stag, prod etc Type: String InstanceType: AllowedValues: - t1.micro - m1.small - m1.medium - m1.large - m1.xlarge - m2.xlarge - m2.2xlarge - m2.4xlarge - c1.medium - c1.xlarge - cc1.4xlarge - cc2.8xlarge - cg1.4xlarge - t2.nano - t2.micro - t2.small - t2.medium - t2.large - t2.xlarge - t2.2xlarge - m4.large - m4.xlarge - m4.2xlarge - m4.4xlarge - m4.10xlarge - m4.16xlarge ConstraintDescription: must be a valid EC2 instance type. Default: <%= @instance_type %> Description: WebServer EC2 instance type Type: String KeyName: Default: default Description: The EC2 Key Pair to allow SSH access to the instances Type: String Role: Default: <%= @role %> Description: redis, psql, app, etc Type: String WebServerPort: Default: '<%= @port %>' Description: The TCP port for the Web Server Type: Number Resources: CPUAlarmHigh: Properties: AlarmActions: - Ref: WebServerScaleUpPolicy AlarmDescription: Scale-up if CPU > <%= @high_threshold %>% for <%= @high_mins %> ComparisonOperator: GreaterThanThreshold Dimensions: - Name: AutoScalingGroupName Value: Ref: WebServerGroup EvaluationPeriods: '<%= @high_periods %>' MetricName: CPUUtilization Namespace: AWS/EC2 Period: '60' Statistic: Average Threshold: '<%= @high_threshold %>' Type: AWS::CloudWatch::Alarm CPUAlarmLow: Properties: AlarmActions: - Ref: WebServerScaleDownPolicy AlarmDescription: Scale-down if CPU < <%= @low_threshold %>% for 10 minutes ComparisonOperator: LessThanThreshold Dimensions: - Name: AutoScalingGroupName Value: Ref: WebServerGroup EvaluationPeriods: '<%= @low_periods %>' MetricName: CPUUtilization Namespace: AWS/EC2 Period: '60' Statistic: Average Threshold: '<%= @low_threshold %>' Type: AWS::CloudWatch::Alarm <%= partial("host_record.yml.erb", domain: "mydomain.net") %> LaunchConfig: Properties: BlockDeviceMappings: - DeviceName: "/dev/sdb" VirtualName: ephemeral0 ImageId: !Ref Ami InstanceType: !Ref InstanceType KeyName: Ref: KeyName SecurityGroups: - global - Fn::Join: - "-" - - Ref: Application - Ref: Environment - Ref: ServiceSecurityGroup UserData: Fn::Base64: !Sub | # No more Fn::Join needed <%= partial("user_data/bootstrap.sh.erb", {}, indent: 10) %> Type: AWS::AutoScaling::LaunchConfiguration ServiceSecurityGroup: Properties: GroupDescription: Enable SSH access and HTTP from the load balancer only SecurityGroupIngress: - CidrIp: 0.0.0.0/0 FromPort: '22' IpProtocol: tcp ToPort: '22' - FromPort: Ref: WebServerPort IpProtocol: tcp SourceSecurityGroupName: Fn::GetAtt: - elb - SourceSecurityGroup.GroupName SourceSecurityGroupOwnerId: Fn::GetAtt: - elb - SourceSecurityGroup.OwnerAlias ToPort: Ref: WebServerPort Type: AWS::EC2::SecurityGroup WebServerGroup: Properties: AvailabilityZones: !GetAZs "" HealthCheckGracePeriod: '300' HealthCheckType: ELB LaunchConfigurationName: Ref: LaunchConfig LoadBalancerNames: - Ref: elb MaxSize: '<%= @max_size %>' MinSize: '<%= @min_size %>' Type: AWS::AutoScaling::AutoScalingGroup WebServerScaleDownPolicy: Properties: AdjustmentType: ChangeInCapacity AutoScalingGroupName: Ref: WebServerGroup Cooldown: '120' ScalingAdjustment: "<%= @down_adjustment %>" Type: AWS::AutoScaling::ScalingPolicy WebServerScaleUpPolicy: Properties: AdjustmentType: ChangeInCapacity AutoScalingGroupName: Ref: WebServerGroup Cooldown: '120' ScalingAdjustment: '<%= @up_adjustment %>' Type: AWS::AutoScaling::ScalingPolicy elb: Properties: AvailabilityZones: !GetAZs "" HealthCheck: HealthyThreshold: '3' Interval: '6' Target: Fn::Join: - '' - - 'HTTP:' - Ref: WebServerPort - "/up/elb" Timeout: '5' UnhealthyThreshold: '5' Listeners: - InstancePort: Ref: WebServerPort LoadBalancerPort: '80' Protocol: HTTP <% if @ssl_cert %> - InstancePort: Ref: WebServerPort LoadBalancerPort: '443' PolicyNames: [] Protocol: HTTPS SSLCertificateId: '<%= @ssl_cert %>' <% end %> Type: AWS::ElasticLoadBalancing::LoadBalancer