Gunakan CreateOpsItem dengan AWS SDK atau CLI - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan CreateOpsItem dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanCreateOpsItem.

Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:

CLI
AWS CLI

Untuk membuat OpsItems

create-ops-itemContoh berikut menggunakan kunci /aws/resources OperationalData untuk membuat OpsItem dengan sumber daya terkait 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"

Output:

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

Untuk informasi selengkapnya, lihat Membuat OpsItems di Panduan Pengguna AWS Systems Manager.

  • Untuk API detailnya, lihat CreateOpsItemdi Referensi AWS CLI Perintah.

Java
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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); } } }
  • Untuk API detailnya, lihat CreateOpsItemdi AWS SDK for Java 2.x APIReferensi.

Python
SDKuntuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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