Amazon Web Services (AWS) Console is a powerful tool to manage all the resources in the cloud environment. It helps to monitor, manage, and maintain all the instances with ease. However, with the increase in the number of resources, it can become cumbersome to manage and locate instances that are no longer needed or have been terminated.
Terminated instances in AWS Console can create clutter and confusion, especially when you are working on several projects. You can often see terminated instances displayed in the console, which can be frustrating when you are trying to find active instances. Fortunately, AWS provides a simple solution to solve this problem: hiding terminated instances in AWS Console. Hiding terminated instances can help you focus on active resources and keep your Console organized.
In this article, we will discuss how to hide terminated instances in AWS Console, and provide you with examples to get the job done quickly and efficiently.
Step 1: Create a Role for Instance Management Access
Before we can start hiding terminated instances in AWS Console, we need to create a role for instance management access. This role will allow us to manage instances without having to grant full access privileges. To create a role for instance management access, complete the following steps:
- Open the AWS Management Console and navigate to the IAM dashboard.
- Click on Roles on the left sidebar.
- Click on Create Role.
- Choose the trusted entity (in this case, we will choose AWS service)
- Select EC2 under Choose a use case.
- Click on Next: Permissions.
- Select the required permissions in the new window. For instance management access, you need to select AmazonEC2ReadOnlyAccess.
- Click on Next: Tags (this is optional).
- Enter a name for the role and click Create Role.
Step 2: Create an AWS Lambda Function
The next step is to create an AWS Lambda function. This function will help to hide terminated instances in AWS Console. The code for the function is straightforward and easy to understand. To create an AWS Lambda function, follow these steps:
- Open the AWS Management Console and navigate to the Lambda dashboard.
- Click on Create Function.
- Choose Author from scratch and enter a name for the function.
- Choose the role you created in Step 1.
- Choose an appropriate runtime (in this case, we will choose Node.js 12.x)
- Click Create Function.
Step 3: Add Code to the AWS Lambda Function
Once you have created the AWS Lambda function, it's time to add the code that will help to hide terminated instances in AWS Console. The code we will use is as follows:
var aws = require('aws-sdk');
aws.config.update({region:'us-west-2'});
var ec2 = new aws.EC2({apiVersion: '2016-11-15'});
exports.handler = async function(event, context) {
var filters = [{
Name: 'instance-state-name',
Values: ['running']
}];
try {
var data = await ec2.describeInstances({Filters: filters}).promise();
console.log(JSON.stringify(data));
} catch (error) {
console.log(error);
}
return {
statusCode: 200,
headers: {},
body: {}
};
};
This code will filter out terminated instances from the list of instances displayed in AWS Console.
Step 4: Test the AWS Lambda Function
Now that we have added code to the AWS Lambda function let's test it to make sure it works as expected. Follow these steps to test the function:
- Click on Test.
- Enter a name for the test event.
- Paste the following into the test event box:
{
}
- Click on Create.
- Click on Test.
- Verify that the output shows only running instances.
Step 5: Schedule the AWS Lambda Function
To keep the instance list updated and hide terminated instances, we need to set up a schedule to run the Lambda function periodically. To do this, follow these steps:
- Open the AWS Management Console and navigate to the CloudWatch dashboard.
- Click on Rules in the left sidebar.
- Click on Create Rule.
- Under Event Source, choose Schedule.
- Under Fixed rate of, choose an appropriate time interval (in this case one hour).
- Under Targets, choose Lambda Function.
- Select the Lambda function you created in Step 2.
- Click on Configure Details.
- Enter a name for the rule.
- Click on Create Rule.
Conclusion
Hiding terminated instances in AWS Console can help to reduce clutter and make it easier to manage resources. With the steps outlined in this article, you can easily implement this feature using an AWS Lambda function and CloudWatch events. The example code provided can give you a head start in creating your solution, and you can modify it further according to your needs.
Once implemented, you can be sure that terminated instances will not be shown unnecessarily on your AWS Console, and you can concentrate on managing active resources efficiently.
I'm sorry, I would need some clarification on which previous topics you would like me to write more about. Could you please provide more information or specific topics? Thank you.
Popular questions
Certainly, here are five questions and answers on the topic of hiding terminated instances in AWS Console with code examples:
Q1. Why is it necessary to hide terminated instances in AWS Console?
A: It's necessary to hide terminated instances in AWS Console to reduce clutter and make it easier to manage resources. With many terminated instances appearing in the Console, it can be challenging and confusing to identify active resources. Hiding terminated instances will allow you to focus on active resources and keep your Console organized.
Q2. What is the role of the AWS Lambda function in hiding terminated instances?
A: The AWS Lambda function plays a crucial role in hiding terminated instances. It filters out terminated instances from the list of instances displayed in AWS Console and ensures that only running instances are shown.
Q3. What permissions are required for the role created for instance management access?
A: For instance management access, the role created must have "AmazonEC2ReadOnlyAccess" permission.
Q4. How do you schedule the Lambda function to run periodically?
A: To schedule the Lambda function to run periodically, CloudWatch events can be used. A CloudWatch Rule can be created to trigger the Lambda function at an appropriate time interval. This ensures that the instance list remains updated and terminated instances are continually filtered out.
Q5. Can the code provided be modified for specific use cases, and how?
A: Yes, the code provided can be modified for specific use cases. The "filters" variable in the code can be customized according to the requirements. For example, if you want to hide instances in a particular VPC, you can add a filter for "vpc-id". Similarly, if you want to hide instances with specific tags, you can add filters for the tag key and value.
Tag
"FILTERING"