|
| 1 | +var AWS = require('aws-sdk'); |
| 2 | +var ecs = new AWS.ECS(); |
| 3 | + |
| 4 | +exports.handler = function(event, context, callback) { |
| 5 | + |
| 6 | + var taskDefParams = { |
| 7 | + taskDefinition: event["ResourceProperties"]["TaskDefinition"], |
| 8 | + }; |
| 9 | + |
| 10 | + var ecsParams = { |
| 11 | + service: event["ResourceProperties"]["EcsService"], |
| 12 | + cluster: event["ResourceProperties"]["EcsCluster"] |
| 13 | + }; |
| 14 | + |
| 15 | + var volume = { |
| 16 | + name: event["ResourceProperties"]["EfsMountName"], |
| 17 | + efsVolumeConfiguration: { fileSystemId: event["ResourceProperties"]["EfsFileSystemId"], rootDirectory: '/' } |
| 18 | + }; |
| 19 | + |
| 20 | + var mountPoint = { |
| 21 | + sourceVolume: event["ResourceProperties"]["EfsMountName"], |
| 22 | + containerPath: '/' + event["ResourceProperties"]["EfsMountName"], |
| 23 | + readOnly: false |
| 24 | + }; |
| 25 | + |
| 26 | + console.log(event); |
| 27 | + console.log(event["ResourceProperties"]); |
| 28 | + try { |
| 29 | + console.log("Getting Task Definition"); |
| 30 | + ecs.describeTaskDefinition(taskDefParams, function(err, data) { |
| 31 | + if (err) |
| 32 | + console.log(err, err.stack); |
| 33 | + else |
| 34 | + data['taskDefinition']['volumes'].push(volume); |
| 35 | + data['taskDefinition']['containerDefinitions'][0]['mountPoints'].push(mountPoint); |
| 36 | + delete data['taskDefinition']['taskDefinitionArn']; |
| 37 | + delete data['taskDefinition']['revision']; |
| 38 | + delete data['taskDefinition']['status']; |
| 39 | + delete data['taskDefinition']['requiresAttributes']; |
| 40 | + delete data['taskDefinition']['compatibilities']; |
| 41 | + |
| 42 | + console.log("Register New Task Definition"); |
| 43 | + ecs.registerTaskDefinition(data['taskDefinition'], function(err, data) { |
| 44 | + if (err) |
| 45 | + console.log(err, err.stack); |
| 46 | + else |
| 47 | + console.log(data); |
| 48 | + console.log("Update Service"); |
| 49 | + ecs.updateService({ |
| 50 | + service: ecsParams['service'], |
| 51 | + cluster: ecsParams['cluster'], |
| 52 | + taskDefinition: data['taskDefinition']['taskDefinitionArn'], |
| 53 | + |
| 54 | + }, |
| 55 | + function (err, data) { |
| 56 | + if (err) |
| 57 | + console.log(err, err.stack); |
| 58 | + else |
| 59 | + console.log(data); |
| 60 | + sendResponse(event, context, 'SUCCESS', { 'Message': 'Resource creation successful!' }); |
| 61 | + }); |
| 62 | + console.log(data); |
| 63 | + }); |
| 64 | + }); |
| 65 | + } catch (e) { |
| 66 | + console.log('Errors' + e); |
| 67 | + sendResponse(event, context, 'FAILED') |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +// Send response to the pre-signed S3 URL |
| 73 | +function sendResponse (event, context, responseStatus, responseData) { |
| 74 | + console.log('Sending response ' + responseStatus); |
| 75 | + var responseBody = JSON.stringify({ |
| 76 | + Status: responseStatus, |
| 77 | + Reason: 'See the details in CloudWatch Log Stream: ' + context.logStreamName, |
| 78 | + PhysicalResourceId: context.logStreamName, |
| 79 | + StackId: event.StackId, |
| 80 | + RequestId: event.RequestId, |
| 81 | + LogicalResourceId: event.LogicalResourceId, |
| 82 | + Data: responseData |
| 83 | + }); |
| 84 | + |
| 85 | + console.log('RESPONSE BODY:\n', responseBody); |
| 86 | + |
| 87 | + var https = require('https'); |
| 88 | + var url = require('url'); |
| 89 | + |
| 90 | + var parsedUrl = url.parse(event.ResponseURL); |
| 91 | + var options = { |
| 92 | + hostname: parsedUrl.hostname, |
| 93 | + port: 443, |
| 94 | + path: parsedUrl.path, |
| 95 | + method: 'PUT', |
| 96 | + headers: { |
| 97 | + 'content-type': '', |
| 98 | + 'content-length': responseBody.length |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + console.log('SENDING RESPONSE...\n'); |
| 103 | + |
| 104 | + var request = https.request(options, function (response) { |
| 105 | + console.log('STATUS: ' + response.statusCode); |
| 106 | + console.log('HEADERS: ' + JSON.stringify(response.headers)); |
| 107 | + // Tell AWS Lambda that the function execution is done |
| 108 | + context.done() |
| 109 | + }); |
| 110 | + |
| 111 | + request.on('error', function (error) { |
| 112 | + console.log('sendResponse Error:' + error); |
| 113 | + // Tell AWS Lambda that the function execution is done |
| 114 | + context.done() |
| 115 | + }); |
| 116 | + |
| 117 | + // write data to request body |
| 118 | + request.write(responseBody); |
| 119 | + request.end() |
| 120 | +} |
0 commit comments