AWS Resource Groups

Using AWS Resource Groups to manage your resources

When building your infrastructure in the cloud, one of the challenges that need to be addressed from the early days is how to monitor and manage the infrastructure.

AWS provide a number of services that help you with monitoring and management. In this article we will take a look at AWS Resource Groups and their integration with other services like Tagging and Cost Explorer.

AWS Resource Groups

AWS Resource Groups help you organize your AWS resources and manage them as a group.  When paired with Tagging; this grouping can be based on environment (e.g. DEV, UAT, and PROD), specific project, cost center or practically any groupings you see fit for your infrastructure.

AWS Resource Groups can also be created based on a CloudFormation stack. Regardless of the method that you create your group (based on Tags or CloudFormation stack) you have the option to select the resource types you want in your group.

Check this Link to learn more about creating Resource Groups.

Once a group is created you can automate the tasks you want to perform on a large number of resources at one time.

TAG Editor

Tagging your resources should be one of the fundamental tasks when creating your infrastructure in AWS. Tags are more than just description fields for your resources.

You can use IAM to grant or restrict access to resources based on specific tags. AWS Cost Explorer can show you your cost breakdown grouped by tags so you can track your cost based on resources (not just services).

When you create a Resource Group for your resources you have the option to assign tags to all the resources in one shot instead of tag the resources one by one.

You can also tag the Resource Group, and see your cost breakdown for the whole Resource Group.

Example

Let’s create a resource group for all our development EC2 instances. After that we will use the created group to shut down the EC2 instances in that group.

Create EC2 instances and tag them with Environment=DEV

using AWS CLI we create three EC2 t2.micro instances and tag them with Environment=DEV

aws ec2 run-instances --image-id ami-0b898040803850657 --count 3 --instance-type t2.micro --key-name Res_Grp --security-group-ids sg-0344117de351fc161 --subnet-id subnet-0e9985fe9d00db12f --tag-specifications ResourceType=instance,Tags=[{Key=Environment,Value=DEV}]

Create an AWS Resource Group based on tag

Now that we have the instances and tagged them, we will create an AWS Resource Group based on the tag query. Please note that in the query I have specified that I want to create the group for EC2 instances.

You can include different resource types in your group. As a matter of act you mention “AllSupported” in your query.

Just be careful if you do that, and later on you add resources to your infrastructure, they will automatically be subject of the actions that you define for the resources in this group which sometimes might not be desirable.

Once we click on “View group resources” button the query will get executed and all the EC2 servers that have the tag “Environemt=DEV” will be added to the group.

Shutdown Instances

Now that we have the group created, we can either use AWS System Manager and have bulk actions applied to all the servers within this group or we can simply write a piece of code or CLI command to shut all of them down with one shot.

Please note that ordinarily you do such bulk actions on AWS Resource Group resources using AWS System Manager, however here we just want to show the possibilities and we use AWS CLI.

aws ec2 stop-instances --instance-ids $(aws resource-groups list-group-resources --group-name EC2DevServers | grep ResourceArn | cut -d "/" -f 2 | tr '\n' ' ' | sed -r 's/\",/ /g')

The result in AWS Management Console:

So as you can AWS Resource Groups make organization and management of your resources easier. If you are interested in using them make sure you look into the integration with AWS System Manager.

 




Leave a Reply

Your email address will not be published.


Comment


Name

Email

Url