Created
January 26, 2021 23:06
-
-
Save AdonousTech/02ef26622c3403db5127a27b7111338f to your computer and use it in GitHub Desktop.
A Cloud Coach - Tutorial - Serverless Stripe Store - 008 - Pipeline Cache Function
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
const AWS = require('aws-sdk'); | |
exports.handler = (event, context, callback) => { | |
const cloudfront = new AWS.CloudFront(); | |
const codepipeline = new AWS.CodePipeline(); | |
//job id passed into fn by CP | |
const jobId = event["CodePipeline.job"].id; | |
let cfInvalidationParams = { | |
DistributionId: process.env.CloudfrontDistId, | |
InvalidationBatch: { | |
CallerReference: Date.now().toString(), | |
Paths: { | |
Quantity: 1, | |
Items: ['/*'] | |
} | |
} | |
}; | |
cloudfront.createInvalidation(cfInvalidationParams, (err, data) => { | |
//define job success | |
let putJobSuccess = (jobSuccessMessage) => { | |
let successParams = { | |
jobId: jobId | |
} | |
codepipeline.putJobSuccessResult(successParams, (err, data) => { | |
if (err) { | |
callback(err, null); | |
} else { | |
callback(null, jobSuccessMessage); | |
} | |
}) | |
} | |
//define job failure | |
let putJobFailure = (jobFailureMessage) => { | |
let failureParams = { | |
jobId: jobId, | |
failureDetails: { | |
message: JSON.stringify(jobFailureMessage), | |
type: 'JobFailed', | |
externalExecutionId: context.invokeid | |
} | |
}; | |
codepipeline.putJobFailureResult(failureParams, (err, data) => { | |
callback(jobFailureMessage, null); | |
}) | |
} | |
if (err) { | |
putJobFailure('Failed to invalidate cache: ' + err); | |
} else { | |
//CP job successful after all files copied | |
putJobSuccess('Successfully invalidated CloudFront cache!'); | |
} | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment