AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK或UpdateMaintenanceWindow
一起使用 CLI
以下代码示例演示如何使用 UpdateMaintenanceWindow
。
- CLI
-
- AWS CLI
-
示例 1:更新维护时段
以下
update-maintenance-window
示例更新了维护时段的名称。aws ssm update-maintenance-window \ --window-id
"mw-1a2b3c4d5e6f7g8h9"
\ --name"My-Renamed-MW"
输出:
{ "Cutoff": 1, "Name": "My-Renamed-MW", "Schedule": "cron(0 16 ? * TUE *)", "Enabled": true, "AllowUnassociatedTargets": true, "WindowId": "mw-1a2b3c4d5e6f7g8h9", "Duration": 4 }
示例 2:禁用维护时段
以下
update-maintenance-window
示例禁用维护时段。aws ssm update-maintenance-window \ --window-id
"mw-1a2b3c4d5e6f7g8h9"
\ --no-enabled示例 3:启用维护时段
以下
update-maintenance-window
示例启用维护时段。aws ssm update-maintenance-window \ --window-id
"mw-1a2b3c4d5e6f7g8h9"
\ --enabled有关更多信息,请参阅《S AWS ystems Manager 用户指南》中的 “更新维护窗口” (AWS CLI)。
-
有关API详细信息,请参阅 “UpdateMaintenanceWindow AWS CLI
命令参考”。
-
- Java
-
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Updates an SSM maintenance window asynchronously. * * @param id The ID of the maintenance window to update. * @param name The new name for the maintenance window. * <p> * This method initiates an asynchronous request to update an SSM maintenance window. * If the request is successful, it prints a success message. * If an exception occurs, it handles the error appropriately. */ public void updateSSMMaintenanceWindow(String id, String name) throws SsmException { UpdateMaintenanceWindowRequest updateRequest = UpdateMaintenanceWindowRequest.builder() .windowId(id) .allowUnassociatedTargets(true) .duration(24) .enabled(true) .name(name) .schedule("cron(0 0 ? * MON *)") .build(); CompletableFuture<UpdateMaintenanceWindowResponse> future = getAsyncClient().updateMaintenanceWindow(updateRequest); future.whenComplete((response, ex) -> { if (response != null) { System.out.println("The SSM maintenance window was successfully updated"); } else { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new CompletionException(cause); } else { throw new RuntimeException(cause); } } }).join(); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 UpdateMaintenanceWindow” 中的。
-
- JavaScript
-
- SDK对于 JavaScript (v3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import { UpdateMaintenanceWindowCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Update an SSM maintenance window. * @param {{ windowId: string, allowUnassociatedTargets?: boolean, duration?: number, enabled?: boolean, name?: string, schedule?: string }} */ export const main = async ({ windowId, allowUnassociatedTargets = undefined, //Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets. duration = undefined, //The duration of the maintenance window in hours. enabled = undefined, name = undefined, schedule = undefined, //The schedule of the maintenance window in the form of a cron or rate expression. }) => { const client = new SSMClient({}); try { const { opsItemArn, opsItemId } = await client.send( new UpdateMaintenanceWindowCommand({ WindowId: windowId, AllowUnassociatedTargets: allowUnassociatedTargets, Duration: duration, Enabled: enabled, Name: name, Schedule: schedule, }), ); console.log("Maintenance window updated."); return { OpsItemArn: opsItemArn, OpsItemId: opsItemId }; } catch (caught) { if (caught instanceof Error && caught.name === "ValidationError") { console.warn(`${caught.message}. Are these values correct?`); } else { throw caught; } } };
-
有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 UpdateMaintenanceWindow” 中的。
-
- PowerShell
-
- 用于 PowerShell
-
示例 1:此示例更新维护时段的名称。
Update-SSMMaintenanceWindow -WindowId "mw-03eb9db42890fb82d" -Name "My-Renamed-MW"
输出:
AllowUnassociatedTargets : False Cutoff : 1 Duration : 2 Enabled : True Name : My-Renamed-MW Schedule : cron(0 */30 * * * ? *) WindowId : mw-03eb9db42890fb82d
示例 2:此示例启用维护时段。
Update-SSMMaintenanceWindow -WindowId "mw-03eb9db42890fb82d" -Enabled $true
输出:
AllowUnassociatedTargets : False Cutoff : 1 Duration : 2 Enabled : True Name : My-Renamed-MW Schedule : cron(0 */30 * * * ? *) WindowId : mw-03eb9db42890fb82d
示例 3:此示例禁用维护时段。
Update-SSMMaintenanceWindow -WindowId "mw-03eb9db42890fb82d" -Enabled $false
输出:
AllowUnassociatedTargets : False Cutoff : 1 Duration : 2 Enabled : False Name : My-Renamed-MW Schedule : cron(0 */30 * * * ? *) WindowId : mw-03eb9db42890fb82d
-
有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考UpdateMaintenanceWindow中的。
-
- 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 update( self, name, enabled, schedule, duration, cutoff, allow_unassociated_targets ): """ Update an AWS Systems Manager maintenance window. :param name: The name of the maintenance window. :param enabled: Whether the maintenance window is enabled to run on managed nodes. :param schedule: The schedule of the maintenance window. :param duration: The duration of the maintenance window. :param cutoff: The cutoff time of the maintenance window. :param allow_unassociated_targets: Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets. """ try: self.ssm_client.update_maintenance_window( WindowId=self.window_id, Name=name, Enabled=enabled, Schedule=schedule, Duration=duration, Cutoff=cutoff, AllowUnassociatedTargets=allow_unassociated_targets, ) self.name = name logger.info("Updated maintenance window %s.", self.window_id) except ParamValidationError as error: logger.error( "Parameter validation error when trying to update maintenance window %s. Here's why: %s", self.window_id, error, ) raise except ClientError as err: logger.error( "Couldn't update maintenance window %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
-
有关API详细信息,请参阅UpdateMaintenanceWindow中的 AWS SDKPython (Boto3) API 参考。
-