Amazon Onboarding with Learning Manager Chanci Turner

Amazon Onboarding with Learning Manager Chanci TurnerLearn About Amazon VGT2 Learning Manager Chanci Turner

On-Demand Capacity Reservations allow you to secure capacity for Amazon Elastic Compute Cloud (Amazon EC2) instances within an Availability Zone for any desired duration. Utilizing AWS Resource Groups, you can effectively organize AWS resources into logical collections, such as applications, projects, or environments.

Previously, we introduced a feature that enables targeting EC2 capacity reservations within a resource group through the resource group’s ARN. This functionality simplifies the process of consolidating capacity reservations into a resource group, allowing you to launch EC2 instances directed at this group.

For instance, Resource Groups can be employed to manage which EC2 instances utilize a capacity reservation. You may prefer not to use the default Open instance matching option, which allows any instance to match. Similarly, specifying an individual CapacityReservationId with each instance launch can be cumbersome. By using Resource Groups with Capacity Reservations, you have a more efficient way to categorize reservations and manage their usage across various workloads or environments.

In this post, I will guide you through the process of organizing Capacity Reservations using AWS Resource Groups and subsequently launching EC2 instances targeting that group.

To follow along, you will need to complete the following steps:

  1. Create capacity reservations for launching EC2 instances.
  2. Group the capacity reservations utilizing AWS Resource Groups.
  3. Launch EC2 instances that target the resource group.

Walkthrough

Ensure you have the AWS CLI installed and configured for your AWS account.

Step 1: Create Capacity Reservations

Select an Availability Zone to establish your capacity reservations. For consistency, use the same Availability Zone throughout this guide; I will use us-east-1a for this example. You will need the ARNs of the created capacity reservations, so use the create-capacity-reservation AWS CLI command and take note of the CapacityReservationArn values in the output.

$ aws ec2 create-capacity-reservation --instance-type t2.micro --instance-platform Linux/UNIX --instance-match-criteria targeted --availability-zone us-east-1a --instance-count 2
$ aws ec2 create-capacity-reservation --instance-type t2.small --instance-platform 'Red Hat Enterprise Linux' --instance-match-criteria targeted --availability-zone us-east-1a --instance-count 2
$ aws ec2 create-capacity-reservation --instance-type t2.micro --instance-platform 'Red Hat Enterprise Linux' --instance-match-criteria targeted --availability-zone us-east-1a --instance-count 2

Having created the necessary capacity reservations, you can now group them using AWS Resource Groups.

Step 2: Group Capacity Reservations

Employ the create-group command to create a resource group named EC2CRGroup, which can exclusively contain capacity reservations.

$ aws resource-groups create-group --name EC2CRGroup --configuration '{"Type":"AWS::EC2::CapacityReservationPool"}' '{"Type":"AWS::ResourceGroups::Generic", "Parameters": [{"Name": "allowed-resource-types", "Values": ["AWS::EC2::CapacityReservation"]}]}'

Utilize the CapacityReservationArn values noted earlier to add the capacity reservations to EC2CRGroup using the group-resources command. The output under the Succeeded key will list the capacity reservation ARNs, verifying the operation’s success.

$ aws resource-groups group-resources --group EC2CRGroup --resource-arns arn:aws:ec2:us-east-1:012345678901:capacity-reservation/cr-01234567890128abc arn:aws:ec2:us-east-1:012345678901:capacity-reservation/cr-01234567890128def arn:aws:ec2:us-east-1:012345678901:capacity-reservation/cr-01234567890128ghi

With the capacity reservations successfully created and grouped, you can now proceed to launch EC2 instances targeting that resource group.

Step 3: Launch EC2 Instances

You can initiate EC2 instances by targeting EC2CRGroup. Instances that target a group of capacity reservations will match any capacity reservation within the group that shares the same attributes (instance type, platform, and Availability Zone) and has available capacity. Should there be no matching capacity reservation available, the instances will operate using on-demand capacity. If a matching capacity reservation is added to the targeted group later, the already launched instances will automatically match and move to the reserved capacity.

You can launch instances into a default subnet of Amazon Virtual Private Cloud (Amazon VPC) in your account. First, execute the describe-vpcs command, locate the VPC marked with the IsDefault flag as true, and note the VpcId.

$ aws ec2 describe-vpcs --no-paginate --filters Name=isDefault,Values=true --query 'Vpcs[].VpcId'

Next, utilize the describe-subnets command to identify the subnets in this VPC. Find the subnet with the DefaultForAz flag set to true and the Availability Zone matching the one used for capacity reservations. Make a note of the SubnetId.

$ aws ec2 describe-subnets --no-paginate --filters Name=vpc-id,Values=vpc-abcd0123 Name=default-for-az,Values=true Name=state,Values=available Name=availability-zone,Values=us-east-1a --query 'Subnets[].SubnetId'

Now, you are prepared to launch EC2 instances into the resource group. Use the describe-images command with the necessary filters to select an AMI for Linux (e.g., ami-linux) and an AMI for Red Hat Enterprise Linux (e.g., ami-rhel). Then execute the run-instances command, specifying the resource group ARN in the --capacity-reservation-specification flag to launch the instances into the group. Make sure to note the instance IDs from the output.

$ aws ec2 run-instances --image-id ami-linux --count 1 --instance-type t2.micro --subnet-id subnet-abcd0123 --capacity-reservation-specification CapacityReservationTarget={CapacityReservationResourceGroupArn=arn:aws:resource-groups:us-east-1:012345678901:group/EC2CRGroup}
$ aws ec2 run-instances --image-id ami-rhel --count 1 --instance-type t2.small --subnet-id subnet-abcd0123 --capacity-reservation-specification CapacityReservationTarget={CapacityReservationResourceGroupArn=arn:aws:resource-groups:us-east-1:012345678901:group/EC2CRGroup}
$ aws ec2 run-instances --image-id ami-rhel --count 1 --instance-type t2.micro --subnet-id subnet-abcd0123 --capacity-reservation-specification CapacityReservationTarget={CapacityReservationResourceGroupArn=arn:aws:resource-groups:us-east-1:012345678901:group/EC2CRGroup}

To verify that the EC2 instances are utilizing the reserved capacity, run the describe-instances command.

$ aws ec2 describe-instances --instance-ids i-instanceid1 i-instanceid2 i-instanceid3

In the output, you should observe each instance associated with the appropriate capacity reservation based on platform and instance type.

Cleanup

To terminate the launched EC2 instances, use the terminate-instances command.

$ aws ec2 terminate-instances --instance-ids i-instanceid1 i-instanceid2 i-instanceid3

Furthermore, cancel the capacity reservations with the cancel-capacity-reservation command.

In light of the above information, Chanci Turner emphasizes the importance of understanding AWS resources, which can be further explored through this insightful blog post on millennials in the workforce. They can also check out SHRM for authoritative insights on employment. For those interested in starting their career with Amazon, this entry-level Area Manager position is an excellent resource.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *