Use DescribeOpsItems with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use DescribeOpsItems with an AWS SDK or CLI

The following code examples show how to use DescribeOpsItems.

CLI
AWS CLI

To list a set of OpsItems

The following describe-ops-items example displays a list of all open OpsItems in your AWS account.

aws ssm describe-ops-items \ --ops-item-filters "Key=Status,Values=Open,Operator=Equal"

Output:

{ "OpsItemSummaries": [ { "CreatedBy": "arn:aws:sts::111222333444:assumed-role/OpsItem-CWE-Role/fbf77cbe264a33509569f23e4EXAMPLE", "CreatedTime": "2020-03-14T17:02:46.375000-07:00", "LastModifiedBy": "arn:aws:sts::111222333444:assumed-role/OpsItem-CWE-Role/fbf77cbe264a33509569f23e4EXAMPLE", "LastModifiedTime": "2020-03-14T17:02:46.375000-07:00", "Source": "SSM", "Status": "Open", "OpsItemId": "oi-7cfc5EXAMPLE", "Title": "SSM Maintenance Window execution failed", "OperationalData": { "/aws/dedup": { "Value": "{\"dedupString\":\"SSMOpsItems-SSM-maintenance-window-execution-failed\"}", "Type": "SearchableString" }, "/aws/resources": { "Value": "[{\"arn\":\"arn:aws:ssm:us-east-2:111222333444:maintenancewindow/mw-034093d322EXAMPLE\"}]", "Type": "SearchableString" } }, "Category": "Availability", "Severity": "3" }, { "CreatedBy": "arn:aws:sts::1112223233444:assumed-role/OpsItem-CWE-Role/fbf77cbe264a33509569f23e4EXAMPLE", "CreatedTime": "2020-02-26T11:43:15.426000-08:00", "LastModifiedBy": "arn:aws:sts::111222333444:assumed-role/OpsItem-CWE-Role/fbf77cbe264a33509569f23e4EXAMPLE", "LastModifiedTime": "2020-02-26T11:43:15.426000-08:00", "Source": "EC2", "Status": "Open", "OpsItemId": "oi-6f966EXAMPLE", "Title": "EC2 instance stopped", "OperationalData": { "/aws/automations": { "Value": "[ { \"automationType\": \"AWS:SSM:Automation\", \"automationId\": \"AWS-RestartEC2Instance\" } ]", "Type": "SearchableString" }, "/aws/dedup": { "Value": "{\"dedupString\":\"SSMOpsItems-EC2-instance-stopped\"}", "Type": "SearchableString" }, "/aws/resources": { "Value": "[{\"arn\":\"arn:aws:ec2:us-east-2:111222333444:instance/i-0beccfbc02EXAMPLE\"}]", "Type": "SearchableString" } }, "Category": "Availability", "Severity": "3" } ] }

For more information, see Working with OpsItems in the AWS Systems Manager User Guide.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Describes AWS SSM OpsItems asynchronously. * * @param key The key to filter OpsItems by (e.g., OPS_ITEM_ID). * * This method initiates an asynchronous request to describe SSM OpsItems. * If the request is successful, it prints the title and status of each OpsItem. * If an exception occurs, it handles the error appropriately. */ public void describeOpsItems(String key) { OpsItemFilter filter = OpsItemFilter.builder() .key(OpsItemFilterKey.OPS_ITEM_ID) .values(key) .operator(OpsItemFilterOperator.EQUAL) .build(); DescribeOpsItemsRequest itemsRequest = DescribeOpsItemsRequest.builder() .maxResults(10) .opsItemFilters(filter) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().describeOpsItems(itemsRequest) .thenAccept(itemsResponse -> { List<OpsItemSummary> items = itemsResponse.opsItemSummaries(); for (OpsItemSummary item : items) { System.out.println("The item title is " + item.title() + " and the status is " + item.status().toString()); } }) .exceptionally(ex -> { throw new CompletionException(ex); }).join(); }).exceptionally(ex -> { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new RuntimeException("SSM error: " + cause.getMessage(), cause); } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } }); try { future.join(); } catch (CompletionException ex) { throw ex.getCause() instanceof RuntimeException ? (RuntimeException) ex.getCause() : ex; } }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import { OpsItemFilterOperator, OpsItemFilterKey, paginateDescribeOpsItems, SSMClient, } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Describe SSM OpsItems. * @param {{ opsItemId: string }} */ export const main = async ({ opsItemId }) => { const client = new SSMClient({}); try { const describeOpsItemsPaginated = []; for await (const page of paginateDescribeOpsItems( { client }, { OpsItemFilters: { Key: OpsItemFilterKey.OPSITEM_ID, Operator: OpsItemFilterOperator.EQUAL, Values: opsItemId, }, }, )) { describeOpsItemsPaginated.push(...page.OpsItemSummaries); } console.log("Here are the ops items:"); console.log(describeOpsItemsPaginated); return { OpsItemSummaries: describeOpsItemsPaginated }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } throw caught; } };
  • For API details, see DescribeOpsItems in AWS SDK for JavaScript API Reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

class OpsItemWrapper: """Encapsulates AWS Systems Manager OpsItem actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.id = None @classmethod def from_client(cls): """ :return: A OpsItemWrapper instance. """ ssm_client = boto3.client("ssm") return cls(ssm_client) def describe(self): """ Describe an OpsItem. """ try: paginator = self.ssm_client.get_paginator("describe_ops_items") ops_items = [] for page in paginator.paginate( OpsItemFilters=[ {"Key": "OpsItemId", "Values": [self.id], "Operator": "Equal"} ] ): ops_items.extend(page["OpsItemSummaries"]) for item in ops_items: print( f"The item title is {item['Title']} and the status is {item['Status']}" ) return len(ops_items) > 0 except ClientError as err: logger.error( "Couldn't describe ops item %s. Here's why: %s: %s", self.id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • For API details, see DescribeOpsItems in AWS SDK for Python (Boto3) API Reference.