简体   繁体   中英

How to invoke AWS CLI command from CodePipeline?

I want to copy artifacts from S3 bucket in Account 1 to S3 bucket in Account 2. Though I was able to setup replication but I want to know whether there is a way to invoke AWS CLI command from within a pipeline.

Can it be invoked using Lambda function? If yes, any small sample script will be helpful.

Yes, you can add a Lambda Invoke action to your pipeline to call the copyobject API. The core part of the Lambda function is as follow.

 exports.copyRepoToProdS3 = (event, context) => { const jobId = event['CodePipeline.job'].id const s3Location = event['CodePipeline.job'].data.inputArtifacts[0].location.s3Location const cpParams = JSON.parse(event['CodePipeline.job'].data.actionConfiguration.configuration.UserParameters) let promises = [] for (let bucket of prodBuckets) { let params = { Bucket: bucket, CopySource: s3Location['bucketName'] + '/' + s3Location['objectKey'], Key: cpParams['S3ObjectKey'] } promises.push(s3.copyObject(params).promise()) } return Promise.all(promises) .then((data) => { console.log('Successfully copied repo to buckets!') }).catch((error) => { console.log('Failed to copy repo to buckets!', error) }) } 

And more detailed steps to add roles and report processing result to CodePipeline can be find at the following link. https://medium.com/@codershunshun/how-to-invoke-aws-lambda-in-codepipeline-d7c77457af95

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM