使用 SDK for JavaScript (v3) 的 Systems Manager 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for JavaScript (v3) 的 Systems Manager 範例

下列程式碼範例示範如何搭配 Systems Manager 使用 AWS SDK for JavaScript (v3) 來執行動作和實作常見案例。

基本概念是程式碼範例,示範如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 Systems Manager。

SDK for JavaScript (v3)
注意

還有更多 on 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 詳細資訊,請參閱 ListDocuments AWS SDK for JavaScript 參考中的 API

基本概念

下列程式碼範例示範如何使用 Systems Manager 維護時段、文件和 OpsItems。

SDK for JavaScript (v3)
注意

還有更多 on 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)
注意

還有更多 on 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 詳細資訊,請參閱 CreateDocument AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateMaintenanceWindow

SDK for JavaScript (v3)
注意

還有更多 on 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)
注意

還有更多 on 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 詳細資訊,請參閱 CreateOpsItem AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 DeleteDocument

SDK for JavaScript (v3)
注意

還有更多 on 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 詳細資訊,請參閱 DeleteDocument AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 DeleteMaintenanceWindow

SDK for JavaScript (v3)
注意

還有更多 on 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)
注意

還有更多 on 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 詳細資訊,請參閱 DescribeOpsItems AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 ListCommandInvocations

SDK for JavaScript (v3)
注意

還有更多 on 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; } };

下列程式碼範例示範如何使用 SendCommand

SDK for JavaScript (v3)
注意

還有更多 on 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 詳細資訊,請參閱 SendCommand AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 UpdateMaintenanceWindow

SDK for JavaScript (v3)
注意

還有更多 on 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)
注意

還有更多 on 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 詳細資訊,請參閱 UpdateOpsItem AWS SDK for JavaScript 參考中的 API