Unverified Commit ba7ec2b6 authored by Doug's avatar Doug Committed by GitHub
Browse files

Added AutoScaling code examples (#576)



* Added AutoScaling code examples

* CR feedback

Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent 82cb50ea
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ dependencies {
// Tier 1 Services have examples and tests
val tier1Services = setOf(
    "apigateway",
    "autoscaling",
    "batch",
    "cloudformation",
    "cloudwatch",
+14 −0
Original line number Diff line number Diff line
[package]
name = "autoscaling-code-examples"
version = "0.1.0"
authors = ["Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
autoscaling = { package = "aws-sdk-autoscaling", path = "../../build/aws-sdk/autoscaling" }
aws-types = { path = "../../build/aws-sdk/aws-types" }
tokio = { version = "1", features = ["full"] }
structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use autoscaling::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region::{self, ProvideRegion};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The name of the AutoScaling group.
    #[structopt(short, long)]
    autoscaling_name: String,

    /// The ID of the EC2 instance to add to the AutoScaling group.
    #[structopt(short, long)]
    instance_id: String,

    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Creates an AutoScaling group in the Region.
/// # Arguments
///
/// * `-a AUTOSCALING-NAME` - The name of the AutoScaling group.
/// * `-i INSTANCE-ID` - The ID of the Ec2 instance to add to the AutoScaling group.
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        autoscaling_name,
        instance_id,
        region,
        verbose,
    } = Opt::from_args();

    let region_provider = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("AutoScaling version:    {}", PKG_VERSION);
        println!("Region:                 {:?}", region_provider.region());
        println!("AutoScaling group name: {}", &autoscaling_name);
        println!("Instance ID:            {}", &instance_id);
        println!();
    }

    let conf = Config::builder().region(region_provider).build();
    let client = Client::from_conf(conf);

    client
        .create_auto_scaling_group()
        .auto_scaling_group_name(autoscaling_name)
        .instance_id(instance_id)
        .min_size(1)
        .max_size(5)
        .send()
        .await?;

    println!("Created AutoScaling group");
    Ok(())
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use autoscaling::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region::{self, ProvideRegion};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The name of the AutoScaling group.
    #[structopt(short, long)]
    autoscaling_name: String,

    /// Whether to force the deletion.
    #[structopt(short, long)]
    force: bool,

    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Updates an AutoScaling group in the Region to the specified maximum size.
/// # Arguments
///
/// * `- AUTOSCALING-NAME` - The name of the AutoScaling group.
/// * - [-f] - Whether to force the deletion.
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        autoscaling_name,
        force,
        region,
        verbose,
    } = Opt::from_args();

    let region_provider = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("AutoScaling version:    {}", PKG_VERSION);
        println!("Region:                 {:?}", region_provider.region());
        println!("AutoScaling group name: {}", &autoscaling_name);
        println!("Force deletion?:        {}", &force);
        println!();
    }

    let conf = Config::builder().region(region_provider).build();
    let client = Client::from_conf(conf);

    client
        .delete_auto_scaling_group()
        .auto_scaling_group_name(autoscaling_name)
        .set_force_delete(force.then(|| true))
        .send()
        .await?;

    println!("Deleted AutoScaling group");
    Ok(())
}
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use autoscaling::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region::{self, ProvideRegion};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Lists your AutoScaling groups in the Region.
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt { region, verbose } = Opt::from_args();

    let region_provider = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("AutoScaling version: {}", PKG_VERSION);
        println!("Region:              {:?}", region_provider.region());
        println!();
    }

    let conf = Config::builder().region(region_provider).build();
    let client = Client::from_conf(conf);

    let resp = client.describe_auto_scaling_groups().send().await?;

    println!("Groups:");

    let groups = resp.auto_scaling_groups.unwrap_or_default();

    for group in &groups {
        println!(
            "  {}",
            group.auto_scaling_group_name.as_deref().unwrap_or_default()
        );
        println!(
            "  ARN:          {}",
            group.auto_scaling_group_arn.as_deref().unwrap_or_default()
        );
        println!("  Minimum size: {}", group.min_size.unwrap_or_default());
        println!("  Maximum size: {}", group.max_size.unwrap_or_default());
        println!();
    }

    println!("Found {} group(s)", groups.len());
    Ok(())
}
Loading