diff --git a/deploy.go b/deploy.go index b6fc98f..6a413e7 100644 --- a/deploy.go +++ b/deploy.go @@ -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) } } @@ -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 } @@ -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) } } @@ -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 { @@ -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 } diff --git a/functionurl.go b/functionurl.go index cbe5bf5..51c0472 100644 --- a/functionurl.go +++ b/functionurl.go @@ -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{ @@ -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 { @@ -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 @@ -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 }