与 AWS SDK或DeleteMaintenanceWindow一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或DeleteMaintenanceWindow一起使用 CLI

以下代码示例演示如何使用 DeleteMaintenanceWindow

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

CLI
AWS CLI

删除维护时段

delete-maintenance-window 示例删除指定的维护时段。

aws ssm delete-maintenance-window \ --window-id "mw-1a2b3c4d5e6f7g8h9"

输出:

{ "WindowId":"mw-1a2b3c4d5e6f7g8h9" }

有关更多信息,请参阅《S AWS ystems Manager 用户指南》中的 “删除维护窗口” (AWS CLI)

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Deletes an AWS SSM Maintenance Window asynchronously. * * @param winId The ID of the Maintenance Window to delete. * <p> * This method initiates an asynchronous request to delete an SSM Maintenance Window. * If an exception occurs, it handles the error appropriately. */ public void deleteMaintenanceWindow(String winId) { DeleteMaintenanceWindowRequest windowRequest = DeleteMaintenanceWindowRequest.builder() .windowId(winId) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().deleteMaintenanceWindow(windowRequest) .thenAccept(response -> { System.out.println("The maintenance window was successfully deleted."); }) .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对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import { DeleteMaintenanceWindowCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Delete an SSM maintenance window. * @param {{ windowId: string }} */ export const main = async ({ windowId }) => { const client = new SSMClient({}); try { await client.send( new DeleteMaintenanceWindowCommand({ WindowId: windowId }), ); console.log(`Maintenance window '${windowId}' deleted.`); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };
PowerShell
用于 PowerShell

示例 1:此示例删除维护时段。

Remove-SSMMaintenanceWindow -WindowId "mw-06d59c1a07c022145"

输出:

mw-06d59c1a07c022145
Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class MaintenanceWindowWrapper: """Encapsulates AWS Systems Manager maintenance window actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.window_id = None self.name = None @classmethod def from_client(cls): ssm_client = boto3.client("ssm") return cls(ssm_client) def delete(self): """ Delete the associated AWS Systems Manager maintenance window. """ if self.window_id is None: return try: self.ssm_client.delete_maintenance_window(WindowId=self.window_id) logger.info("Deleted maintenance window %s.", self.window_id) print(f"Deleted maintenance window {self.name}") self.window_id = None except ClientError as err: logger.error( "Couldn't delete maintenance window %s. Here's why: %s: %s", self.window_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise