Skip to content

Instantly share code, notes, and snippets.

@hades2510
Created April 5, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hades2510/c949391a69b3cb7d81c8b68eb7528c64 to your computer and use it in GitHub Desktop.
Save hades2510/c949391a69b3cb7d81c8b68eb7528c64 to your computer and use it in GitHub Desktop.
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