SDK for JavaScript (v3)를 사용하는 Systems Manager 예제 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 참조 가이드에서는 버전 3(V3)의 모든 API 작업에 대해 AWS SDK for JavaScript 자세히 설명합니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

SDK for JavaScript (v3)를 사용하는 Systems Manager 예제

다음 코드 예제에서는 Systems Manager에서 AWS SDK for JavaScript (v3)를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.

시작

다음 코드 예제에서는 Systems Manager를 사용하여 시작하는 방법을 보여줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { paginateListDocuments, SSMClient } from "@aws-sdk/client-ssm"; // Call ListDocuments and display the result. export const main = async () => { const client = new SSMClient(); const listDocumentsPaginated = []; console.log( "Hello, AWS Systems Manager! Let's list some of your documents:\n", ); try { // The paginate function is a wrapper around the base command. const paginator = paginateListDocuments({ client }, { MaxResults: 5 }); for await (const page of paginator) { listDocumentsPaginated.push(...page.DocumentIdentifiers); } } catch (caught) { console.error(`There was a problem saying hello: ${caught.message}`); throw caught; } for (const { Name, DocumentFormat, CreatedDate } of listDocumentsPaginated) { console.log(`${Name} - ${DocumentFormat} - ${CreatedDate}`); } }; // Call function if run directly. import { fileURLToPath } from "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { main(); }
  • API 자세한 내용은 AWS SDK for JavaScript API 참조ListDocuments의 섹션을 참조하세요.

기본 사항

다음 코드 예제에서는 Systems Manager 유지 관리 기간, 문서 및 작업을 수행하는 방법을 보여줍니다 OpsItems.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { Scenario, ScenarioAction, ScenarioInput, ScenarioOutput, } from "@aws-doc-sdk-examples/lib/scenario/index.js"; import { fileURLToPath } from "node:url"; import { CreateDocumentCommand, CreateMaintenanceWindowCommand, CreateOpsItemCommand, DeleteDocumentCommand, DeleteMaintenanceWindowCommand, DeleteOpsItemCommand, DescribeOpsItemsCommand, DocumentAlreadyExists, OpsItemStatus, waitUntilCommandExecuted, CancelCommandCommand, paginateListCommandInvocations, SendCommandCommand, UpdateMaintenanceWindowCommand, UpdateOpsItemCommand, SSMClient, } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * @typedef {{ * ssmClient: import('@aws-sdk/client-ssm').SSMClient, * documentName?: string * maintenanceWindow?: string * winId?: int * ec2InstanceId?: string * requestedDateTime?: Date * opsItemId?: string * askToDeleteResources?: boolean * }} State */ const defaultMaintenanceWindow = "ssm-maintenance-window"; const defaultDocumentName = "ssmdocument"; // The timeout duration is highly dependent on the specific setup and environment necessary. This example handles only the most common error cases, and uses a much shorter duration than most productions systems would use. const COMMAND_TIMEOUT_DURATION_SECONDS = 30; // 30 seconds const pressEnter = new ScenarioInput("continue", "Press Enter to continue", { type: "confirm", }); const greet = new ScenarioOutput( "greet", `Welcome to the AWS Systems Manager SDK Getting Started scenario. This program demonstrates how to interact with Systems Manager using the AWS SDK for JavaScript V3. 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...`, { header: true }, ); const createMaintenanceWindow = new ScenarioOutput( "createMaintenanceWindow", "Step 1: Create a Systems Manager maintenance window.", ); const getMaintenanceWindow = new ScenarioInput( "maintenanceWindow", "Please enter the maintenance window name:", { type: "input", default: defaultMaintenanceWindow }, ); export const sdkCreateMaintenanceWindow = new ScenarioAction( "sdkCreateMaintenanceWindow", async (/** @type {State} */ state) => { try { const response = await state.ssmClient.send( new CreateMaintenanceWindowCommand({ Name: state.maintenanceWindow, Schedule: "cron(0 10 ? * MON-FRI *)", //The schedule of the maintenance window in the form of a cron or rate expression. Duration: 2, //The duration of the maintenance window in hours. Cutoff: 1, //The number of hours before the end of the maintenance window that Amazon Web Services Systems Manager stops scheduling new tasks for execution. AllowUnassociatedTargets: true, //Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets. }), ); state.winId = response.WindowId; } catch (caught) { console.error(caught.message); console.log( `An error occurred while creating the maintenance window. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const modifyMaintenanceWindow = new ScenarioOutput( "modifyMaintenanceWindow", "Modify the maintenance window by changing the schedule.", ); const sdkModifyMaintenanceWindow = new ScenarioAction( "sdkModifyMaintenanceWindow", async (/** @type {State} */ state) => { try { await state.ssmClient.send( new UpdateMaintenanceWindowCommand({ WindowId: state.winId, Schedule: "cron(0 0 ? * MON *)", }), ); } catch (caught) { console.error(caught.message); console.log( `An error occurred while modifying the maintenance window. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const createSystemsManagerActions = new ScenarioOutput( "createSystemsManagerActions", "Create a document that defines the actions that Systems Manager performs on your EC2 instance.", ); const getDocumentName = new ScenarioInput( "documentName", "Please enter the document: ", { type: "input", default: defaultDocumentName }, ); const sdkCreateSSMDoc = new ScenarioAction( "sdkCreateSSMDoc", async (/** @type {State} */ state) => { const contentData = `{ "schemaVersion": "2.2", "description": "Run a simple shell command", "mainSteps": [ { "action": "aws:runShellScript", "name": "runEchoCommand", "inputs": { "runCommand": [ "echo 'Hello, world!'" ] } } ] }`; try { await state.ssmClient.send( new CreateDocumentCommand({ Content: contentData, Name: state.documentName, DocumentType: "Command", }), ); } catch (caught) { console.log(`Exception type: (${typeof caught})`); if (caught instanceof DocumentAlreadyExists) { console.log("Document already exists. Continuing...\n"); } else { console.error(caught.message); console.log( `An error occurred while creating the document. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } } }, ); const ec2HelloWorld = new ScenarioOutput( "ec2HelloWorld", `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.`, ); const enterIdOrSkipEC2HelloWorld = new ScenarioInput( "enterIdOrSkipEC2HelloWorld", "Enter your EC2 InstanceId or press enter to skip this step: ", { type: "input", default: "" }, ); const sdkEC2HelloWorld = new ScenarioAction( "sdkEC2HelloWorld", async (/** @type {State} */ state) => { try { const response = await state.ssmClient.send( new SendCommandCommand({ DocumentName: state.documentName, InstanceIds: [state.ec2InstanceId], TimeoutSeconds: COMMAND_TIMEOUT_DURATION_SECONDS, }), ); state.CommandId = response.Command.CommandId; } catch (caught) { console.error(caught.message); console.log( `An error occurred while sending the command. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, { skipWhen: (/** @type {State} */ state) => state.enterIdOrSkipEC2HelloWorld === "", }, ); const sdkGetCommandTime = new ScenarioAction( "sdkGetCommandTime", async (/** @type {State} */ state) => { const listInvocationsPaginated = []; console.log( "Let's get the time when the specific command was sent to the specific managed node.", ); console.log( `First, we'll wait for the command to finish executing. This may take up to ${COMMAND_TIMEOUT_DURATION_SECONDS} seconds.`, ); const commandExecutedResult = waitUntilCommandExecuted( { client: state.ssmClient }, { CommandId: state.CommandId, InstanceId: state.ec2InstanceId, }, ); // This is necessary because the TimeoutSeconds of SendCommandCommand is only for the delivery, not execution. try { await new Promise((_, reject) => setTimeout( reject, COMMAND_TIMEOUT_DURATION_SECONDS * 1000, new Error("Command Timed Out"), ), ); } catch (caught) { if (caught.message === "Command Timed Out") { commandExecutedResult.state = "TIMED_OUT"; } else { throw caught; } } if (commandExecutedResult.state !== "SUCCESS") { console.log( `The command with id: ${state.CommandId} did not execute in the allotted time. Canceling command.`, ); state.ssmClient.send( new CancelCommandCommand({ CommandId: state.CommandId, }), ); state.enterIdOrSkipEC2HelloWorld === ""; return; } for await (const page of paginateListCommandInvocations( { client: state.ssmClient }, { CommandId: state.CommandId }, )) { listInvocationsPaginated.push(...page.CommandInvocations); } /** * @type {import('@aws-sdk/client-ssm').CommandInvocation} */ const commandInvocation = listInvocationsPaginated.shift(); // Because the call was made with CommandId, there's only one result, so shift it off. state.requestedDateTime = commandInvocation.RequestedDateTime; console.log( `The command invocation happened at: ${state.requestedDateTime}.`, ); }, { skipWhen: (/** @type {State} */ state) => state.enterIdOrSkipEC2HelloWorld === "", }, ); const createSSMOpsItem = new ScenarioOutput( "createSSMOpsItem", `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.`, ); const sdkCreateSSMOpsItem = new ScenarioAction( "sdkCreateSSMOpsItem", async (/** @type {State} */ state) => { try { const response = await state.ssmClient.send( new CreateOpsItemCommand({ Description: "Created by the System Manager Javascript API", Title: "Disk Space Alert", Source: "EC2", Category: "Performance", Severity: "2", }), ); state.opsItemId = response.OpsItemId; } catch (caught) { console.error(caught.message); console.log( `An error occurred while creating the ops item. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const updateOpsItem = new ScenarioOutput( "updateOpsItem", (/** @type {State} */ state) => `Now we will update the OpsItem: ${state.opsItemId}`, ); const sdkUpdateOpsItem = new ScenarioAction( "sdkUpdateOpsItem", async (/** @type {State} */ state) => { try { const _response = await state.ssmClient.send( new UpdateOpsItemCommand({ OpsItemId: state.opsItemId, Description: `An update to ${state.opsItemId}`, }), ); } catch (caught) { console.error(caught.message); console.log( `An error occurred while updating the ops item. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const getOpsItemStatus = new ScenarioOutput( "getOpsItemStatus", (/** @type {State} */ state) => `Now we will get the status of the OpsItem: ${state.opsItemId}`, ); const sdkOpsItemStatus = new ScenarioAction( "sdkGetOpsItemStatus", async (/** @type {State} */ state) => { try { const response = await state.ssmClient.send( new DescribeOpsItemsCommand({ OpsItemId: state.opsItemId, }), ); state.opsItemStatus = response.OpsItemStatus; } catch (caught) { console.error(caught.message); console.log( `An error occurred while describing the ops item. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const resolveOpsItem = new ScenarioOutput( "resolveOpsItem", (/** @type {State} */ state) => `Now we will resolve the OpsItem: ${state.opsItemId}`, ); const sdkResolveOpsItem = new ScenarioAction( "sdkResolveOpsItem", async (/** @type {State} */ state) => { try { const _response = await state.ssmClient.send( new UpdateOpsItemCommand({ OpsItemId: state.opsItemId, Status: OpsItemStatus.RESOLVED, }), ); } catch (caught) { console.error(caught.message); console.log( `An error occurred while updating the ops item. Please fix the error and try again. Error message: ${caught.message}`, ); throw caught; } }, ); const askToDeleteResources = new ScenarioInput( "askToDeleteResources", "Would you like to delete the Systems Manager resources created during this example run?", { type: "confirm" }, ); const confirmDeleteChoice = new ScenarioOutput( "confirmDeleteChoice", (/** @type {State} */ state) => { if (state.askToDeleteResources) { return "You chose to delete the resources."; } return "The Systems Manager resources will not be deleted. Please delete them manually to avoid charges."; }, ); export const sdkDeleteResources = new ScenarioAction( "sdkDeleteResources", async (/** @type {State} */ state) => { try { await state.ssmClient.send( new DeleteOpsItemCommand({ OpsItemId: state.opsItemId, }), ); console.log(`The ops item: ${state.opsItemId} was successfully deleted.`); } catch (caught) { console.log( `There was a problem deleting the ops item: ${state.opsItemId}. Please delete it manually. Error: ${caught.message}`, ); } try { await state.ssmClient.send( new DeleteMaintenanceWindowCommand({ Name: state.maintenanceWindow, WindowId: state.winId, }), ); console.log( `The maintenance window: ${state.maintenanceWindow} was successfully deleted.`, ); } catch (caught) { console.log( `There was a problem deleting the maintenance window: ${state.opsItemId}. Please delete it manually. Error: ${caught.message}`, ); } try { await state.ssmClient.send( new DeleteDocumentCommand({ Name: state.documentName, }), ); console.log( `The document: ${state.documentName} was successfully deleted.`, ); } catch (caught) { console.log( `There was a problem deleting the document: ${state.documentName}. Please delete it manually. Error: ${caught.message}`, ); } }, { skipWhen: (/** @type {{}} */ state) => !state.askToDeleteResources }, ); const goodbye = new ScenarioOutput( "goodbye", "This concludes the Systems Manager Basics scenario for the AWS Javascript SDK v3. Thank you!", ); const myScenario = new Scenario( "SSM Basics", [ greet, pressEnter, createMaintenanceWindow, getMaintenanceWindow, sdkCreateMaintenanceWindow, modifyMaintenanceWindow, pressEnter, sdkModifyMaintenanceWindow, createSystemsManagerActions, getDocumentName, sdkCreateSSMDoc, ec2HelloWorld, enterIdOrSkipEC2HelloWorld, sdkEC2HelloWorld, sdkGetCommandTime, pressEnter, createSSMOpsItem, pressEnter, sdkCreateSSMOpsItem, updateOpsItem, pressEnter, sdkUpdateOpsItem, getOpsItemStatus, pressEnter, sdkOpsItemStatus, resolveOpsItem, pressEnter, sdkResolveOpsItem, askToDeleteResources, confirmDeleteChoice, sdkDeleteResources, goodbye, ], { ssmClient: new SSMClient({}) }, ); /** @type {{ stepHandlerOptions: StepHandlerOptions }} */ export const main = async (stepHandlerOptions) => { await myScenario.run(stepHandlerOptions); }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const { values } = parseArgs({ options: { yes: { type: "boolean", short: "y", }, }, }); main({ confirmAll: values.yes }); }

작업

다음 코드 예시에서는 CreateDocument을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { CreateDocumentCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Create an SSM document. * @param {{ content: string, name: string, documentType?: DocumentType }} */ export const main = async ({ content, name, documentType }) => { const client = new SSMClient({}); try { const { documentDescription } = await client.send( new CreateDocumentCommand({ Content: content, // The content for the new SSM document. The content must not exceed 64KB. Name: name, DocumentType: documentType, // Document format type can be JSON, YAML, or TEXT. The default format is JSON. }), ); console.log("Document created successfully."); return { DocumentDescription: documentDescription }; } catch (caught) { if (caught instanceof Error && caught.name === "DocumentAlreadyExists") { console.warn(`${caught.message}. Did you provide a new document name?`); } else { throw caught; } } };
  • 자세한 API 내용은 AWS SDK for JavaScript API 참조CreateDocument의 섹션을 참조하세요.

다음 코드 예시에서는 CreateMaintenanceWindow을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

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 }} */ export const 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 instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide these values?`); } else { throw caught; } } };

다음 코드 예시에서는 CreateOpsItem을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { CreateOpsItemCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Create an SSM OpsItem. * @param {{ title: string, source: string, category?: string, severity?: string }} */ export const main = async ({ title, source, category = undefined, severity = undefined, }) => { const client = new SSMClient({}); try { const { opsItemArn, opsItemId } = await client.send( new CreateOpsItemCommand({ Title: title, Source: source, // The origin of the OpsItem, such as Amazon EC2 or Systems Manager. Category: category, Severity: severity, }), ); console.log(`Ops item created with id: ${opsItemId}`); return { OpsItemArn: opsItemArn, OpsItemId: opsItemId }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide these values?`); } else { throw caught; } } };
  • 자세한 API 내용은 AWS SDK for JavaScript API 참조CreateOpsItem의 섹션을 참조하세요.

다음 코드 예시에서는 DeleteDocument을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { DeleteDocumentCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Delete an SSM document. * @param {{ documentName: string }} */ export const main = async ({ documentName }) => { const client = new SSMClient({}); try { await client.send(new DeleteDocumentCommand({ Name: documentName })); console.log(`Document '${documentName}' deleted.`); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };
  • 자세한 API 내용은 AWS SDK for JavaScript API 참조DeleteDocument의 섹션을 참조하세요.

다음 코드 예시에서는 DeleteMaintenanceWindow을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { DeleteMaintenanceWindowCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Delete an SSM maintenance window. * @param {{ windowId: string }} */ export const main = async ({ windowId }) => { const client = new SSMClient({}); try { await client.send( new DeleteMaintenanceWindowCommand({ WindowId: windowId }), ); console.log(`Maintenance window '${windowId}' deleted.`); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };

다음 코드 예시에서는 DescribeOpsItems을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { OpsItemFilterOperator, OpsItemFilterKey, paginateDescribeOpsItems, SSMClient, } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Describe SSM OpsItems. * @param {{ opsItemId: string }} */ export const main = async ({ opsItemId }) => { const client = new SSMClient({}); try { const describeOpsItemsPaginated = []; for await (const page of paginateDescribeOpsItems( { client }, { OpsItemFilters: { Key: OpsItemFilterKey.OPSITEM_ID, Operator: OpsItemFilterOperator.EQUAL, Values: opsItemId, }, }, )) { describeOpsItemsPaginated.push(...page.OpsItemSummaries); } console.log("Here are the ops items:"); console.log(describeOpsItemsPaginated); return { OpsItemSummaries: describeOpsItemsPaginated }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } throw caught; } };
  • API 자세한 내용은 AWS SDK for JavaScript API 참조DescribeOpsItems의 섹션을 참조하세요.

다음 코드 예시에서는 ListCommandInvocations을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { paginateListCommandInvocations, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * List SSM command invocations on an instance. * @param {{ instanceId: string }} */ export const main = async ({ instanceId }) => { const client = new SSMClient({}); try { const listCommandInvocationsPaginated = []; // The paginate function is a wrapper around the base command. const paginator = paginateListCommandInvocations( { client }, { InstanceId: instanceId, }, ); for await (const page of paginator) { listCommandInvocationsPaginated.push(...page.CommandInvocations); } console.log("Here is the list of command invocations:"); console.log(listCommandInvocationsPaginated); return { CommandInvocations: listCommandInvocationsPaginated }; } catch (caught) { if (caught instanceof Error && caught.name === "ValidationError") { console.warn(`${caught.message}. Did you provide a valid instance ID?`); } throw caught; } };
  • API 자세한 내용은 AWS SDK for JavaScript API 참조ListCommandInvocations의 섹션을 참조하세요.

다음 코드 예시에서는 SendCommand을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { SendCommandCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Send an SSM command to a managed node. * @param {{ documentName: string }} */ export const main = async ({ documentName }) => { const client = new SSMClient({}); try { await client.send( new SendCommandCommand({ DocumentName: documentName, }), ); console.log("Command sent successfully."); return { Success: true }; } catch (caught) { if (caught instanceof Error && caught.name === "ValidationError") { console.warn(`${caught.message}. Did you provide a valid document name?`); } else { throw caught; } } };
  • API 자세한 내용은 AWS SDK for JavaScript API 참조SendCommand의 섹션을 참조하세요.

다음 코드 예시에서는 UpdateMaintenanceWindow을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { UpdateMaintenanceWindowCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Update an SSM maintenance window. * @param {{ windowId: string, allowUnassociatedTargets?: boolean, duration?: number, enabled?: boolean, name?: string, schedule?: string }} */ export const main = async ({ windowId, allowUnassociatedTargets = undefined, //Allow the maintenance window to run on managed nodes, even if you haven't registered those nodes as targets. duration = undefined, //The duration of the maintenance window in hours. enabled = undefined, name = undefined, schedule = undefined, //The schedule of the maintenance window in the form of a cron or rate expression. }) => { const client = new SSMClient({}); try { const { opsItemArn, opsItemId } = await client.send( new UpdateMaintenanceWindowCommand({ WindowId: windowId, AllowUnassociatedTargets: allowUnassociatedTargets, Duration: duration, Enabled: enabled, Name: name, Schedule: schedule, }), ); console.log("Maintenance window updated."); return { OpsItemArn: opsItemArn, OpsItemId: opsItemId }; } catch (caught) { if (caught instanceof Error && caught.name === "ValidationError") { console.warn(`${caught.message}. Are these values correct?`); } else { throw caught; } } };

다음 코드 예시에서는 UpdateOpsItem을 사용하는 방법을 보여 줍니다.

SDK for JavaScript (v3)
참고

더 많은 기능이 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { UpdateOpsItemCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Update an SSM OpsItem. * @param {{ opsItemId: string, status?: OpsItemStatus }} */ export const main = async ({ opsItemId, status = undefined, // The OpsItem status. Status can be Open, In Progress, or Resolved }) => { const client = new SSMClient({}); try { await client.send( new UpdateOpsItemCommand({ OpsItemId: opsItemId, Status: status, }), ); console.log("Ops item updated."); return { Success: true }; } catch (caught) { if ( caught instanceof Error && caught.name === "OpsItemLimitExceededException" ) { console.warn( `Couldn't create ops item because you have exceeded your open OpsItem limit. ${caught.message}.`, ); } else { throw caught; } } };
  • 자세한 API 내용은 AWS SDK for JavaScript API 참조UpdateOpsItem의 섹션을 참조하세요.