How can terminated instances be removed from AWS SSM's inventory?

Viewed 25

I'm trying to remove terminated managed nodes from SSM's inventory, but couldn't find any way to do so through either the CLI or through an SDK. Currently, when I run the command aws ssm get-inventory the resulting list contains a lot of entries of the following pattern:

{
    "Id": "i-0ffeeb0756265c10f",
    "Data": {
        "AWS:InstanceInformation": {
            "TypeName": "AWS:InstanceInformation",
            "SchemaVersion": "1.0",
            "CaptureTime": "2022-09-09T05:11:53Z",
            "Content": [
                {
                    "InstanceId": "i-0ffeeb0756265c10f",
                    "InstanceStatus": "Terminated"
                }
            ]
        }
    }
}

These instances also do not show up in managed nodes in the AWS console. Is there a way to remove these terminated instances from the SSM inventory? Thanks a lot!

1 Answers

aws ssm get-inventory can use --filters.

Key=string,Values=string,string,Type=string

For you:

Key=InstanceStatus,Values=Terminated,Type=NotEqual

So something like this should work:

aws ssm get-inventory --filters Key=InstanceStatus,Values=Terminated,Type=NotEqual

This is just what I get from docs here. I haven't tried it so may need some tweaking.

Related