CreateOpsItem 搭配 AWS SDK或 使用 CLI - AWS Systems Manager

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateOpsItem 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 CreateOpsItem

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

CLI
AWS CLI

若要建立 OpsItems

下列create-ops-item範例使用 中的 /aws/resources 金鑰 OperationalData 來建立 OpsItem 具有 Amazon DynamoDB 相關資源的 。

aws ssm create-ops-item \ --title "EC2 instance disk full" \ --description "Log clean up may have failed which caused the disk to be full" \ --priority 2 \ --source ec2 \ --operational-data '{"/aws/resources":{"Value":"[{\"arn\": \"arn:aws:dynamodb:us-west-2:12345678:table/OpsItems\"}]","Type":"SearchableString"}}' \ --notifications Arn="arn:aws:sns:us-west-2:12345678:TestUser"

輸出:

{ "OpsItemId": "oi-1a2b3c4d5e6f" }

如需詳細資訊,請參閱 AWS Systems Manager 使用者指南中的建立 OpsItems

  • 如需API詳細資訊,請參閱 命令參考 CreateOpsItem中的 。 AWS CLI

Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Creates an SSM OpsItem asynchronously. * * @param title The title of the OpsItem. * @param source The source of the OpsItem. * @param category The category of the OpsItem. * @param severity The severity of the OpsItem. * @return The ID of the created OpsItem. * <p> * This method initiates an asynchronous request to create an SSM OpsItem. * If the request is successful, it returns the OpsItem ID. * If an exception occurs, it handles the error appropriately. */ public String createSSMOpsItem(String title, String source, String category, String severity) { CreateOpsItemRequest opsItemRequest = CreateOpsItemRequest.builder() .description("Created by the SSM Java API") .title(title) .source(source) .category(category) .severity(severity) .build(); CompletableFuture<CreateOpsItemResponse> future = getAsyncClient().createOpsItem(opsItemRequest); try { CreateOpsItemResponse response = future.join(); return response.opsItemId(); } catch (CompletionException e) { Throwable cause = e.getCause(); if (cause instanceof SsmException) { throw (SsmException) cause; } else { throw new RuntimeException(cause); } } }
  • 如需API詳細資訊,請參閱 參考 CreateOpsItem中的 。 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import { CreateOpsItemCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "util"; /** * Create an SSM OpsItem. * @param {{ title: string, source: string, category?: string, severity?: string }} */ export const main = async ({ title, source, category = undefined, severity = undefined, }) => { const client = new SSMClient({}); try { const { opsItemArn, opsItemId } = await client.send( new CreateOpsItemCommand({ Title: title, Source: source, // The origin of the OpsItem, such as Amazon EC2 or Systems Manager. Category: category, Severity: severity, }), ); console.log("Ops item created with id: " + opsItemId); return { OpsItemArn: opsItemArn, OpsItemId: opsItemId }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide these values?`); } else { throw caught; } } };
  • 如需API詳細資訊,請參閱 參考 CreateOpsItem中的 。 AWS SDK for JavaScript API

Python
SDK for Python (Boto3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 create(self, title, source, category, severity, description): """ Create an OpsItem :param title: The OpsItem title. :param source: The OpsItem source. :param category: The OpsItem category. :param severity: The OpsItem severity. :param description: The OpsItem description. """ try: response = self.ssm_client.create_ops_item( Title=title, Source=source, Category=category, Severity=severity, Description=description, ) self.id = response["OpsItemId"] except self.ssm_client.exceptions.OpsItemLimitExceededException as err: logger.error( "Couldn't create ops item because you have exceeded your open OpsItem limit. " "Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise except ClientError as err: logger.error( "Couldn't create ops item %s. Here's why: %s: %s", title, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 如需API詳細資訊,請參閱 CreateOpsItem 中的 AWS SDK for Python (Boto3) API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 搭配 使用 Systems Manager AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。