使用 SDK 的 Amazon Systems Manager 的代码示例 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 SDK 的 Amazon Systems Manager 的代码示例

以下代码示例显示如何将 Systems Manager 与 Amazon 软件开发工具包(SDK)一起使用。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 Systems Manager 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。

开始使用

以下代码示例展示了如何开始使用 Systems Manager。

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ssm.SsmClient; import software.amazon.awssdk.services.ssm.model.DocumentFilter; import software.amazon.awssdk.services.ssm.model.ListDocumentsRequest; import software.amazon.awssdk.services.ssm.model.ListDocumentsResponse; public class HelloSSM { public static void main(String[] args) { final String usage = """ Usage: <awsAccount> Where: awsAccount - Your AWS Account number. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String awsAccount = args[0] ; Region region = Region.US_EAST_1; SsmClient ssmClient = SsmClient.builder() .region(region) .build(); listDocuments(ssmClient, awsAccount); } /* This code automatically fetches the next set of results using the `nextToken` and stops once the desired maxResults (20 in this case) have been reached. */ public static void listDocuments(SsmClient ssmClient, String awsAccount) { String nextToken = null; int totalDocumentsReturned = 0; int maxResults = 20; do { ListDocumentsRequest request = ListDocumentsRequest.builder() .documentFilterList( DocumentFilter.builder() .key("Owner") .value(awsAccount) .build() ) .maxResults(maxResults) .nextToken(nextToken) .build(); ListDocumentsResponse response = ssmClient.listDocuments(request); response.documentIdentifiers().forEach(identifier -> System.out.println("Document Name: " + identifier.name())); nextToken = response.nextToken(); totalDocumentsReturned += response.documentIdentifiers().size(); } while (nextToken != null && totalDocumentsReturned < maxResults); } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 listThings

Python
SDK for Python (Boto3)
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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"])
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 listThings

代码示例