/
ssm-run-shell.ts
50 lines (40 loc) · 1.95 KB
/
ssm-run-shell.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { SSMClient, GetServiceSettingCommand, UpdateServiceSettingCommand, ListDocumentsCommand, CreateDocumentCommand, GetDocumentCommand, UpdateDocumentCommand } from "@aws-sdk/client-ssm";
import { readFileSync } from "fs";
import { resolve } from "path";
const SESSION_MANAGER_RUN_SHELL_CONTENT_PATH = resolve(__dirname, "../sessionManagerRunShell.json");
const SETTING_ID = `/ssm/managed-instance/activation-tier`;
const ssmClient = new SSMClient({});
export async function deploySSMRunShell(accountId: string) {
const serviceSetting = await ssmClient.send(new GetServiceSettingCommand({ SettingId: SETTING_ID }));
const settingValue = serviceSetting.ServiceSetting?.SettingValue;
if (settingValue === "standard") {
await ssmClient.send(new UpdateServiceSettingCommand({
SettingId: SETTING_ID,
SettingValue: "advanced"
}));
}
const documentList = await ssmClient.send(new ListDocumentsCommand({
DocumentFilterList: [{ key: "Name", value: "SSM-SessionManagerRunShell" }]
}));
const documentCount = documentList.DocumentIdentifiers?.length;
if (documentCount === 0) {
await ssmClient.send(new CreateDocumentCommand({
Name: "SSM-SessionManagerRunShell",
Content: readFileSync(SESSION_MANAGER_RUN_SHELL_CONTENT_PATH, "utf8"),
DocumentType: "Session"
}));
} else {
const currentDocument = await ssmClient.send(new GetDocumentCommand({
Name: "SSM-SessionManagerRunShell",
DocumentVersion: "$LATEST"
}));
const newDocument = readFileSync(SESSION_MANAGER_RUN_SHELL_CONTENT_PATH, "utf8");
if (JSON.stringify(currentDocument.Content) !== JSON.stringify(newDocument)) {
await ssmClient.send(new UpdateDocumentCommand({
Name: "SSM-SessionManagerRunShell",
Content: newDocument,
DocumentVersion: "$LATEST"
}));
}
}
}