Skip to content

Commit

Permalink
add validate function before deploy.
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Dec 17, 2021
1 parent 5e168d6 commit 4001a7e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (app *App) Deploy(opt DeployOption) error {
}

log.Printf("[info] starting deploy function %s", *fn.FunctionName)
if _, err := app.lambda.GetFunction(&lambda.GetFunctionInput{
if current, err := app.lambda.GetFunction(&lambda.GetFunctionInput{
FunctionName: fn.FunctionName,
}); err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand All @@ -93,6 +93,8 @@ func (app *App) Deploy(opt DeployOption) error {
}
}
return err
} else if err := validateUpdateFunction(current.Configuration, current.Code, fn); err != nil {
return err
}

if err := app.prepareFunctionCodeForDeploy(opt, fn); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (app *App) Diff(opt DiffOption) error {
fmt.Println(color.GreenString("+++" + *opt.FunctionFilePath))
fmt.Println(coloredDiff(ds))
}

if err := validateUpdateFunction(latest, code, newFunc); err != nil {
return err
}
return nil
}

Expand Down
24 changes: 24 additions & 0 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,27 @@ func exportEnvFile(file string) error {
}
return nil
}

var errCannotUpdateImageAndZip = errors.New("cannot update function code between Image and Zip")

func validateUpdateFunction(currentConf *lambda.FunctionConfiguration, currentCode *lambda.FunctionCodeLocation, newFn *lambda.CreateFunctionInput) error {
newCode := newFn.Code

// new=Image
if newCode != nil && newCode.ImageUri != nil || aws.StringValue(newFn.PackageType) == packageTypeImage {
// current=Zip
if currentCode == nil || currentCode.ImageUri == nil {
return errCannotUpdateImageAndZip
}
}

// current=Image
if currentCode != nil && currentCode.ImageUri != nil || aws.StringValue(currentConf.PackageType) == packageTypeImage {
// new=Zip
if newCode == nil || newCode.ImageUri == nil {
return errCannotUpdateImageAndZip
}
}

return nil
}

0 comments on commit 4001a7e

Please sign in to comment.