如何通过SDK获取ElasticBeanstalk环境列表及其AutoScaling组信息?
我想为用户做一个API,将实例缩小到0,以便在办公时间之后节省成本。
我可以使用DescribeEnvironments
然后为每个环境调用DescribeEnvironmentResources
,但我认为这不太理想?我正在O(n)
拨打DescribeEnvironmentResources
这可能会慢一些?
答案 0 :(得分:2)
您可能希望描述ElasticBeanstalk环境,并从响应中获取自动缩放组的名称。然后查询自动缩放组的信息。您可能希望从此处更改Autoscaling组的所需容量。您应该能够将其添加到用户的API中。
使用ruby sdk: 获取有关ElasticBeanStalk的信息: http://docs.aws.amazon.com/sdkforruby/api/Aws/ElasticBeanstalk/Client.html#describe_environment_resources-instance_method
获取有关AutoScaling组的信息: http://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#describe_auto_scaling_groups-instance_method
设置AutoScaling组的所需容量: http://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#set_desired_capacity-instance_method
代码应该类似于:
elasticbeanstalk = Aws::ElasticBeanstalk::Client.new(
region: region_name,
credentials: credentials,
)
# assuming you know the environment names
resp = elasticbeanstalk.describe_environment_resources({
environment_name: "my-env",
})
auto_scaling_groups_array = resp.to_hash['environment_resources']['auto_scaling_groups']
# auto_scaling_groups_array[0].name should give the name of the first autoscaling group
autoscaling = Aws::AutoScaling::Client.new(
region: region_name,
credentials: credentials,
# ...
)
# get the resources of an AutoScaling Group, you probably want to do this for each group
resp = autoscaling.describe_auto_scaling_groups({
auto_scaling_group_names: [
"my-auto-scaling-group", # or auto_scaling_groups_array[0].name
]
})
# set the desired capacity, you probably want to do this for each group
resp = autoscaling.set_desired_capacity({
auto_scaling_group_name: "my-auto-scaling-group", # or auto_scaling_groups_array[0].name
desired_capacity: 2,
honor_cooldown: true,
})
您可能希望为每个地区执行此操作。我没有测试过这个。我希望有所帮助。