Created
April 5, 2020 13:40
-
-
Save hades2510/c949391a69b3cb7d81c8b68eb7528c64 to your computer and use it in GitHub Desktop.
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
const AWS = require('aws-sdk') | |
var ecs = new AWS.ECS(); | |
exports.handler = async (event, context) => { | |
const record = event.Records[0] | |
const receiptHandle = record.receiptHandle | |
const sqsARN = record.eventSourceARN | |
let message = null | |
try { | |
message = JSON.parse(record.body) | |
} catch (ex) { | |
console.log("Parsing JSON body failed. We're returning 200 so the message is deleted from SQS") | |
console.log("This is the exception from JSON.parse") | |
console.log(ex) | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('ECS task not started, because of JSON.parse error. Check CloudWatch for more details.'), | |
}; | |
return response; | |
} | |
var taskDefinition = process.env.TASK_DEFINITION | |
var cluster = process.env.CLUSTER_NAME | |
var count = 1 | |
var params = { | |
taskDefinition: taskDefinition, | |
cluster: cluster, | |
count: count, | |
networkConfiguration: { | |
awsvpcConfiguration: { | |
subnets: [process.env.SUBNET], | |
assignPublicIp: 'ENABLED', | |
securityGroups: [process.env.SECURITY_GROUP] | |
} | |
}, | |
overrides: { | |
containerOverrides: [ | |
{ | |
name: process.env.CONTAINER_NAME, | |
environment: [ | |
{ | |
name: 'BUCKET', | |
value: message.bucket | |
}, | |
{ | |
name: 'KEY', | |
value: message.key | |
}, | |
{ | |
name: 'DESTINATION', | |
value: message.destination | |
}, | |
{ | |
name: 'EXCLUDE', | |
value: message.exclude | |
} | |
] | |
}] | |
} | |
} | |
try { | |
const result = await ecs.runTask(params).promise() | |
} catch (e) { | |
console.log('error', e) | |
} | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('ECS task started'), | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment