Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Contoh Systems Manager menggunakan SDK untuk Python (Boto3)
Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for Python (Boto3) with Systems Manager.
Dasar-dasar adalah contoh kode yang menunjukkan kepada Anda bagaimana melakukan operasi penting dalam suatu layanan.
Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.
Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.
Memulai
Contoh kode berikut menunjukkan cara memulai menggunakan Systems Manager.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. import boto3 from botocore.exceptions import ClientError def hello_systems_manager(ssm_client): """ Use the AWS SDK for Python (Boto3) to create an AWS Systems Manager client and list the first 5 documents in your account. This example uses the default settings specified in your shared credentials and config files. :param ssm_client: A Boto3 AWS Systems Manager Client object. This object wraps the low-level AWS Systems Manager service API. """ print("Hello, AWS Systems Manager! Let's list some of your documents:\n") paginator = ssm_client.get_paginator("list_documents") page_iterator = paginator.paginate(PaginationConfig={"MaxItems": 5}) for page in page_iterator: for document in page["DocumentIdentifiers"]: print(f" {document['Name']}") if __name__ == "__main__": try: hello_systems_manager(boto3.client("ssm")) except ClientError as err: print("Hello systems manager had an error.") print(err.response["Error"]["Code"]) print(err.response["Error"]["Message"])
-
Untuk API detailnya, lihat ListDocuments AWSSDKReferensi Python (Boto3). API
-
Hal-hal mendasar
Contoh kode berikut menunjukkan cara bekerja dengan jendela pemeliharaan Systems Manager, dokumen, dan OpsItems.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. Jalankan skenario interaktif di penggugah/prompt perintah.
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.")
Tentukan kelas yang membungkus tindakan dokumen dan perintah.
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
Tentukan kelas yang membungkus tindakan item ops.
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
Tentukan kelas yang membungkus tindakan jendela pemeliharaan.
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
-
Untuk API detailnya, lihat topik berikut AWS SDKuntuk Referensi Python (Boto3). API
-
Tindakan
Contoh kode berikut menunjukkan cara menggunakanCreateDocument
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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
-
Untuk API detailnya, lihat CreateDocument AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanCreateMaintenanceWindow
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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 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
-
Untuk API detailnya, lihat CreateMaintenanceWindow AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanCreateOpsItem
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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
-
Untuk API detailnya, lihat CreateOpsItem AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanDeleteDocument
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat DeleteDocument AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanDeleteMaintenanceWindow
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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
-
Untuk API detailnya, lihat DeleteMaintenanceWindow AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanDeleteOpsItem
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat DeleteOpsItem AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanDescribeOpsItems
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat DescribeOpsItems AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanListCommandInvocations
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat ListCommandInvocations AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanSendCommand
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat SendCommand AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanUpdateMaintenanceWindow
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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
-
Untuk API detailnya, lihat UpdateMaintenanceWindow AWSSDKReferensi Python (Boto3). API
-
Contoh kode berikut menunjukkan cara menggunakanUpdateOpsItem
.
- SDKuntuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. 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 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
-
Untuk API detailnya, lihat UpdateOpsItem AWSSDKReferensi Python (Boto3). API
-