Created
September 5, 2017 17:18
-
-
Save izinin/662bef99f6b89f0dc58ecee26d486ea6 to your computer and use it in GitHub Desktop.
creating S3 static website from AWS CodeBuild pipeline
This file contains 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
'use strict'; | |
// https://github.com/aws/aws-sdk-js | |
// http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/creating-and-calling-service-objects.html | |
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/ | |
// http://docs.amazonaws.cn/en_us/AWSJavaScriptSDK/guide/node-making-requests.html | |
var AWS = require('aws-sdk'); | |
var s3 = new AWS.S3({ | |
apiVersion: '2006-03-01' | |
}); | |
var AdmZip = require('adm-zip'); | |
var mimeTypes = require('mime-types'); | |
exports.handler = function(event, context) { | |
var codepipeline = new AWS.CodePipeline(); | |
// Retrieve the Job ID from the Lambda action | |
var jobId = event["CodePipeline.job"] ? event["CodePipeline.job"].id : "abc"; | |
// Retrieve the value of UserParameters from the Lambda action configuration in AWS CodePipeline | |
var bucket = event["CodePipeline.job"] ? | |
event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters : | |
"peformance-test-results"; | |
// Notify AWS CodePipeline of a successful job | |
var putJobSuccess = function(message) { | |
var params = { | |
jobId: jobId | |
}; | |
codepipeline.putJobSuccessResult(params, function(err, data) { | |
if (err) { | |
context.fail(err); | |
} else { | |
context.succeed(message); | |
} | |
}); | |
}; | |
// Notify AWS CodePipeline of a failed job | |
var putJobFailure = function(message) { | |
var params = { | |
jobId: jobId, | |
failureDetails: { | |
message: JSON.stringify(message), | |
type: 'JobFailed', | |
externalExecutionId: context.invokeid | |
} | |
}; | |
codepipeline.putJobFailureResult(params, function(err, data) { | |
context.fail(message); | |
}); | |
}; | |
var createS3Client = function(job) { | |
let artifactCredentials = job.data.artifactCredentials; | |
return new AWS.S3({ | |
apiVersion: '2006-03-01', | |
accessKeyId: artifactCredentials.accessKeyId, | |
secretAccessKey: artifactCredentials.secretAccessKey, | |
sessionToken: artifactCredentials.sessionToken, | |
signatureVersion: "v4" | |
}); | |
}; | |
var putObjComposeParams = function(bucket, zipEntry){ | |
let params = { | |
Bucket: bucket, | |
// 2 levels up in directory structure, e.g. | |
// 'logs/20170904-0822/images/graphes-Users_Arrival-rate.png' | |
Key: zipEntry.entryName.split('/').slice(2).join('/'), | |
Body: zipEntry.getData(), | |
ACL: "public-read" | |
}; | |
let contentType = mimeTypes.lookup(zipEntry.name); | |
if (contentType) { | |
params.ContentType = contentType.split("/")[0] === "text" ? | |
contentType + ";charset=UTF-8" : contentType; | |
} | |
return params; | |
} | |
var populate = function(bucket, s3source, params){ | |
s3source.getObject(params).on('success', | |
(resp)=>{ | |
if (!resp.data) console.log('No Data!'); | |
let zip = new AdmZip(resp.data.Body); | |
let requests = []; | |
// http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html | |
zip.getEntries().forEach((zipEntry) =>{ | |
let params = putObjComposeParams(bucket, zipEntry); | |
requests.push(s3.putObject(params)); | |
}); | |
Promise.all(requests.map((e)=>{return e.promise()})).then((_)=>{ | |
putJobSuccess("done"); | |
}); | |
}).on('error', (err)=> {putJobFailure(`get objects: ${err}`)}).send(); | |
} | |
console.log(`Bucket : ${bucket}`); | |
try { | |
let job = event["CodePipeline.job"]; | |
let inputArtifacts = job.data.inputArtifacts; | |
let params = { | |
Bucket: inputArtifacts[0].location.s3Location.bucketName, | |
Key: inputArtifacts[0].location.s3Location.objectKey | |
}; | |
let s3source = job ? createS3Client(job) : s3; | |
//let params = { Bucket: "codepipeline-eu-west-1-658285659212", | |
// Key: "performance-test-pip/TestLog/IV7VzlJ" }; | |
s3.listObjects({Bucket: bucket, MaxKeys: 1000}) | |
.on('success', (response)=>{ | |
let objs = response.data.Contents.map((e)=>{return {Key: e.Key};}); | |
s3.deleteObjects({ | |
Bucket: bucket, | |
Delete: { | |
Objects: objs, | |
Quiet: true | |
} | |
}).on('success', (_)=>{populate(bucket, s3source, params)}) | |
.on('error', (err)=> {populate(bucket, s3source, params)}).send() | |
}).send(); | |
} catch (ex) { | |
putJobFailure(ex); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment