import{ CreateMaintenanceWindowCommand, SSMClient } from"@aws-sdk/client-ssm";
import{ parseArgs } from"node:util";
/**
* Create an SSM maintenance window.
* @param {{ name: string, allowUnassociatedTargets: boolean, duration: number, cutoff: number, schedule: string, description?: string }}
*/exportconst main = async ({
name,
allowUnassociatedTargets, // Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets.
duration, // The duration of the maintenance window in hours.
cutoff, // The number of hours before the end of the maintenance window that Amazon Web Services Systems Manager stops scheduling new tasks for execution.
schedule, // The schedule of the maintenance window in the form of a cron or rate expression.
description = undefined,
}) => {const client = new SSMClient({});
try{const{ windowId } = await client.send(
new CreateMaintenanceWindowCommand({Name: name,
Description: description,
AllowUnassociatedTargets: allowUnassociatedTargets, // Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets.Duration: duration, // The duration of the maintenance window in hours.Cutoff: cutoff, // The number of hours before the end of the maintenance window that Amazon Web Services Systems Manager stops scheduling new tasks for execution.Schedule: schedule, // The schedule of the maintenance window in the form of a cron or rate expression.
}),
);
console.log(`Maintenance window created with Id: ${windowId}`);
return{WindowId: windowId };
} catch (caught) {if (caught instanceofError && caught.name === "MissingParameter") {console.warn(`${caught.message}. Did you provide these values?`);
} else{throw caught;
}
}
};
classMaintenanceWindowWrapper:"""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 @classmethoddeffrom_client(cls):
ssm_client = boto3.client("ssm")
return cls(ssm_client)
defcreate(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,
)
raiseexcept 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