AWS Cloud Operations & Migrations Blog
Recommended practices for deleting custom inventory types and data using AWS Systems Manager
AWS Systems Manager Inventory provides an easy and secure way to collect metadata and insights from your managed instances. The service provides ready-to-use inventory gatherers for insights on installed applications, network configurations, file metadata, Windows updates, Windows services, Windows server roles/features, and Windows registry. In addition, you can use AWS Systems Manager Inventory to collect any other system property as Inventory, for example rack information on the system or results from a legacy application running on the managed instance.
Recently AWS Systems Manager added capabilities for deleting custom inventory types and deleting data associated with custom inventory types using the delete-inventory AWS CLI or the DeleteInventory API action. This comes along with the ability to get detailed status on the delete operation. The capabilities are integrated with Amazon CloudWatch Events for alarms and AWS CloudTrail for audit logs of the delete operations. Some typical use cases for using these capabilities include getting rid of custom inventory types created by mistake or for testing and cleaning up any Inventory data for certain custom inventory types for a variety of reasons.
In this blog post, I’ll walk you through some things to note about the new capabilities and our recommended practices so you can safely delete custom inventory types or associated data.
Things to note
- The capability to delete custom inventory types or data is available using the AWS CLI and API only.
- If you have Resource Data Sync set up, deleting inventory data for a custom inventory type doesn’t delete inventory data from your Amazon S3 bucket.
- After inventory data is deleted, you will not be able restore the data.
- Deleting a custom inventory type does not stop collection of other inventory types in the account.
- Deleted or disabled custom inventory types no longer count towards the maximum number of custom types allowed in the account.
1. Configure Amazon CloudWatch Events to receive notifications for delete operations
CloudWatch Events help you respond to state changes in your AWS resources. You can also create rules that match selected events and route rules to trigger actions. While deleting custom inventory types or data, we trigger events in Amazon CloudWatch for the following conditions:
- When inventory data for an instance gets deleted.
- When the delete inventory operation is complete.
- When one or more instances send inventory data for a custom inventory type that was already disabled.
We recommend that you add a CloudWatch rule in the same Region where you are performing the delete operation for all delete custom inventory operation events. Set a target action, such as sending email notifications. You can add a rule using the AWS Management Console, the AWS CLI or the Amazon CloudWatch Events API. For this blog post we will use the AWS Management Console.
- Go to the AWS Management Console and search for the Amazon CloudWatch console.
- In the CloudWatch console, on the left navigation pane, choose Events (or Rule if you already have created some Rules).
- Choose Create Rule. In Step 1 make the following choices:
- Choose Event Pattern.
- For Service name choose EC2 Simple Systems Manager (SSM).
- For Event Type choose All Events.
- On the right side, choose Add Target, then choose SNS Topic. This will show all SNS topics in your AWS account.
- Select the SNS topic that is configured to send you an email notification.
- Choose Configure details to define a name for the rule.
- Provide a name and choose Create Rule to complete the configuration.
Here is an example of a CloudWatch event detail that was triggered at the completion of a delete operation:
2. Decide what action you want the system to take if an instance sends inventory data for custom inventory type that was deleted
In case you delete a custom inventory type but your account still has managed instances that might collect and send data for the deleted custom inventory type, you have two options: 1) ignore any new data coming for a deleted inventory type or 2) recreate custom inventory type.
3. First understand what will get deleted
Before you delete a custom inventory type, perform a dry run (without deleting the data). This will help you understand what would get deleted if the operation had been run without using the dry-run option. You can execute the following command to use the dry-run option:
This returns information similar to the following indicating what might have been deleted if the command were run without the dry-run option:
4. Delete custom inventory type or data associated to a custom inventory type
Depending on the decision that you make in Step 2, you can execute the following commands using the AWS CLI for the delete operation to take place.
a) If you want to ignore any new data coming for a deleted inventory type data for the deleted type, execute the following command using the AWS CLI:
In this case, the system will return information similar to the following:
b) If you want to re-create the custom inventory type when a managed instance sends inventory type data for the deleted type, execute the following command using AWS CLI:
In this case the system will return information similar to the following:
5. Verify that the delete operation is complete
Depending on the amount of inventory data that your instances have collected using AWS Systems Manager, a delete operation could take several minutes to complete. While we do not expect DeleteInventory to cause notable impact to any instance or AWS Systems Manager operations, we recommend that you verify that the delete operation has successfully been completed. Also note that while the actual delete operation might take less time, we check the status every 10 minutes, so it might take up to 10 minutes for the describe-inventory-deletions action to show the status as complete.
You can check the status of a delete operation by using the describe-inventory-deletions CLI command. You can specify a deletion ID to view the status of a specific delete operation. Execute the following command to view the status of a deletion operation. (Note that the system returned the deletion ID in the delete-inventory summary in steps 4a and 4b.)
The system returns the latest status. If the delete operation has not yet finished, wait for few minutes and execute the command again. When a successful delete has been completed, the system returns information similar to the following.
Note that by default the AWS CLI output shows timestamps such as DeletionStartTime and LastStatusUpdateTime in epoch time. To view the AWS CLI output in a friendlier UTC time zone format perform the following steps:
- Open your AWS CLI config file. The default location for the AWS CLI Config file is ~/.aws/config. If you don’t find the file at this location, check the value of environment variable AWS_CONFIG_FILE and search in the location.
- Reformat the epoch time expression to the ISO8601 UTC time zone by adding or updating (if a value already exists) the value for cli_timestamp_format to iso8601, that is,
See the AWS CLI Command Reference for more details on AWS CLI general options.
In summary, before you delete a custom Inventory type or delete data associated with a custom inventory type, we recommend that you set up CloudWatch Event rules to get appropriate notifications or actions. Later, before performing the delete, first understand and confirm that you are ok with what the delete operation will delete, and then perform the delete operation. Also, when you are performing a delete operation for a custom type that collects the data from a large number of instances, wait for the delete to be completed before initiating other large-scale operations.
About the Author
Tanu Mutreja is a Sr. Product Manager for AWS Systems Manager. She loves to work on innovative products and solve customer problems with easy solutions and simple experiences.