const { LightsailClient, GetInstancesCommand, GetInstanceCommand, StartInstanceCommand, StopInstanceCommand, RebootInstanceCommand, CreateInstancesCommand, DeleteInstanceCommand, CreateInstanceSnapshotCommand, GetInstanceSnapshotsCommand, DeleteInstanceSnapshotCommand, } = require('@aws-sdk/client-lightsail'); function getConfig() { const region = process.env.AWS_REGION; const accessKeyId = process.env.AWS_ACCESS_KEY_ID; const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY; if (!region || !accessKeyId || !secretAccessKey) { throw new Error('AWS_REGION / AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY が設定されていません'); } return { region, accessKeyId, secretAccessKey }; } function getClient() { const { region, accessKeyId, secretAccessKey } = getConfig(); return new LightsailClient({ region, credentials: { accessKeyId, secretAccessKey } }); } async function listInstances(client = getClient()) { const res = await client.send(new GetInstancesCommand({})); return res.instances; } async function getInstance(instanceName, client = getClient()) { const res = await client.send(new GetInstanceCommand({ instanceName })); return res.instance; } async function startInstance(instanceName, client = getClient()) { const res = await client.send(new StartInstanceCommand({ instanceName })); return res.operations; } async function stopInstance(instanceName, client = getClient()) { const res = await client.send(new StopInstanceCommand({ instanceName })); return res.operations; } async function rebootInstance(instanceName, client = getClient()) { const res = await client.send(new RebootInstanceCommand({ instanceName })); return res.operations; } async function createInstance({ instanceName, availabilityZone, blueprintId, bundleId }, client = getClient()) { const res = await client.send(new CreateInstancesCommand({ instanceNames: [instanceName], availabilityZone, blueprintId, bundleId, })); return res.operations; } async function deleteInstance(instanceName, client = getClient()) { const res = await client.send(new DeleteInstanceCommand({ instanceName })); return res.operations; } async function createSnapshot(instanceName, snapshotName, client = getClient()) { const res = await client.send(new CreateInstanceSnapshotCommand({ instanceSnapshotName: snapshotName, instanceName, })); return res.operations; } async function listSnapshots(client = getClient()) { const res = await client.send(new GetInstanceSnapshotsCommand({})); return res.instanceSnapshots; } async function deleteSnapshot(snapshotName, client = getClient()) { const res = await client.send(new DeleteInstanceSnapshotCommand({ instanceSnapshotName: snapshotName })); return res.operations; } module.exports = { getConfig, getClient, listInstances, getInstance, startInstance, stopInstance, rebootInstance, createInstance, deleteInstance, createSnapshot, listSnapshots, deleteSnapshot, };