diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..3c03207 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +18 diff --git a/resources/serverstatus.ts b/resources/serverstatus.ts index 648acb1..00c009a 100644 --- a/resources/serverstatus.ts +++ b/resources/serverstatus.ts @@ -1,4 +1,4 @@ -/* +/* This code was created from sample code provided for the AWS SDK for JavaScript version 3 (v3), which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at @@ -35,7 +35,7 @@ const ec2Client = new EC2Client({ region: REGION }); /** * Everything here has the assumption there is only one task. */ -const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: Context) => { +export const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: Context) => { console.log(`New getServerStatus request: ${JSON.stringify(event)}`); console.log(`With context: ${JSON.stringify(context)}`); @@ -50,7 +50,6 @@ const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: }; }; -export default handler; async function getIPFunction() { @@ -62,15 +61,15 @@ async function getIPFunction() { try { - const listTasksCommandInput: ListTasksCommandInput = { + const listTasksCommandInput: ListTasksCommandInput = { serviceName: SERVICE_ARN, cluster: CLUSTER_ARN, desiredStatus: "RUNNING" } const listTasksCommandResult = await ecsClient.send(new ListTasksCommand(listTasksCommandInput)); - console.log(listTasksCommandResult); + console.log("ECS List Tasks Command Result: " + listTasksCommandResult); - if (!listTasksCommandResult.taskArns || listTasksCommandResult.taskArns.length <= 0) return; + if (!listTasksCommandResult.taskArns || listTasksCommandResult.taskArns.length <= 0) return statusResults; const networkInterfaceId = await getNetworkInterfaceId(CLUSTER_ARN, listTasksCommandResult.taskArns); @@ -79,7 +78,7 @@ async function getIPFunction() { } const describeNetworkInterfacesResult = await ec2Client.send(new DescribeNetworkInterfacesCommand(describeNetworkInterfacesInput)); - if (!describeNetworkInterfacesResult.NetworkInterfaces || describeNetworkInterfacesResult.NetworkInterfaces.length <= 0) return; + if (!describeNetworkInterfacesResult.NetworkInterfaces || describeNetworkInterfacesResult.NetworkInterfaces.length <= 0) return statusResults; const publicIp = describeNetworkInterfacesResult.NetworkInterfaces.find(x => x.Association != undefined)?.Association?.PublicIp; console.log("found public IP " + publicIp); @@ -111,7 +110,7 @@ const getNetworkInterfaceId = async (cluster: string, tasks: string[]): Promise< const networkInterfaceId = firstAttachment.details?.find(d => d.name === 'networkInterfaceId')?.value; if (!networkInterfaceId) throw new Error(`Could not find a network interface on given attachment: ${JSON.stringify(firstAttachment)}`); - + console.debug(`Found interface "${networkInterfaceId}"`); return networkInterfaceId; } catch (err) { diff --git a/resources/startstopserver.ts b/resources/startstopserver.ts index e20dd15..490f5dc 100644 --- a/resources/startstopserver.ts +++ b/resources/startstopserver.ts @@ -1,4 +1,4 @@ -/* +/* ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at @@ -20,63 +20,64 @@ import { ECSClient, UpdateServiceCommand } from '@aws-sdk/client-ecs'; import { APIGatewayEvent, APIGatewayProxyHandler, Context } from 'aws-lambda'; //Set the AWS Region -const REGION = process.env.REGION; +const REGION = process.env.REGION; -const SERVICE_NAME = process.env.SERVICE_NAME; -const CLUSTER_ARN = process.env.CLUSTER_ARN; +const SERVICE_NAME = process.env.SERVICE_NAME; +const CLUSTER_ARN = process.env.CLUSTER_ARN; const PASSWORD = process.env.PASSWORD; -const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: Context) => { - console.log("request: " + JSON.stringify(event)); - let responseCode = 400; - let message = "authentication failed"; +export const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: Context) => { + console.log("request: " + JSON.stringify(event)); + let responseCode = 400; + let message = "authentication failed"; - const params = { - desiredCount: 1, - service: SERVICE_NAME, - cluster: CLUSTER_ARN - } - - if (event.queryStringParameters && event.queryStringParameters.desiredCount !== undefined) { - let count = Math.min(Math.max(+event.queryStringParameters.desiredCount, 0), 1); - params.desiredCount = count; - console.log("changing desiredCount to " + count); - } + const params = { + desiredCount: 1, + service: SERVICE_NAME, + cluster: CLUSTER_ARN + } - if (event.queryStringParameters && event.queryStringParameters.key) { - let key = event.queryStringParameters.key; - if (key == PASSWORD) { - const client = new ECSClient({ region: REGION }); - console.log("starting service " + JSON.stringify(params)); - message = "authentication success"; - responseCode = 200; + if (event.queryStringParameters && event.queryStringParameters.desiredCount !== undefined) { + let count = Math.min(Math.max(+event.queryStringParameters.desiredCount, 0), 1); + params.desiredCount = count; + console.log("changing desiredCount to " + count); + } + if (event.queryStringParameters && event.queryStringParameters.key) { + let key = event.queryStringParameters.key; + if (key == PASSWORD) { + const client = new ECSClient({region: REGION}); + console.log("starting service " + JSON.stringify(params)); + message = "authentication success"; + responseCode = 200; - const updateCommand = new UpdateServiceCommand(params); - client.send(updateCommand).then( - (data) => {console.log(data);}, - (err) => { console.log(err);} - ); + const updateCommand = new UpdateServiceCommand(params); + + await client.send(updateCommand).then( + (data) => { + console.log(`OK! ${JSON.stringify(data, null, 2)}`); + }, + (err) => { + console.log(`Error! ${JSON.stringify(err, null, 2)}`); } + ); } + } - let responseBody = { - message: message, - }; - - // The output from a Lambda proxy integration must be - // in the following JSON object. The 'headers' property - // is for custom response headers in addition to standard - // ones. The 'body' property must be a JSON string. For - // base64-encoded payload, you must also set the 'isBase64Encoded' - // property to 'true'. - return { - statusCode: responseCode, - headers: { - }, - body: JSON.stringify(responseBody) - }; -}; + let responseBody = { + message: message, + }; -export default handler; + // The output from a Lambda proxy integration must be + // in the following JSON object. The 'headers' property + // is for custom response headers in addition to standard + // ones. The 'body' property must be a JSON string. For + // base64-encoded payload, you must also set the 'isBase64Encoded' + // property to 'true'. + return { + statusCode: responseCode, + headers: {}, + body: JSON.stringify(responseBody) + }; +};