Figuring out the best way of triggering a Lambda Code Pipeline S3 depoyment is a key part of a serverless architecture.
It can be really useful when you want your app to update pending an update from a 3rd party service.
Step 1

Step 2

Step 3

I’ve posted the Lambda function below (with comments) so you can trigger a Lambda Code Pipeline S3 depoyment! I’ve also written a similar post to this on how to add authentication to S3 bucket with Lambda functions, that you may find helpful! Enjoy 😀
Lambda function
// require the AWS SDK
const AWS = require('aws-sdk');
// create an instance of code pipeline from the AWS SDK, so you can reference it
const codepipeline = new AWS.CodePipeline();
// The Lambda function that excecutes on run time
exports.handler = async (event) => {
// javascript try statement
try {
// Await the result of the asynchronous function called build
await Promise.all([
// call the function build, passing through the name of the codepipeline to trigger
build('Test')
]);
// log the success of the pipeline trigger
console.log('SUCCESS: triggered code pipeline');
}
// javascript catch statement
catch (error) {
// log any occurred errors
throw new Error(`FAILED: dataToS3 lambda. ${error.message}`);
}
};
// asynchronous function called build
async function build(pipelineName) {
// define parameters for the pipeline build, in this case only the pipeline's name is required
const params = {
name: pipelineName,
};
// await the confirmation that the code pipeline has been started
await codepipeline.startPipelineExecution(params).promise();
}