diff --git a/cmd/root.go b/cmd/root.go index 4cf285f4..d1a51cc9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,7 +65,10 @@ Complete documentation is available at https://github.com/awslabs/ssosync`, // running inside of AWS Lambda, we use the Lambda // execution path. func Execute() { - if cfg.IsLambda { + if cfg.IsLambdaRunningInCodePipeline { + lambda.Start(Handler) + } + else if cfg.IsLambda { lambda.Start(rootCmd.Execute) } @@ -74,6 +77,50 @@ func Execute() { } } +func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error) { + log.Debug(event) + err := rootCmd.Execute() + if err != nil { + // notify codepipeline and mark its job execution as Failure + s := session.Must(session.NewSession()) + cpl := codepipeline.New(s) + log.Fatal("Notifying CodePipeline and mark its job execution as Failure") + jobID := event.CodePipelineJob.ID + if len(jobID) == 0 { + panic("CodePipeline Job ID is not set") + } + // mark the job as Failure. + cplFailure := &codepipeline.PutJobFailureResultInput{ + JobId: aws.String(jobID), + FailureDetails: &codepipeline.FailureDetails{ + Message: aws.String(err.Error()), + Type: aws.String("JobFailed"), + }, + } + _, cplErr := cpl.PutJobFailureResult(cplFailure) + if cplErr != nil { + log.Fatal("Failed to update CodePipeline jobID %s status with: %s", jobID, cplErr.Error()) + } + return "Failure", err + } + s := session.Must(session.NewSession()) + cpl := codepipeline.New(s) + log.Info("Notifying CodePipeline and mark its job execution as Success") + jobID := event.CodePipelineJob.ID + if len(jobID) == 0 { + panic("CodePipeline Job ID is not set") + } + // mark the job as Success. + cplSuccess := &codepipeline.PutJobSuccessResultInput{ + JobId: aws.String(jobID), + } + _, cplErr := cpl.PutJobSuccessResult(cplSuccess) + if cplErr != nil { + log.Fatal("Failed to update CodePipeline jobID %s status with: %s", jobID, cplErr.Error()) + } + return "Success", nil +} + func init() { // init config cfg = config.New() diff --git a/internal/config/config.go b/internal/config/config.go index 8fd3803d..3e3ad60b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -25,6 +25,8 @@ type Config struct { IsLambda bool // IsLambdaRunningInCodePipeline ... IsLambdaRunningInCodePipeline bool + // IsLambdaRunningInCodePipeline ... + IsLambdaRunningInCodePipeline bool // Ignore users ... IgnoreUsers []string `mapstructure:"ignore_users"` // Ignore groups ...