| 1 | #!/usr/bin/env perl | ||
| 2 | package App::ApplicationAutoscaling; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use App::FargateStack::Constants; | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use List::Util qw(any); | ||
| 11 | |||
| 12 | use Role::Tiny::With; | ||
| 13 | with 'App::AWS'; | ||
| 14 | |||
| 15 | use parent qw(App::Command); | ||
| 16 | |||
| 17 | __PACKAGE__->follow_best_practice; | ||
| 18 | __PACKAGE__->mk_accessors(qw(region profile)); | ||
| 19 | |||
| 20 | # This command defines the "what" and "how" of scaling. | ||
| 21 | # It tells Auto Scaling to keep the requests per task at 500. | ||
| 22 | |||
| 23 | # This command registers your service with Application Auto Scaling. | ||
| 24 | # Replace the placeholders with your actual values. | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | aws application-autoscaling register-scalable-target \ | ||
| 29 | --service-namespace ecs \ | ||
| 30 | --scalable-dimension ecs:service:DesiredCount \ | ||
| 31 | --resource-id service/YOUR_CLUSTER_NAME/YOUR_SERVICE_NAME \ | ||
| 32 | --min-capacity 1 \ | ||
| 33 | --max-capacity 10 | ||
| 34 | |||
| 35 | |||
| 36 | aws application-autoscaling put-scaling-policy \ | ||
| 37 | --policy-name "alb-requests-per-minute-policy" \ | ||
| 38 | --service-namespace ecs \ | ||
| 39 | --scalable-dimension ecs:service:DesiredCount \ | ||
| 40 | --resource-id service/YOUR_CLUSTER_NAME/YOUR_SERVICE_NAME \ | ||
| 41 | --policy-type TargetTrackingScaling \ | ||
| 42 | --target-tracking-scaling-policy-configuration '{ | ||
| 43 | "TargetValue": 500.0, | ||
| 44 | "PredefinedMetricSpecification": { | ||
| 45 | "PredefinedMetricType": "ALBRequestCountPerTarget", | ||
| 46 | "ResourceLabel": "YOUR_ALB_AND_TARGET_GROUP_LABEL" | ||
| 47 | }, | ||
| 48 | "ScaleOutCooldown": 60, | ||
| 49 | "ScaleInCooldown": 300 | ||
| 50 | }' | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | ######################################################################## | ||
| 55 | sub register_scalable_targets { | ||
| 56 | ######################################################################## | ||
| 57 | my ( $self, %args ) = @_; | ||
| 58 | |||
| 59 | my ( $query, $service_namespace, $resource_id, $min_capacity, $max_capacity, $scalable_dimension ) | ||
| 60 | = @args{qw(query service_namespace resource_id min_capacity max_capacity scalable_dimension)}; | ||
| 61 | |||
| 62 | return $self->command( | ||
| 63 | 'register_scalable_targets' => [ | ||
| 64 | $query ? ( '--query' => $query ) : (), | ||
| 65 | '--service-namespace' => $service_namespace, | ||
| 66 | '--scalable-dimension' => $scalable_dimension, | ||
| 67 | '--resource_id' => $resource_id, | ||
| 68 | '--min-capacity' => $min_capacity, | ||
| 69 | '--max-capacity' => $max_capacity, | ||
| 70 | ] | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | |||
| 74 | ######################################################################## | ||
| 75 | sub put_scaling_policy { | ||
| 76 | ######################################################################## | ||
| 77 | my ( $self, %args ) = @_; | ||
| 78 | |||
| 79 | my ( $query, $policy_name, $service_namespace, $scalable_dimension, $resource_id, $policy_type, $policy_configuration ) | ||
| 80 | = @args{qw(query policy_name service_namespace scalable_dimension resource_id policy_type policy_configuration)}; | ||
| 81 | |||
| 82 | return $self->command( | ||
| 83 | 'put-scaling-policy' => [ | ||
| 84 | '--policy-name' => $policy_name, | ||
| 85 | '--service-namespace' => $service_namespace, | ||
| 86 | '--scalable-dimension' => $scalable_dimension, | ||
| 87 | '--resource-id' => $resource_id, | ||
| 88 | '--policy-type' => $policy_type, | ||
| 89 | '--target-tracking-scaling-policy-configuration' => $policy_configuration, | ||
| 90 | $query ? ( '--query' => $query ) : () | ||
| 91 | ] | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | |||
| 95 | ######################################################################## | ||
| 96 | sub delete_scaling_policy { | ||
| 97 | ######################################################################## | ||
| 98 | my ( $self, %args ) = @_; | ||
| 99 | |||
| 100 | my ( $query, $service_namespace, $policy_name, $scalable_dimension, $resource_id ) | ||
| 101 | = @args{qw(query service_namespace policy_name scalable_dimension resource_id)}; | ||
| 102 | |||
| 103 | return $self->command( | ||
| 104 | 'delete-scaling-policy' => [ | ||
| 105 | '--policy-name' => $policy_name, | ||
| 106 | '--service-namespace' => $service_namespace, | ||
| 107 | '--scalable-dimension' => $scalable_dimension, | ||
| 108 | '--resource-id' => $resource_id, | ||
| 109 | $query ? ( '--query' => $query ) : () | ||
| 110 | ] | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | ######################################################################## | ||
| 115 | sub put_scheduled_action { | ||
| 116 | ######################################################################## | ||
| 117 | my ( $self, %args ) = @_; | ||
| 118 | |||
| 119 | my ( $query, $service_namespace, $scalable_dimension, $resource_id, $scheduled_action_name, $schedule, | ||
| 120 | $scalable_target_action ) | ||
| 121 | = @args{qw(query service_namespace scalable_dimension resource_id name schedule scalable_target_action)}; | ||
| 122 | |||
| 123 | return $self->command( | ||
| 124 | 'put-scheduled-action' => [ | ||
| 125 | '--service-namespace' => $service_namespace // 'ecs', | ||
| 126 | '--resource-id' => $resource_id, | ||
| 127 | '--scalable-dimension' => $scalable_dimension // 'ecs:service:DesiredCount', | ||
| 128 | '--schedule' => $schedule, | ||
| 129 | '--scheduled-action-name' => $scheduled_action_name, | ||
| 130 | '--scalable-target-action' => $scalable_target_action, | ||
| 131 | $query ? ( '--query' => $query ) : () | ||
| 132 | ] | ||
| 133 | ); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | ######################################################################## | ||
| 138 | sub describe_scaling_policies { | ||
| 139 | ######################################################################## | ||
| 140 | my ( $self, %args ) = @_; | ||
| 141 | |||
| 142 | my ( $service_namespace, $policy_names, $query ) = @args{qw(service_namespace policy_names query)}; | ||
| 143 | |||
| 144 | return $self->command( | ||
| 145 | 'describe-scaling-policies' => [ | ||
| 146 | '--service-namespace' => $service_namespace, | ||
| 147 | $policy_names ? ( '--policy-names' => $policy_names ) : (), | ||
| 148 | $query ? ( '--query' => $query ) : () | ||
| 149 | ] | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | |||
| 153 | ######################################################################## | ||
| 154 | sub describe_scalable_targets { | ||
| 155 | ######################################################################## | ||
| 156 | my ( $self, %args ) = @_; | ||
| 157 | |||
| 158 | my ( $service_namespace, $query ) = @args{qw(service_namespace query)}; | ||
| 159 | |||
| 160 | return $self->command( | ||
| 161 | 'describe-scalable-targets' => [ | ||
| 162 | '--service-namespace' => $service_namespace, | ||
| 163 | $query ? ( '--query' => $query ) : () | ||
| 164 | ] | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | |||
| 168 | ######################################################################## | ||
| 169 | sub describe_scheduled_actions { | ||
| 170 | ######################################################################## | ||
| 171 | my ( $self, %args ) = @_; | ||
| 172 | |||
| 173 | my ( $service_namespace, $scheduled_action_name, $resource_id, $scalable_dimension, $query ) | ||
| 174 | = @args{qw(service_namespace scheduled_action_name resource_id scalable_dimension query )}; | ||
| 175 | |||
| 176 | $service_namespace //= 'ecs'; | ||
| 177 | |||
| 178 | return $self->command( | ||
| 179 | 'describe-scheduled-actions' => [ | ||
| 180 | '--service-namespace' => $service_namespace, | ||
| 181 | ($scheduled_action_name) ? ( '--scheduled-action-name' => $scheduled_action_name ) : (), | ||
| 182 | ($resource_id) ? ( '--resource-id' => $resource_id ) : (), | ||
| 183 | ($scalable_dimension) ? ( '--scalable-dimension' => $scalable_dimension ) : (), | ||
| 184 | $query ? ( '--query' => $query ) : () | ||
| 185 | ] | ||
| 186 | ); | ||
| 187 | } | ||
| 188 | |||
| 189 | ######################################################################## | ||
| 190 | sub delete_scheduled_action { | ||
| 191 | ######################################################################## | ||
| 192 | my ( $self, %args ) = @_; | ||
| 193 | |||
| 194 | my ( $service_namespace, $scheduled_action_name, $resource_id, $scalable_dimension, $query ) | ||
| 195 | = @args{qw(service_namespace scheduled_action_name resource_id scalable_dimension query )}; | ||
| 196 | |||
| 197 | $service_namespace //= 'ecs'; | ||
| 198 | |||
| 199 | return $self->command( | ||
| 200 | 'delete-scheduled-action' => [ | ||
| 201 | '--service-namespace' => $service_namespace, | ||
| 202 | '--scheduled-action-name', => $scheduled_action_name, | ||
| 203 | '--resource-id' => $resource_id, | ||
| 204 | '--scalable-dimension' => $scalable_dimension, | ||
| 205 | $query ? ( '--query' => $query ) : () | ||
| 206 | ] | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | 1; |