Created
August 21, 2019 07:00
-
-
Save skabbes/6cf76e8e9e91cd8767adcd1cd41b81e2 to your computer and use it in GitHub Desktop.
A quick script coded up to free space used to old lambda versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package command | |
import ( | |
"fmt" | |
"strings" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awserr" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/lambda" | |
"github.com/urfave/cli" | |
) | |
// LambdaCleanup is a command described below | |
var LambdaCleanup = cli.Command{ | |
Name: "lambda-cleanup", | |
Usage: "Cleanup lambda function versions that are not deployed and not specifically tagged", | |
Action: wrapErrorCode(lambdaCleanupCmd), | |
Flags: []cli.Flag{ | |
cli.StringFlag{ | |
Name: "region", | |
Usage: "The AWS region to operate in", | |
Value: "us-west-2", | |
}, | |
}, | |
} | |
func lambdaCleanupCmd(c *cli.Context) error { | |
region := c.String("region") | |
session, err := session.NewSession() | |
if err != nil { | |
return err | |
} | |
config := aws.NewConfig().WithRegion(region) | |
svc := lambda.New(session, config) | |
input := &lambda.ListFunctionsInput{ | |
FunctionVersion: aws.String("ALL"), | |
} | |
seen := map[string]bool{} | |
funcs := []string{} | |
for { | |
output, err := svc.ListFunctions(input) | |
if err != nil { | |
return err | |
} | |
for _, out := range output.Functions { | |
if *out.Version == "$LATEST" { | |
continue | |
} | |
if !seen[*out.FunctionName] { | |
funcs = append(funcs, *out.FunctionName) | |
} | |
seen[*out.FunctionName] = true | |
} | |
if output.NextMarker == nil { | |
break | |
} | |
input.SetMarker(*output.NextMarker) | |
} | |
for _, name := range funcs { | |
aliases, err := svc.ListAliases(&lambda.ListAliasesInput{ | |
FunctionName: aws.String(name), | |
}) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("%s\n", name) | |
for _, a := range aliases.Aliases { | |
if !strings.HasPrefix(*a.Name, "commit-") { | |
continue | |
} | |
fmt.Printf("Deleting alias %s:%s\n", name, *a.Name) | |
_, err := svc.DeleteAlias(&lambda.DeleteAliasInput{ | |
FunctionName: aws.String(name), | |
Name: a.Name, | |
}) | |
if err != nil { | |
return err | |
} | |
} | |
} | |
input = &lambda.ListFunctionsInput{ | |
FunctionVersion: aws.String("ALL"), | |
} | |
for { | |
output, err := svc.ListFunctions(input) | |
if err != nil { | |
return err | |
} | |
for _, out := range output.Functions { | |
if *out.Version == "$LATEST" { | |
continue | |
} | |
fmt.Printf("Deleting %s, Version=%s\n", *out.FunctionName, *out.Version) | |
_, err := svc.DeleteFunction(&lambda.DeleteFunctionInput{ | |
FunctionName: out.FunctionArn, | |
Qualifier: out.Version, | |
}) | |
if err != nil { | |
if aErr, ok := err.(awserr.Error); ok { | |
if aErr.Code() == "ResourceConflictException" { | |
fmt.Printf("%s: %s\n", aErr.Code(), aErr.Message()) | |
break | |
} | |
return err | |
} | |
} | |
if err != nil { | |
return err | |
} | |
} | |
if output.NextMarker == nil { | |
break | |
} | |
input.SetMarker(*output.NextMarker) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment