Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. var AWS = require('aws-sdk');
  2. var S3 = new AWS.S3()
  3. var STS = new AWS.STS()
  4.  
  5. exports.handler = async function(event, context) {
  6. console.log('event is: ', JSON.stringify(event, null, 2))
  7.  
  8. var artifactLocation = event["CodePipeline.job"].data.inputArtifacts[0].location
  9. var originBucketName = artifactLocation.s3Location.bucketName
  10. var originKey = artifactLocation.s3Location.objectKey
  11.  
  12. var codepipeline = new AWS.CodePipeline();
  13.  
  14. // Retrieve the Job ID from the Lambda action
  15. var jobId = event["CodePipeline.job"].id;
  16.  
  17. // Notify AWS CodePipeline of a successful job
  18. var putJobSuccess = async function(message) {
  19. var params = {
  20. jobId: jobId
  21. };
  22. return codepipeline.putJobSuccessResult(params).promise()
  23. };
  24.  
  25. // Notify AWS CodePipeline of a failed job
  26. var putJobFailure = async function(message) {
  27. var params = {
  28. jobId: jobId,
  29. failureDetails: {
  30. message: JSON.stringify(message),
  31. type: 'JobFailed',
  32. externalExecutionId: context.invokeid
  33. }
  34. };
  35. return codepipeline.putJobFailureResult(params).promise()
  36. };
  37.  
  38. try {
  39. console.log("attempting to assume new role...")
  40.  
  41. let obj = await S3.getObject({
  42. Bucket: originBucketName,
  43. Key: originKey
  44. }).promise()
  45.  
  46. let body = obj.Body
  47.  
  48. await STS.assumeRole({
  49. RoleArn: "arn:aws:iam::470140892651:role/Allow2ndAccountAdminAccess",
  50. RoleSessionName: "2ndAccountRole"
  51. }).promise()
  52.  
  53. console.log('assuming new role')
  54. AWS.config.credentials = new AWS.TemporaryCredentials({RoleArn: "arn:aws:iam::470140892651:role/Allow2ndAccountAdminAccess"});
  55.  
  56. console.log("putting new object...")
  57. await new AWS.S3().putObject({
  58. Bucket: "codepipeline-test-bucket-tqh",
  59. Key: "test.zip",
  60. Body: body
  61. }).promise()
  62.  
  63. console.log("put object successfully")
  64.  
  65. // await S3.copyObject({
  66. // Bucket: "codepipeline-test-bucket-tqh",
  67. // CopySource: `/${originBucketName}/${originKey}`,
  68. // Key: "test.zip"
  69. // }).promise()
  70.  
  71. return putJobSuccess('it worked!');
  72. } catch(e) {
  73. console.error(e)
  74. return putJobFailure('something went wrong')
  75. }
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement