Skip to content

Commit

Permalink
Merge pull request #356 from fujiwara/fix/v1-create-url
Browse files Browse the repository at this point in the history
fix: deploy function url after create function.
  • Loading branch information
fujiwara authored Feb 8, 2024
2 parents b3ec4d3 + 6fb0198 commit 9268df9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
26 changes: 16 additions & 10 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
if err != nil {
return fmt.Errorf("failed to load function url config: %w", err)
}
return app.deployFunctionURL(ctx, fc)
return app.deployFunctionURL(ctx, fc, opt)
}
}

Expand All @@ -104,10 +104,16 @@ func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
FunctionName: fn.FunctionName,
}); err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return app.create(ctx, opt, fn)
if !errors.As(err, &nfe) {
return err
}
return err
if err := app.create(ctx, opt, fn); err != nil {
return err
}
if err := deployFunctionURL(ctx); err != nil {
return err
}
return nil
} else if err := validateUpdateFunction(current.Configuration, current.Code, fn); err != nil {
return err
}
Expand Down Expand Up @@ -160,7 +166,7 @@ func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
proc := func(ctx context.Context) error {
return app.updateFunctionConfiguration(ctx, confIn)
}
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function configuration", proc); err != nil {
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function configuration", proc, opt.label()); err != nil {
return fmt.Errorf("failed to update function configuration: %w", err)
}
}
Expand Down Expand Up @@ -190,7 +196,7 @@ func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
res, err = app.updateFunctionCode(ctx, codeIn)
return err
}
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function code", proc); err != nil {
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function code", proc, opt.label()); err != nil {
return err
}
if res.Version != nil {
Expand Down Expand Up @@ -256,19 +262,19 @@ func (app *App) updateFunctionCode(ctx context.Context, in *lambda.UpdateFunctio
return res, nil
}

func (app *App) ensureLastUpdateStatusSuccessful(ctx context.Context, name string, msg string, code func(ctx context.Context) error) error {
log.Println("[info]", msg, "...")
func (app *App) ensureLastUpdateStatusSuccessful(ctx context.Context, name string, msg string, code func(ctx context.Context) error, label string) error {
log.Println("[info]", msg, "...", label)
if err := app.waitForLastUpdateStatusSuccessful(ctx, name); err != nil {
return err
}
if err := code(ctx); err != nil {
return err
}
log.Println("[info]", msg, "accepted. waiting for LastUpdateStatus to be successful.")
log.Println("[info]", msg, "accepted. waiting for LastUpdateStatus to be successful.", label)
if err := app.waitForLastUpdateStatusSuccessful(ctx, name); err != nil {
return err
}
log.Println("[info]", msg, "successfully")
log.Println("[info]", msg, "successfully", label)
return nil
}

Expand Down
46 changes: 27 additions & 19 deletions functionurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,22 @@ func (app *App) loadFunctionUrl(path string, functionName string) (*FunctionURL,
return f, nil
}

func (app *App) deployFunctionURL(ctx context.Context, fc *FunctionURL) error {
log.Println("[info] deploying function url...")
func (app *App) deployFunctionURL(ctx context.Context, fc *FunctionURL, opt *DeployOption) error {
log.Printf("[info] deploying function url... %s", opt.label())

if err := app.deployFunctionURLConfig(ctx, fc); err != nil {
if err := app.deployFunctionURLConfig(ctx, fc, opt); err != nil {
return fmt.Errorf("failed to deploy function url config: %w", err)
}

if err := app.deployFunctionURLPermissions(ctx, fc); err != nil {
if err := app.deployFunctionURLPermissions(ctx, fc, opt); err != nil {
return fmt.Errorf("failed to deploy function url permissions: %w", err)
}

log.Println("[info] deployed function url")
log.Println("[info] deployed function url", opt.label())
return nil
}

func (app *App) deployFunctionURLConfig(ctx context.Context, fc *FunctionURL) error {
func (app *App) deployFunctionURLConfig(ctx context.Context, fc *FunctionURL, opt *DeployOption) error {
create := false
fqFunctionName := fullQualifiedFunctionName(*fc.Config.FunctionName, fc.Config.Qualifier)
functinoUrlConfig, err := app.lambda.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{
Expand All @@ -196,13 +196,18 @@ func (app *App) deployFunctionURLConfig(ctx context.Context, fc *FunctionURL) er
if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
log.Printf("[info] function url config for %s not found. creating", fqFunctionName)
log.Printf("[info] function url config for %s not found. creating %s", fqFunctionName, opt.label())
create = true
} else {
return fmt.Errorf("failed to get function url config: %w", err)
}
}

if opt.DryRun {
log.Println("[info] dry-run mode. skipping function url config deployment")
return nil
}

if create {
res, err := app.lambda.CreateFunctionUrlConfig(ctx, fc.Config)
if err != nil {
Expand Down Expand Up @@ -232,7 +237,7 @@ func (app *App) deployFunctionURLConfig(ctx context.Context, fc *FunctionURL) er
return nil
}

func (app *App) deployFunctionURLPermissions(ctx context.Context, fc *FunctionURL) error {
func (app *App) deployFunctionURLPermissions(ctx context.Context, fc *FunctionURL, opt *DeployOption) error {
adds, removes, err := app.calcFunctionURLPermissionsDiff(ctx, fc)
if err != nil {
return err
Expand All @@ -242,22 +247,25 @@ func (app *App) deployFunctionURLPermissions(ctx context.Context, fc *FunctionUR
return nil
}

log.Printf("[info] adding %d permissions", len(adds))
for _, in := range adds {
if _, err := app.lambda.AddPermission(ctx, in); err != nil {
return fmt.Errorf("failed to add permission: %w", err)
log.Printf("[info] adding %d permissions %s", len(adds), opt.label())
if !opt.DryRun {
for _, in := range adds {
if _, err := app.lambda.AddPermission(ctx, in); err != nil {
return fmt.Errorf("failed to add permission: %w", err)
}
log.Printf("[info] added permission Sid: %s", *in.StatementId)
}
log.Printf("[info] added permission Sid: %s", *in.StatementId)
}

log.Printf("[info] removing %d permissions", len(removes))
for _, in := range removes {
if _, err := app.lambda.RemovePermission(ctx, in); err != nil {
return fmt.Errorf("failed to remove permission: %w", err)
log.Printf("[info] removing %d permissions %s", len(removes), opt.label())
if !opt.DryRun {
for _, in := range removes {
if _, err := app.lambda.RemovePermission(ctx, in); err != nil {
return fmt.Errorf("failed to remove permission: %w", err)
}
log.Printf("[info] removed permission Sid: %s", *in.StatementId)
}
log.Printf("[info] removed permission Sid: %s", *in.StatementId)
}

return nil
}

Expand Down

0 comments on commit 9268df9

Please sign in to comment.