项目作者: base2Services

项目描述 :
Start and stop AWS resources managed by Cloudformation for cost savings
高级语言: Ruby
项目地址: git://github.com/base2Services/cfn-start-stop-stack.git
创建时间: 2017-06-26T07:23:44Z
项目社区:https://github.com/base2Services/cfn-start-stop-stack

开源协议:MIT License

下载


cfn-library

Build Status

About

Base2Services Common Cloud Formation stacks functionality

Installation

  • As a gem gem install cfn_manage

  • As a docker container docker pull base2/cfn-manage

  • Download source code git clone https://github.com/base2Services/cfn-start-stop-stack

Run As Docker Container

Running cfn_manage inside a docker container means you don’t have to worry about
managing the runtime environment.

  1. docker run -ti --rm -v $HOME/.aws/credentials:/root/.aws/credentials base2/cfn-manage

You can also pass in additional Environment Variables using the -e flag in the run command

  1. docker run -ti --rm -v $HOME/.aws/credentials:/root/.aws/credentials -e AWS_REGION=us-east-1 base2/cfn-manage

Functionality

Stack traversal

Used to traverse through stack and all it’s substacks

Start-stop environment functionality

Stop environment will

  • Set all ASG’s size to 0
  • Stops RDS instances
  • If RDS instance is Multi-AZ, it is converted to single-az prior it
    is being stopped
  • Disable CloudWatch Alarm actions

Start environment operation will

  • Set all ASG’s size to what was prior stop operation
  • Starts ASG instances
  • If ASG instance was Mutli-AZ, it is converted back to Multi-AZ
  • Enable CloudWatch Alarm actions
    Metadata about environment, such as number of desired/max/min instances within ASG and MultiAZ property
    for rds instances, is stored in S3 bucket specified via --source-bucket switch or SOURCE_BUCKET environment
    variable.

Both start and stop environment operations are idempotent, so if you run stop-environment
two times in a row, initial configuration of ASG will persist in S3 bucket (rather than storing 0/0/0) as ASG configuration.
Same applies for start operation - running it against already running environment won’t perform any operations.

In case of some configuration data being lost, script will continue and work with existing data (e.g data about asgs
removed from S3, but rds data persists will results in RDS instances being started)

Order of operations is supported at this point as hardcoded weights per resource type. Pull Requests are welcome
for supporting dynamic discovery of order of execution - local configuration file override is one of
the possible sources.

Start - stop cloudformation stack

Supported resources

AWS::AutoScaling::AutoScalingGroup

Stop operation will set desired capacity of ASG to 0

Start operation will restore previous capacity

AWS::EC2::Instance

Stop operation will stop instance using StopInstances api call

Start operation will start instance using StartInstances api call

AWS::RDS::DBInstance

Stop operation will stop rds instance. Aurora is not supported yet on AWS side. Note that RDS instance can be stopped
for two weeks at maximum. If instance is Multi-AZ, it will get converted to Single-AZ instance, before being stopped
(Amazon does not support stopping Multi-AZ rds instances)

Start operation will start rds instance. If instance was running in Multi-AZ mode before being stopped,
it will get converted to Multi-AZ prior being started

AWS::DocDB::DBCluster

Stop Cluster will be stopped, note that a cluster can only be stopped for a maximum of 7 days, after this time it will turn back on.

Start Cluster will be started

AWS::CloudWatch::Alarm

Stop operation will disable all of alarm’s actions

Start operation will enable all of alarm’s actions

AWS::EC2::SpotFleet

Stop operation will set spot fleet target to capacity to 0

Start operation will restore spot fleet target to capacity to what was set prior the stack being stopped.

AWS::ECS::Cluster

Stop operation will query all services running in the cluster and set desired capacity to 0

Start operation will query all services assocated with the cluster restore desired capacity to what was set prior the stack being stopped.

CLI usage

You’ll find usage of cfn_manage within usage.txt file

  1. Usage: cfn_manage [command] [options]
  2. Commands:
  3. cfn_manage help
  4. cfn_manage version
  5. cfn_manage stop-environment --stack-name [STACK_NAME]
  6. cfn_manage start-environment --stack-name [STACK_NAME]
  7. cfn_manage stop-asg --asg-name [ASG]
  8. cfn_manage start-asg --asg-name [ASG]
  9. cfn_manage stop-rds --rds-instance-id [RDS_INSTANCE_ID]
  10. cfn_manage start-rds --rds-instance-id [RDS_INSTANCE_ID]
  11. cfn_manage stop-aurora-cluster --aurora-cluster-id [AURORA_CLUSTER_ID]
  12. cfn_manage start-aurora-cluster --aurora-cluster-id [AURORA_CLUSTER_ID]
  13. cfn_manage stop-docdb-cluster --docdb-cluster-id [DOCDB_CLUSTER_ID]
  14. cfn_manage start-docdb-cluster --docdb-cluster-id [DOCDB_CLUSTER_ID]
  15. cfn_manage stop-ec2 --ec2-instance-id [EC2_INSTANCE_ID]
  16. cfn_manage start-ec2 --ec2-instance-id [EC2_INSTANCE_ID]
  17. cfn_manage stop-spot-fleet --spot-fleet [SPOT_FLEET]
  18. cfn_manage start-spot-fleet --spot-fleet [SPOT_FLEET]
  19. cfn_manage stop-ecs-cluster --ecs-cluster [ECS_CLUSTER]
  20. cfn_manage start-ecs-cluster --ecs-cluster [ECS_CLUSTER]
  21. cfn_manage disable-alarm --alarm [ALARM]
  22. cfn_manage enable-alarm --alarm [ALARM]
  23. General options:
  24. --source-bucket [BUCKET]
  25. Pucket used to store / pull information from
  26. --aws-role [ROLE_ARN]
  27. AWS Role to assume when performing operations. Any reads and
  28. write to source bucket will be performed outside of this role
  29. -r [AWS_REGION], --region [AWS_REGION]
  30. AWS Region to use when making API calls
  31. -p [AWS_PROFILE], --profile [AWS_PROFILE]
  32. AWS Shared profile to use when making API calls
  33. --dry-run
  34. Applicable only to [start|stop-environment] commands. If dry run is enabled
  35. info about assets being started / stopped will ne only printed to standard output,
  36. without any action taken.
  37. --debug
  38. Displays debug logs
  39. --continue-on-error
  40. Applicable only to [start|stop-environment] commands. If there is problem with stopping a resource,
  41. (e.g. cloudformation stack not being synced or manual resource deletion) script will continue it's
  42. operation. By default script stops when there is problem with starting/stopping resource, and expects
  43. manual intervention to fix the root cause for failure.
  44. --skip-wait
  45. Skips waiting for resources to achieve stopped or started states.
  46. --wait-async
  47. Default wait action is to wait for each individual resource to be stopped and started before continuing.
  48. This will enabled waiting for resources in groups based on priority. Option only useful when used with
  49. start-environment and stop-environment commands.
  50. --ignore-missing-ecs-config
  51. This option is required for starting a ecs service that was stopped outside of cfn_manage.
  52. --asg-suspend-termination
  53. Will stop instances in the autoscaling group(s) instead of the default behaviour of termination.
  54. --asg-wait-state
  55. Allowed values ['HealthyInASG','Running','HealthyInTargetGroup']
  56. Default: 'HealthyInASG'
  57. 'HealthyInASG' - waits for all instances to reach a healthy state in the asg
  58. 'Running' - waits for all instances to reach the EC2 running state
  59. 'HealthyInTargetGroup' - waits for all instances to reach a healthy state in all asg assocated target groups
  60. --ecs-wait-state
  61. Allowed values ['Running','HealthyInTargetGroup']
  62. Default: 'Skip'
  63. 'Running' - waits for all ecs services in cluster to reach the running state
  64. 'HealthyInTargetGroup' - waits for all ecs services in cluster to reach a healthy state in all assocated target groups
  65. --tags
  66. will query resource tags for individual resource settings.
  67. `cfn_manage:priority` for prefered starting order
  68. will default to defined resource order if no tag is found or resource doesn't support tags
  69. --ecs-wait-container-instances
  70. waits for a container instance to be active in the ecs cluster before starting services
  71. --ec2-hibernate
  72. Hibernates ec2 instance if the instance was enabled for hibernation at launch. If the instance cannot hibernate successfully, a normal shutdown occurs.

Environment Variables

Also, there are some environment variables that control behaviour of the application.
There are command line switch counter parts for all of the

AWS_ASSUME_ROLE as env var or --aws-role as CLI switch

AWS_REGION as env car or -r, --region as CLI switch

AWS_PROFILE as env var or -p, --profile as CLI switch

SOURCE_BUCKET as env var or --source-bucket as CLI switch

DRY_RUN as env var (set to ‘1’ to enable) or --dry-run as CLI switch

IGNORE_MISSING_ECS_CONFIG as env var (set to ‘1’ to enable) or --ignore-missing-ecs-config as CLI switch

CFN_CONTINUE_ON_ERROR as env var (set to ‘1’ to enable) or --continue-on-error as CLI switch

SKIP_WAIT as env var (set to ‘1’ to enable) or --skip-wait as CLI switch

WAIT_ASYNC as env var (set to ‘1’ to enable) or --wait-async as CLI switch

ASG_SUSPEND_TERMINATION as env var (set to ‘1’ to enable) or --asg-suspend-termination as CLI switch

CFN_TAGS as env var (set to ‘1’ to enable) or --tags as CLI switch

ECS_WAIT_CONTAINER_INSTANCES as env var (set to ‘1’ to enable) or --ecs-wait-container-instances as CLI switch

CFN_DEBUG as env var (set to ‘1’ to enable) or --debug as CLI switch

EC2_HIBERNATE as env var (set to ‘1’ to enable) or --ec2-hibernate as CLI switch

AWS Resource Tags

will query resource tags for individual resource settings. please see bellow the list of resources currently supported by tags and their options.

AWS::AutoScaling::AutoScalingGroup’

  1. cfn_manage:wait_state: 'HealthyInASG'
  2. cfn_manage:skip_wait: true
  3. cfn_manage:priority: 200
  4. cfn_manage:suspend_termination: true

AWS::ECS::Cluster

  1. cfn_manage:wait_state: 'Running'
  2. cfn_manage:skip_wait: true
  3. cfn_manage:priority: 200
  4. cfn_manage:wait_container_instances: true
  5. cfn_manage:ignore_missing_ecs_config: true

AWS::EC2::Instance

  1. cfn_manage:ec2_hibernate: true

Release process

  • Bump up version gem install bump && bump [patch|minor|major]
  • Update timestamp in cfn_manage.gemspec
  • Create and publish gem gem build cfn_manage.gemspec && gem push cfn_manage-$VERSION.gem
  • Create release page on GitHub