-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix startstop lambda has no effect on desire count #35
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before, wrt specifying the return type: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/trigger/api-gateway-proxy.d.ts#L140-L154 |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized, this should be Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness |
||
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)}`); | ||
Comment on lines
+57
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would refactor this to be pure async/await: try {
const result = await client.send(updateCommand);
console.log(`OK! ${JSON.stringify(result, null, 2)}`);
} catch (error) {
console.log(`Error! ${JSON.stringify(error, 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: {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed, and the comment above is redundant if the handler response is strongly typed |
||
body: JSON.stringify(responseBody) | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets specify the return type to clarify the required response values: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/trigger/api-gateway-proxy.d.ts#L140-L154