0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.