Created
August 16, 2021 22:12
-
-
Save kavinarvind/7e7127651d0bbee5ff2fa58b2866fc06 to your computer and use it in GitHub Desktop.
AWSFargate_LambdaFunction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exports.handler = async function (event) | |
| const AWS = require('aws-sdk'); | |
| console.log("Starting lambda"); | |
| AWS.config.region = event.key4; | |
| const ecs = new AWS.ECS({ apiVersion: '2014-11-13' }); | |
| const sns = new AWS.SNS({ apiVersion: '2016-11-15' }); | |
| const FARGATE_CLUSTERS = [ | |
| { | |
| cluster: event.key1, | |
| service: event.key2, | |
| enabled: true | |
| }]; | |
| try { | |
| for (var i = 0; i < FARGATE_CLUSTERS.length; i++) { | |
| if (FARGATE_CLUSTERS[i].enabled) { | |
| var res = await ecs.listTasks({ | |
| cluster: FARGATE_CLUSTERS[i].cluster, | |
| serviceName: FARGATE_CLUSTERS[i].service, | |
| desiredStatus: 'RUNNING', | |
| launchType: 'FARGATE', | |
| maxResults: event.key3 | |
| }).promise(); | |
| var index = Math.floor(res.taskArns.length); | |
| for ( var j=0; j< index; j++) { | |
| var stoppedTask = res.taskArns[j]; | |
| await ecs.stopTask({ | |
| cluster: FARGATE_CLUSTERS[i].cluster, | |
| task: stoppedTask, | |
| reason: 'AWS FIS' | |
| }).promise(); | |
| } | |
| } | |
| } | |
| } catch (err) { | |
| console.log("Didnt enter"); | |
| console.log(err.stack); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment