Get started with Systems Manager using an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Get started with Systems Manager using an AWS SDK

The following code examples show how to work with Systems Manager maintenance windows, documents, and OpsItems.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ssm.SsmClient; import software.amazon.awssdk.services.ssm.model.CommandInvocation; import software.amazon.awssdk.services.ssm.model.CommandInvocationStatus; import software.amazon.awssdk.services.ssm.model.CreateDocumentRequest; import software.amazon.awssdk.services.ssm.model.CreateDocumentResponse; import software.amazon.awssdk.services.ssm.model.CreateMaintenanceWindowRequest; import software.amazon.awssdk.services.ssm.model.CreateMaintenanceWindowResponse; import software.amazon.awssdk.services.ssm.model.CreateOpsItemRequest; import software.amazon.awssdk.services.ssm.model.CreateOpsItemResponse; import software.amazon.awssdk.services.ssm.model.DeleteDocumentRequest; import software.amazon.awssdk.services.ssm.model.DeleteMaintenanceWindowRequest; import software.amazon.awssdk.services.ssm.model.DeleteOpsItemRequest; import software.amazon.awssdk.services.ssm.model.DescribeDocumentRequest; import software.amazon.awssdk.services.ssm.model.DescribeDocumentResponse; import software.amazon.awssdk.services.ssm.model.DescribeMaintenanceWindowsRequest; import software.amazon.awssdk.services.ssm.model.DescribeMaintenanceWindowsResponse; import software.amazon.awssdk.services.ssm.model.DescribeOpsItemsRequest; import software.amazon.awssdk.services.ssm.model.DescribeOpsItemsResponse; import software.amazon.awssdk.services.ssm.model.DocumentAlreadyExistsException; import software.amazon.awssdk.services.ssm.model.DocumentType; import software.amazon.awssdk.services.ssm.model.GetCommandInvocationRequest; import software.amazon.awssdk.services.ssm.model.GetCommandInvocationResponse; import software.amazon.awssdk.services.ssm.model.GetOpsItemRequest; import software.amazon.awssdk.services.ssm.model.GetOpsItemResponse; import software.amazon.awssdk.services.ssm.model.ListCommandInvocationsRequest; import software.amazon.awssdk.services.ssm.model.ListCommandInvocationsResponse; import software.amazon.awssdk.services.ssm.model.MaintenanceWindowFilter; import software.amazon.awssdk.services.ssm.model.MaintenanceWindowIdentity; import software.amazon.awssdk.services.ssm.model.OpsItemDataValue; import software.amazon.awssdk.services.ssm.model.OpsItemFilter; import software.amazon.awssdk.services.ssm.model.OpsItemFilterKey; import software.amazon.awssdk.services.ssm.model.OpsItemFilterOperator; import software.amazon.awssdk.services.ssm.model.OpsItemStatus; import software.amazon.awssdk.services.ssm.model.OpsItemSummary; import software.amazon.awssdk.services.ssm.model.SendCommandRequest; import software.amazon.awssdk.services.ssm.model.SendCommandResponse; import software.amazon.awssdk.services.ssm.model.SsmException; import software.amazon.awssdk.services.ssm.model.UpdateMaintenanceWindowRequest; import software.amazon.awssdk.services.ssm.model.UpdateOpsItemRequest; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.concurrent.TimeUnit; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html * * * This Java program performs these tasks: * 1. Creates an AWS Systems Manager maintenance window with a default name or a user-provided name. * 2. Modifies the maintenance window schedule. * 3. Creates a Systems Manager document with a default name or a user-provided name. * 4. Sends a command to a specified EC2 instance using the created Systems Manager document and displays the time when the command was invoked. * 5. Creates a Systems Manager OpsItem with a predefined title, source, category, and severity. * 6. Updates and resolves the created OpsItem. * 7. Deletes the Systems Manager maintenance window, OpsItem, and document. */ public class SSMScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws InterruptedException { String usage = """ Usage: <instanceId> <title> <source> <category> <severity> Where: instanceId - The Amazon EC2 Linux/UNIX instance Id that AWS Systems Manager uses (ie, i-0149338494ed95f06). title - The title of the parameter (default is Disk Space Alert). source - The source of the parameter (default is EC2). category - The category of the parameter. Valid values are 'Availability', 'Cost', 'Performance', 'Recovery', 'Security' (default is Performance). severity - The severity of the parameter. Severity should be a number from 1 to 4 (default is 2). """; if (args.length != 1) { System.out.println(usage); System.exit(1); } Scanner scanner = new Scanner(System.in); String documentName; String windowName; String instanceId = args[0]; String title = "Disk Space Alert" ; String source = "EC2" ; String category = "Performance" ; String severity = "2" ; Region region = Region.US_EAST_1; SsmClient ssmClient = SsmClient.builder() .region(region) .build(); System.out.println(DASHES); System.out.println(""" Welcome to the AWS Systems Manager SDK Getting Started scenario. This program demonstrates how to interact with Systems Manager using the AWS SDK for Java (v2). Systems Manager is the operations hub for your AWS applications and resources and a secure end-to-end management solution. The program's primary functions include creating a maintenance window, creating a document, sending a command to a document, listing documents, listing commands, creating an OpsItem, modifying an OpsItem, and deleting Systems Manager resources. Upon completion of the program, all AWS resources are cleaned up. Let's get started... Please hit Enter """); scanner.nextLine(); System.out.println(DASHES); System.out.println("Create a Systems Manager maintenance window."); System.out.println("Please enter the maintenance window name (default is ssm-maintenance-window):"); String win = scanner.nextLine(); windowName = win.isEmpty() ? "ssm-maintenance-window" : win; String winId = createMaintenanceWindow(ssmClient, windowName); System.out.println(DASHES); System.out.println("Modify the maintenance window by changing the schedule"); System.out.println("Please hit Enter"); scanner.nextLine(); updateSSMMaintenanceWindow(ssmClient, winId, windowName); System.out.println(DASHES); System.out.println("Create a document that defines the actions that Systems Manager performs on your EC2 instance."); System.out.println("Please enter the document name (default is ssmdocument):"); String doc = scanner.nextLine(); documentName = doc.isEmpty() ? "ssmdocument" : doc; createSSMDoc(ssmClient, documentName); System.out.println("Now we are going to run a command on an EC2 instance that echoes 'Hello, world!'"); System.out.println("Please hit Enter"); scanner.nextLine(); String commandId = sendSSMCommand(ssmClient, documentName, instanceId); System.out.println(DASHES); System.out.println("Lets get the time when the specific command was sent to the specific managed node"); System.out.println("Please hit Enter"); scanner.nextLine(); displayCommands(ssmClient, commandId); System.out.println(DASHES); System.out.println(DASHES); System.out.println(""" Now we will create a Systems Manager OpsItem. An OpsItem is a feature provided by the Systems Manager service. It is a type of operational data item that allows you to manage and track various operational issues, events, or tasks within your AWS environment. You can create OpsItems to track and manage operational issues as they arise. For example, you could create an OpsItem whenever your application detects a critical error or an anomaly in your infrastructure. """); System.out.println("Please hit Enter"); scanner.nextLine(); String opsItemId = createSSMOpsItem(ssmClient, title, source, category, severity); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Now we will update the OpsItem "+opsItemId); System.out.println("Please hit Enter"); scanner.nextLine(); String description = "An update to "+opsItemId ; updateOpsItem(ssmClient, opsItemId, title, description); System.out.println("Now we will get the status of the OpsItem "+opsItemId); System.out.println("Please hit Enter"); scanner.nextLine(); describeOpsItems(ssmClient, opsItemId); System.out.println("Now we will resolve the OpsItem "+opsItemId); System.out.println("Please hit Enter"); scanner.nextLine(); resolveOpsItem(ssmClient, opsItemId); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Would you like to delete the Systems Manager resources? (y/n)"); String delAns = scanner.nextLine().trim(); if (delAns.equalsIgnoreCase("y")) { System.out.println("You selected to delete the resources."); System.out.print("Press Enter to continue..."); scanner.nextLine(); deleteOpsItem(ssmClient, opsItemId); deleteMaintenanceWindow(ssmClient, winId); deleteDoc(ssmClient, documentName); } else { System.out.println("The Systems Manager resources will not be deleted"); } System.out.println(DASHES); System.out.println("This concludes the Systems Manager SDK Getting Started scenario."); System.out.println(DASHES); } // Displays the date and time when the specific command was invoked. public static void displayCommands(SsmClient ssmClient, String commandId) { try { ListCommandInvocationsRequest commandInvocationsRequest = ListCommandInvocationsRequest.builder() .commandId(commandId) .build(); ListCommandInvocationsResponse response = ssmClient.listCommandInvocations(commandInvocationsRequest); List<CommandInvocation> commandList = response.commandInvocations(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); for (CommandInvocation invocation : commandList) { System.out.println("The time of the command invocation is " + formatter.format(invocation.requestedDateTime())); } } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } // Create an SSM OpsItem public static String createSSMOpsItem(SsmClient ssmClient, String title, String source, String category, String severity) { try { CreateOpsItemRequest opsItemRequest = CreateOpsItemRequest.builder() .description("Created by the Systems Manager Java API") .title(title) .source(source) .category(category) .severity(severity) .build(); CreateOpsItemResponse itemResponse = ssmClient.createOpsItem(opsItemRequest); return itemResponse.opsItemId(); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Update the AWS SSM OpsItem. public static void updateOpsItem(SsmClient ssmClient, String opsItemId, String title, String description) { Map<String, OpsItemDataValue> operationalData = new HashMap<>(); operationalData.put("key1", OpsItemDataValue.builder().value("value1").build()); operationalData.put("key2", OpsItemDataValue.builder().value("value2").build()); try { UpdateOpsItemRequest request = UpdateOpsItemRequest.builder() .opsItemId(opsItemId) .title(title) .operationalData(operationalData) .status(getOpsItem(ssmClient, opsItemId)) .description(description) .build(); ssmClient.updateOpsItem(request); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void resolveOpsItem(SsmClient ssmClient, String opsID) { try { UpdateOpsItemRequest opsItemRequest = UpdateOpsItemRequest.builder() .opsItemId(opsID) .status(OpsItemStatus.RESOLVED) .build(); ssmClient.updateOpsItem(opsItemRequest); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } // Gets a specific OpsItem. private static OpsItemStatus getOpsItem(SsmClient ssmClient, String opsItemId) { GetOpsItemRequest itemRequest = GetOpsItemRequest.builder() .opsItemId(opsItemId) .build(); try { GetOpsItemResponse response = ssmClient.getOpsItem(itemRequest); return response.opsItem().status(); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } return null; } // Sends a SSM command to a managed node. public static String sendSSMCommand(SsmClient ssmClient, String documentName, String instanceId) throws InterruptedException { // Before we use Document to send a command - make sure it is active. boolean isDocumentActive = false; DescribeDocumentRequest request = DescribeDocumentRequest.builder() .name(documentName) .build(); while (!isDocumentActive) { DescribeDocumentResponse response = ssmClient.describeDocument(request); String documentStatus = response.document().statusAsString(); if (documentStatus.equals("Active")) { System.out.println("The Systems Manager document is active and ready to use."); isDocumentActive = true; } else { System.out.println("The Systems Manager document is not active. Status: " + documentStatus); try { // Add a delay to avoid making too many requests. Thread.sleep(5000); // Wait for 5 seconds before checking again } catch (InterruptedException e) { e.printStackTrace(); } } } // Create the SendCommandRequest. SendCommandRequest commandRequest = SendCommandRequest.builder() .documentName(documentName) .instanceIds(instanceId) .build(); // Send the command. SendCommandResponse commandResponse = ssmClient.sendCommand(commandRequest); String commandId = commandResponse.command().commandId(); System.out.println("The command Id is " + commandId); // Wait for the command execution to complete. GetCommandInvocationRequest invocationRequest = GetCommandInvocationRequest.builder() .commandId(commandId) .instanceId(instanceId) .build(); System.out.println("Wait 5 secs"); TimeUnit.SECONDS.sleep(5); // Retrieve the command execution details. GetCommandInvocationResponse commandInvocationResponse = ssmClient.getCommandInvocation(invocationRequest); // Check the status of the command execution. CommandInvocationStatus status = commandInvocationResponse.status(); if (status == CommandInvocationStatus.SUCCESS) { System.out.println("Command execution successful."); } else { System.out.println("Command execution failed. Status: " + status); } return commandId; } // Deletes an AWS Systems Manager document. public static void deleteDoc(SsmClient ssmClient, String documentName) { try { DeleteDocumentRequest documentRequest = DeleteDocumentRequest.builder() .name(documentName) .build(); ssmClient.deleteDocument(documentRequest); System.out.println("The Systems Manager document was successfully deleted."); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void deleteMaintenanceWindow(SsmClient ssmClient, String winId) { try { DeleteMaintenanceWindowRequest windowRequest = DeleteMaintenanceWindowRequest.builder() .windowId(winId) .build(); ssmClient.deleteMaintenanceWindow(windowRequest); System.out.println("The maintenance window was successfully deleted."); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } // Update the maintenance window schedule public static void updateSSMMaintenanceWindow(SsmClient ssmClient, String id, String name) { try { UpdateMaintenanceWindowRequest updateRequest = UpdateMaintenanceWindowRequest.builder() .windowId(id) .allowUnassociatedTargets(true) .duration(24) .enabled(true) .name(name) .schedule("cron(0 0 ? * MON *)") .build(); ssmClient.updateMaintenanceWindow(updateRequest); System.out.println("The Systems Manager maintenance window was successfully updated."); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } 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; } // Create an AWS SSM document to use in this scenario. public static void createSSMDoc(SsmClient ssmClient, String docName) { // Create JSON for the content String jsonData = """ { "schemaVersion": "2.2", "description": "Run a simple shell command", "mainSteps": [ { "action": "aws:runShellScript", "name": "runEchoCommand", "inputs": { "runCommand": [ "echo 'Hello, world!'" ] } } ] } """; try { CreateDocumentRequest request = CreateDocumentRequest.builder() .content(jsonData) .name(docName) .documentType(DocumentType.COMMAND) .build(); // Create the document. CreateDocumentResponse response = ssmClient.createDocument(request); System.out.println("The status of the document is " + response.documentDescription().status()); } catch (DocumentAlreadyExistsException e) { System.err.println("The document already exists. Moving on." ); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void describeOpsItems(SsmClient ssmClient, String key) { try { OpsItemFilter filter = OpsItemFilter.builder() .key(OpsItemFilterKey.OPS_ITEM_ID) .values(key) .operator(OpsItemFilterOperator.EQUAL) .build(); DescribeOpsItemsRequest itemsRequest = DescribeOpsItemsRequest.builder() .maxResults(10) .opsItemFilters(filter) .build(); DescribeOpsItemsResponse itemsResponse = ssmClient.describeOpsItems(itemsRequest); List<OpsItemSummary> items = itemsResponse.opsItemSummaries(); for (OpsItemSummary item : items) { System.out.println("The item title is " + item.title() +" and the status is "+item.status().toString()); } } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void deleteOpsItem(SsmClient ssmClient, String opsId) { try { DeleteOpsItemRequest deleteOpsItemRequest = DeleteOpsItemRequest.builder() .opsItemId(opsId) .build(); ssmClient.deleteOpsItem(deleteOpsItemRequest); System.out.println(opsId +" Opsitem was deleted"); } catch (SsmException e) { System.err.println(e.getMessage()); System.exit(1); } } }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Run an interactive scenario at a command prompt.

class SystemsManagerScenario: """Runs an interactive scenario that shows how to get started using Amazon Systems Manager.""" def __init__(self, document_wrapper, maintenance_window_wrapper, ops_item_wrapper): """ :param document_wrapper: An object that wraps Systems Manager document functions. :param maintenance_window_wrapper: An object that wraps Systems Manager maintenance window functions. :param ops_item_wrapper: An object that wraps Systems Manager OpsItem functions. """ self.document_wrapper = document_wrapper self.maintenance_window_wrapper = maintenance_window_wrapper self.ops_item_wrapper = ops_item_wrapper def run(self): """Demonstrates how to use the AWS SDK for Python (Boto3) to get started with Systems Manager.""" try: print("-" * 88) print( """ Welcome to the AWS Systems Manager SDK Getting Started scenario. This program demonstrates how to interact with Systems Manager using the AWS SDK for Python (Boto3). Systems Manager is the operations hub for your AWS applications and resources and a secure end-to-end management solution. The program's primary functions include creating a maintenance window, creating a document, sending a command to a document, listing documents, listing commands, creating an OpsItem, modifying an OpsItem, and deleting Systems Manager resources. Upon completion of the program, all AWS resources are cleaned up. Let's get started...""" ) q.ask("Please hit Enter") print("-" * 88) print("Create a Systems Manager maintenance window.") maintenance_window_name = q.ask( "Please enter the maintenance window name (default is ssm-maintenance-window):", ) if not maintenance_window_name: maintenance_window_name = "ssm-maintenance-window" self.maintenance_window_wrapper.create( name=maintenance_window_name, schedule="cron(0 10 ? * MON-FRI *)", duration=2, cutoff=1, allow_unassociated_targets=True, ) print("-" * 88) print("Modify the maintenance window by changing the schedule") q.ask("Please hit Enter") self.maintenance_window_wrapper.update( name=maintenance_window_name, schedule="cron(0 0 ? * MON *)", duration=24, cutoff=1, allow_unassociated_targets=True, enabled=True, ) print("-" * 88) print( "Create a document that defines the actions that Systems Manager performs on your EC2 instance." ) document_name = q.ask( "Please enter the document name (default is ssmdocument):" ) if not document_name: document_name = "ssmdocument" self.document_wrapper.create( name=document_name, content=""" { "schemaVersion": "2.2", "description": "Run a simple shell command", "mainSteps": [ { "action": "aws:runShellScript", "name": "runEchoCommand", "inputs": { "runCommand": [ "echo 'Hello, world!'" ] } } ] } """, ) self.document_wrapper.wait_until_active() print( """ Now you have the option of running a command on an EC2 instance that echoes 'Hello, world!'. In order to run this command, you must provide the instance ID of a Linux EC2 instance. If you do not already have a running Linux EC2 instance in your account, you can create one using the AWS console. For information about creating an EC2 instance, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance-wizard.html. """ ) if q.ask( "Would you like to run a command on an EC2 instance? (y/n)", q.is_yesno, ): instance_id = q.ask( "Please enter the instance ID of the EC2 instance:", q.non_empty ) command_id = self.document_wrapper.send_command( instance_ids=[instance_id] ) self.document_wrapper.wait_command_executed( command_id=command_id, instance_id=instance_id ) print("-" * 88) print( "Lets get the time when the specific command was sent to the specific managed node" ) q.ask("Please hit Enter") self.document_wrapper.list_command_invocations(instance_id=instance_id) print("-" * 88) print("-" * 88) print( """ Now we will create a Systems Manager OpsItem. An OpsItem is a feature provided by the Systems Manager service. It is a type of operational data item that allows you to manage and track various operational issues, events, or tasks within your AWS environment. You can create OpsItems to track and manage operational issues as they arise. For example, you could create an OpsItem whenever your application detects a critical error or an anomaly in your infrastructure. """ ) q.ask("Please hit Enter") self.ops_item_wrapper.create( title="Disk Space Alert", description="Created by the Systems Manager Python (Boto3) API", source="EC2", category="Performance", severity="2", ) print("-" * 88) print("-" * 88) print(f"Now we will update the OpsItem {self.ops_item_wrapper.id}") q.ask("Please hit Enter") self.ops_item_wrapper.update( title="Disk Space Alert", description=f"An update to {self.ops_item_wrapper.id}", ) print( f"Now we will get the status of the OpsItem {self.ops_item_wrapper.id}" ) q.ask("Please hit Enter") # It may take a second for the ops item to be available counter = 0 while not self.ops_item_wrapper.describe() and counter < 5: counter += 1 time.sleep(1) print(f"Now we will resolve the OpsItem {self.ops_item_wrapper.id}") q.ask("Please hit Enter") self.ops_item_wrapper.update(status="Resolved") print("-" * 88) print("-" * 88) if q.ask( "Would you like to delete the Systems Manager resources? (y/n)", q.is_yesno, ): print("You selected to delete the resources.") self.cleanup() else: print("The Systems Manager resources will not be deleted") print("-" * 88) print("This concludes the Systems Manager SDK Getting Started scenario.") print("-" * 88) except Exception: self.cleanup() raise def cleanup(self): self.maintenance_window_wrapper.delete() self.ops_item_wrapper.delete() self.document_wrapper.delete() if __name__ == "__main__": try: scenario = SystemsManagerScenario( DocumentWrapper.from_client(), MaintenanceWindowWrapper.from_client(), OpsItemWrapper.from_client(), ) scenario.run() except Exception: logging.exception("Something went wrong with the demo.")

Define a class that wraps document and command actions.

class DocumentWrapper: """Encapsulates AWS Systems Manager Document actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.name = None @classmethod def from_client(cls): ssm_client = boto3.client("ssm") return cls(ssm_client) def create(self, content, name): """ Creates a document. :param content: The content of the document. :param name: The name of the document. """ try: self.ssm_client.create_document( Name=name, Content=content, DocumentType="Command" ) self.name = name except self.ssm_client.exceptions.DocumentAlreadyExists: print(f"Document {name} already exists.") self.name = name except ClientError as err: logger.error( "Couldn't create %s. Here's why: %s: %s", name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def delete(self): """ Deletes an AWS Systems Manager document. """ if self.name is None: return try: self.ssm_client.delete_document(Name=self.name) print(f"Deleted document {self.name}.") self.name = None except ClientError as err: logger.error( "Couldn't delete %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def send_command(self, instance_ids): """ Sends a command to one or more instances. :param instance_ids: The IDs of the instances to send the command to. :return: The ID of the command. """ try: response = self.ssm_client.send_command( InstanceIds=instance_ids, DocumentName=self.name, TimeoutSeconds=3600 ) return response["Command"]["CommandId"] except ClientError as err: logger.error( "Couldn't send command to %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def describe(self): """ Describes the document. :return: Document status. """ try: response = self.ssm_client.describe_document(Name=self.name) return response["Document"]["Status"] except ClientError as err: logger.error( "Couldn't get %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def wait_until_active(self, max_attempts=20, delay=5): """ Waits until the document is active. :param max_attempts: The maximum number of attempts for checking the status. :param delay: The delay in seconds between each check. """ attempt = 0 status = "" while attempt <= max_attempts: status = self.describe() if status == "Active": break attempt += 1 time.sleep(delay) if status != "Active": logger.error("Document is not active.") else: logger.info("Document is active.") def wait_command_executed(self, command_id, instance_id): """ Waits until the command is executed on the instance. :param command_id: The ID of the command. :param instance_id: The ID of the instance. """ waiter = self.ssm_client.get_waiter("command_executed") waiter.wait(CommandId=command_id, InstanceId=instance_id) def list_command_invocations(self, instance_id): """ Lists the commands for an instance. :param instance_id: The ID of the instance. :return: The list of commands. """ try: paginator = self.ssm_client.get_paginator("list_command_invocations") command_invocations = [] for page in paginator.paginate(InstanceId=instance_id): command_invocations.extend(page["CommandInvocations"]) num_of_commands = len(command_invocations) print( f"{num_of_commands} command invocation(s) found for instance {instance_id}." ) if num_of_commands > 10: print("Displaying the first 10 commands:") num_of_commands = 10 date_format = "%A, %d %B %Y %I:%M%p" for command in command_invocations[:num_of_commands]: print( f" The time of command invocation is {command['RequestedDateTime'].strftime(date_format)}" ) except ClientError as err: logger.error( "Couldn't list commands for %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

Define a class that wraps ops item actions.

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 def delete(self): """ Delete the OpsItem. """ if self.id is None: return try: self.ssm_client.delete_ops_item(OpsItemId=self.id) print(f"Deleted ops item with id {self.id}") self.id = None except ClientError as err: logger.error( "Couldn't delete ops item %s. Here's why: %s: %s", self.id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def describe(self): """ Describe an OpsItem. """ try: paginator = self.ssm_client.get_paginator("describe_ops_items") ops_items = [] for page in paginator.paginate( OpsItemFilters=[ {"Key": "OpsItemId", "Values": [self.id], "Operator": "Equal"} ] ): ops_items.extend(page["OpsItemSummaries"]) for item in ops_items: print( f"The item title is {item['Title']} and the status is {item['Status']}" ) return len(ops_items) > 0 except ClientError as err: logger.error( "Couldn't describe ops item %s. Here's why: %s: %s", self.id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def update(self, title=None, description=None, status=None): """ Update an OpsItem. :param title: The new OpsItem title. :param description: The new OpsItem description. :param status: The new OpsItem status. :return: """ args = dict(OpsItemId=self.id) if title is not None: args["Title"] = title if description is not None: args["Description"] = description if status is not None: args["Status"] = status try: self.ssm_client.update_ops_item(**args) except ClientError as err: logger.error( "Couldn't update ops item %s. Here's why: %s: %s", self.id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

Define a class that wraps maintenance window actions.

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 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 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