Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var AWS = require('aws-sdk');
- var S3 = new AWS.S3()
- var STS = new AWS.STS()
- exports.handler = async function(event, context) {
- console.log('event is: ', JSON.stringify(event, null, 2))
- var artifactLocation = event["CodePipeline.job"].data.inputArtifacts[0].location
- var originBucketName = artifactLocation.s3Location.bucketName
- var originKey = artifactLocation.s3Location.objectKey
- var codepipeline = new AWS.CodePipeline();
- // Retrieve the Job ID from the Lambda action
- var jobId = event["CodePipeline.job"].id;
- // Notify AWS CodePipeline of a successful job
- var putJobSuccess = async function(message) {
- var params = {
- jobId: jobId
- };
- return codepipeline.putJobSuccessResult(params).promise()
- };
- // Notify AWS CodePipeline of a failed job
- var putJobFailure = async function(message) {
- var params = {
- jobId: jobId,
- failureDetails: {
- message: JSON.stringify(message),
- type: 'JobFailed',
- externalExecutionId: context.invokeid
- }
- };
- return codepipeline.putJobFailureResult(params).promise()
- };
- try {
- console.log("attempting to assume new role...")
- let obj = await S3.getObject({
- Bucket: originBucketName,
- Key: originKey
- }).promise()
- let body = obj.Body
- await STS.assumeRole({
- RoleArn: "arn:aws:iam::470140892651:role/Allow2ndAccountAdminAccess",
- RoleSessionName: "2ndAccountRole"
- }).promise()
- console.log('assuming new role')
- AWS.config.credentials = new AWS.TemporaryCredentials({RoleArn: "arn:aws:iam::470140892651:role/Allow2ndAccountAdminAccess"});
- console.log("putting new object...")
- await new AWS.S3().putObject({
- Bucket: "codepipeline-test-bucket-tqh",
- Key: "test.zip",
- Body: body
- }).promise()
- console.log("put object successfully")
- // await S3.copyObject({
- // Bucket: "codepipeline-test-bucket-tqh",
- // CopySource: `/${originBucketName}/${originKey}`,
- // Key: "test.zip"
- // }).promise()
- return putJobSuccess('it worked!');
- } catch(e) {
- console.error(e)
- return putJobFailure('something went wrong')
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement