将 CreateMaintenanceWindow
与 Amazon SDK 或 CLI 配合使用
以下代码示例演示如何使用 CreateMaintenanceWindow
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- CLI
-
- Amazon CLI
-
示例 1:创建维护时段
以下
create-maintenance-window
示例创建一个新的维护时段,每五分钟执行一次,最多持续两个小时(根据需要),防止新任务在维护时段执行结束后的一小时内启动,允许未关联的目标(您尚未向维护时段注册的实例),并通过使用自定义标签表明其创建者打算在教程中进行使用。aws ssm create-maintenance-window \ --name "My-Tutorial-Maintenance-Window" \ --schedule "rate(5 minutes)" \ --duration 2 --cutoff 1 \ --allow-unassociated-targets \ --tags "Key=Purpose,Value=Tutorial"
输出:
{ "WindowId": "mw-0c50858d01EXAMPLE" }
示例 2:创建仅运行一次的维护时段
以下
create-maintenance-window
示例创建了一个仅在指定日期和时间运行一次的新维护时段。aws ssm create-maintenance-window \ --name My-One-Time-Maintenance-Window \ --schedule "at(2020-05-14T15:55:00)" \ --duration 5 \ --cutoff 2 \ --allow-unassociated-targets \ --tags "Key=Environment,Value=Production"
输出:
{ "WindowId": "mw-01234567890abcdef" }
有关更多信息,请参阅《Amazon Systems Manager 用户指南》中的维护时段。
-
有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 CreateMaintenanceWindow
。
-
- Java
-
- SDK for Java 2.x
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 public static String createMaintenanceWindow(SsmClient ssmClient, String winName) { CreateMaintenanceWindowRequest request = CreateMaintenanceWindowRequest.builder() .name(winName) .description("This is my maintenance window") .allowUnassociatedTargets(true) .duration(2) .cutoff(1) .schedule("cron(0 10 ? * MON-FRI *)") .build(); try { CreateMaintenanceWindowResponse response = ssmClient.createMaintenanceWindow(request); String maintenanceWindowId = response.windowId(); System.out.println("The maintenance window id is " + maintenanceWindowId); return maintenanceWindowId; } catch (DocumentAlreadyExistsException e) { System.err.println("The maintenance window already exists. Moving on."); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } MaintenanceWindowFilter filter = MaintenanceWindowFilter.builder() .key("name") .values(winName) .build(); DescribeMaintenanceWindowsRequest winRequest = DescribeMaintenanceWindowsRequest.builder() .filters(filter) .build(); String windowId = ""; DescribeMaintenanceWindowsResponse response = ssmClient.describeMaintenanceWindows(winRequest); List<MaintenanceWindowIdentity> windows = response.windowIdentities(); if (!windows.isEmpty()) { windowId = windows.get(0).windowId(); System.out.println("Window ID: " + windowId); } else { System.out.println("Window not found."); } return windowId; }
-
有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API Reference》中的 CreateMaintenanceWindow。
-
- PowerShell
-
- 适用于 PowerShell 的工具
-
示例 1:此示例使用指定名称创建了一个新的维护时段,该时段在每周二下午 4 点运行 4 小时,中断 1 小时,且允许未关联的目标。
New-SSMMaintenanceWindow -Name "MyMaintenanceWindow" -Duration 4 -Cutoff 1 -AllowUnassociatedTarget $true -Schedule "cron(0 16 ? * TUE *)"
输出:
mw-03eb53e1ea7383998
-
有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet Reference》中的 CreateMaintenanceWindow。
-
- Python
-
- SDK for Python(Boto3)
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 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 create(self, name, schedule, duration, cutoff, allow_unassociated_targets): """ Create an AWS Systems Manager maintenance window. :param name: The name of the maintenance window. :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: response = self.ssm_client.create_maintenance_window( Name=name, Schedule=schedule, Duration=duration, Cutoff=cutoff, AllowUnassociatedTargets=allow_unassociated_targets, ) self.window_id = response["WindowId"] self.name = name logger.info("Created maintenance window %s.", self.window_id) except ParamValidationError as error: logger.error( "Parameter validation error when trying to create maintenance window %s. Here's why: %s", self.window_id, error, ) raise except ClientError as err: logger.error( "Couldn't create maintenance window %s. Here's why: %s: %s", name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
-
有关 API 详细信息,请参阅《Amazon SDK for Python (Boto3) API 参考》中的 CreateMaintenanceWindow。
-
有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 Systems Manager 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。