From 4001a7eaef9243ee6c7726d1e3e8db086351abf4 Mon Sep 17 00:00:00 2001 From: FUJIWARA Shunichiro Date: Fri, 17 Dec 2021 22:59:13 +0900 Subject: [PATCH] add validate function before deploy. --- deploy.go | 4 +++- diff.go | 4 ++++ lambroll.go | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/deploy.go b/deploy.go index 3b636f0..f7b6cad 100644 --- a/deploy.go +++ b/deploy.go @@ -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 { @@ -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 { diff --git a/diff.go b/diff.go index 364597e..e64e74a 100644 --- a/diff.go +++ b/diff.go @@ -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 } diff --git a/lambroll.go b/lambroll.go index 02634bd..a7bac54 100644 --- a/lambroll.go +++ b/lambroll.go @@ -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 +}