Skip to content

Commit

Permalink
Merge pull request #206 from fujiwara/validate-update-code
Browse files Browse the repository at this point in the history
Validate update code
  • Loading branch information
fujiwara authored Dec 17, 2021
2 parents c1822bf + 4001a7e commit 73ebdbe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (app *App) prepareFunctionCodeForDeploy(opt DeployOption, fn *Function) err
info os.FileInfo
)

if aws.StringValue(fn.PackageType) == "Image" {
if aws.StringValue(fn.PackageType) == packageTypeImage {
if fn.Code == nil || fn.Code.ImageUri == nil {
return errors.New("PackageType=Image requires Code.ImageUri in function definition")
}
Expand Down
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
29 changes: 27 additions & 2 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

const versionLatest = "$LATEST"
const packageTypeImage = "Image"

var retryPolicy = retry.Policy{
MinDelay: time.Second,
Expand Down Expand Up @@ -244,9 +245,9 @@ func newFunctionFrom(c *lambda.FunctionConfiguration, code *lambda.FunctionCodeL
}
}

if aws.StringValue(code.RepositoryType) == "ECR" || aws.StringValue(fn.PackageType) == "Image" {
if aws.StringValue(code.RepositoryType) == "ECR" || aws.StringValue(fn.PackageType) == packageTypeImage {
log.Printf("[debug] Image URL=%s", *code.ImageUri)
fn.PackageType = aws.String("Image")
fn.PackageType = aws.String(packageTypeImage)
fn.Code = &lambda.FunctionCode{
ImageUri: code.ImageUri,
}
Expand Down Expand Up @@ -277,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 73ebdbe

Please sign in to comment.