$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-auto-scalingProvides AWS CloudFormation patterns for Auto Scaling including EC2, ECS, and Lambda. Use when creating Auto Scaling groups, launch configurations, launch templates, scaling policies, lifecycle hooks, and predictive scaling. Covers template structure with Parameters, Outputs, Map
| 1 | # AWS CloudFormation Auto Scaling |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Create production-ready Auto Scaling infrastructure using AWS CloudFormation templates. This skill covers Auto Scaling Groups for EC2, ECS, and Lambda, launch configurations, launch templates, scaling policies, lifecycle hooks, and best practices for high availability and cost optimization. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | Use this skill when: |
| 10 | - Creating Auto Scaling Groups for EC2 instances |
| 11 | - Configuring Launch Configurations or Launch Templates |
| 12 | - Implementing scaling policies (step, target tracking, simple) |
| 13 | - Adding lifecycle hooks for lifecycle management |
| 14 | - Creating scaling for ECS services |
| 15 | - Implementing Lambda provisioned concurrency scaling |
| 16 | - Organizing templates with Parameters, Outputs, Mappings, Conditions |
| 17 | - Implementing cross-stack references with export/import |
| 18 | - Using mixed instances policies for diversity |
| 19 | |
| 20 | ## Instructions |
| 21 | |
| 22 | Follow these steps to create Auto Scaling infrastructure with CloudFormation: |
| 23 | |
| 24 | ### 1. Define Parameters |
| 25 | |
| 26 | Specify capacity and instance settings with AWS-specific parameter types: |
| 27 | |
| 28 | ```yaml |
| 29 | Parameters: |
| 30 | MinSize: |
| 31 | Type: Number |
| 32 | Default: 2 |
| 33 | Description: Minimum number of instances |
| 34 | |
| 35 | MaxSize: |
| 36 | Type: Number |
| 37 | Default: 10 |
| 38 | Description: Maximum number of instances |
| 39 | |
| 40 | DesiredCapacity: |
| 41 | Type: Number |
| 42 | Default: 2 |
| 43 | Description: Desired number of instances |
| 44 | |
| 45 | InstanceType: |
| 46 | Type: AWS::EC2::Instance::Type |
| 47 | Default: t3.micro |
| 48 | Description: EC2 instance type |
| 49 | |
| 50 | AmiId: |
| 51 | Type: AWS::EC2::Image::Id |
| 52 | Description: AMI ID for instances |
| 53 | |
| 54 | SubnetIds: |
| 55 | Type: List<AWS::EC2::Subnet::Id> |
| 56 | Description: Subnets for Auto Scaling group |
| 57 | ``` |
| 58 | |
| 59 | ### 2. Create Launch Configuration |
| 60 | |
| 61 | Define instance launch settings: |
| 62 | |
| 63 | ```yaml |
| 64 | Resources: |
| 65 | MyLaunchConfiguration: |
| 66 | Type: AWS::AutoScaling::LaunchConfiguration |
| 67 | Properties: |
| 68 | LaunchConfigurationName: !Sub "${AWS::StackName}-lc" |
| 69 | ImageId: !Ref AmiId |
| 70 | InstanceType: !Ref InstanceType |
| 71 | KeyName: !Ref KeyName |
| 72 | SecurityGroups: |
| 73 | - !Ref InstanceSecurityGroup |
| 74 | InstanceMonitoring: Enabled |
| 75 | UserData: |
| 76 | Fn::Base64: | |
| 77 | #!/bin/bash |
| 78 | yum update -y |
| 79 | yum install -y httpd |
| 80 | systemctl start httpd |
| 81 | ``` |
| 82 | |
| 83 | ### 3. Create Auto Scaling Group |
| 84 | |
| 85 | Specify min/max/desired capacity and networking: |
| 86 | |
| 87 | ```yaml |
| 88 | Resources: |
| 89 | MyAutoScalingGroup: |
| 90 | Type: AWS::AutoScaling::AutoScalingGroup |
| 91 | Properties: |
| 92 | AutoScalingGroupName: !Sub "${AWS::StackName}-asg" |
| 93 | MinSize: !Ref MinSize |
| 94 | MaxSize: !Ref MaxSize |
| 95 | DesiredCapacity: !Ref DesiredCapacity |
| 96 | VPCZoneIdentifier: !Ref SubnetIds |
| 97 | LaunchConfigurationName: !Ref MyLaunchConfiguration |
| 98 | TargetGroupARNs: |
| 99 | - !Ref MyTargetGroup |
| 100 | HealthCheckType: ELB |
| 101 | HealthCheckGracePeriod: 300 |
| 102 | Tags: |
| 103 | - Key: Environment |
| 104 | Value: !Ref Environment |
| 105 | PropagateAtLaunch: true |
| 106 | ``` |
| 107 | |
| 108 | ### 4. Configure Load Balancer Integration |
| 109 | |
| 110 | Set up ALB for traffic distribution: |
| 111 | |
| 112 | ```yaml |
| 113 | Resources: |
| 114 | MyTargetGroup: |
| 115 | Type: AWS::ElasticLoadBalancingV2::TargetGroup |
| 116 | Properties: |
| 117 | Name: !Sub "${AWS::StackName}-tg" |
| 118 | Port: 80 |
| 119 | Protocol: HTTP |
| 120 | VpcId: !Ref VPCId |
| 121 | HealthCheckPath: / |
| 122 | TargetType: instance |
| 123 | |
| 124 | MyLoadBalancer: |
| 125 | Type: AWS::ElasticLoadBalancingV2::LoadBalancer |
| 126 | Properties: |
| 127 | Name: !Sub "${AWS::StackName}-alb" |
| 128 | Scheme: internet-facing |
| 129 | Type: application |
| 130 | Subnets: |
| 131 | - !Ref PublicSubnet1 |
| 132 | - !Ref PublicSubnet2 |
| 133 | ``` |
| 134 | |
| 135 | ### 5. Add Scaling Policies |
| 136 | |
| 137 | Implement target tracking scaling: |
| 138 | |
| 139 | ```yaml |
| 140 | Resources: |
| 141 | TargetTrackingPolicy: |
| 142 | Type: AWS::AutoScaling::ScalingPolicy |
| 143 | Properties: |
| 144 | PolicyName: !Sub "${AWS::StackName}-target-tracking" |
| 145 | PolicyType: TargetTrackingScaling |
| 146 | AutoScalingGroupName: !Ref MyAutoScalingGroup |
| 147 | TargetTrackingConfiguration: |
| 148 | PredefinedMetricSpecification: |
| 149 | PredefinedMetricType: ASGAverageCPUUtilization |
| 150 | TargetValue: 70 |
| 151 | DisableScaleIn: false |
| 152 | ``` |
| 153 | |
| 154 | ### 6. Configure Lifecycle Hooks |
| 155 | |
| 156 | Implement hooks for graceful instance management: |
| 157 | |
| 158 | ```yaml |
| 159 | Resources: |
| 160 | LifecycleHookTermination: |
| 161 | Type: AWS::AutoScaling::LifecycleHook |
| 162 | Properties: |
| 163 | LifecycleHookName: !Sub "${AWS::StackName}-termination-hook" |
| 164 | AutoScalingGroupName: !Ref MyAutoScalingGroup |
| 165 | LifecycleTransition: autoscaling:EC2_INSTANCE_TERMINATING |
| 166 | HeartbeatTimeout: 300 |
| 167 | NotificationTargetARN: !Ref SNSTopic |
| 168 | RoleARN: !Ref LifecycleHookRole |
| 169 | ``` |
| 170 | |
| 171 | ### 7. Set Up Monitoring |
| 172 | |
| 173 | Configure CloudWat |