Delete orphaned Lambda Log groups very fast
Published: 2022-12-13 16:41:02


## The problem

We have some Lambda Functions

```bash
aws lambda list-functions --query "Functions[].FunctionName"
```

```log
[
    "xraystarter-LogRetentionaae0aa3c5b4d4f87b02d85b201-2QZ9sjniwX2i",
    "xraystarter-BucketNotificationsHandler050a0587b754-z8E1DEE5a2Zy",
    "xraystarter-py",
    "xraystarter-go",
    "xraystarter-ts"
]
```

The first time a function writes to stdout/stderr, a Log Group will be created.

```bash
aws logs describe-log-groups --query "logGroups[].logGroupName"
    "/aws/lambda/hellodockerarm",
    "/aws/lambda/xraystarter-BucketNotificationsHandler050a0587b754-z8E1DEE5a2Zy",
    "/aws/lambda/xraystarter-LogRetentionaae0aa3c5b4d4f87b02d85b201-2QZ9sjniwX2i",
    "/aws/lambda/xraystarter-go",
    "/aws/lambda/xraystarter-py",
    "/aws/lambda/xraystarter-ts"
]
```

## After deleting all Functions

```bash
aws lambda list-functions --query "Functions[].FunctionName"
[]
```

### All log groups are still there:

```bash
╰─ aws logs describe-log-groups --query "logGroups[].logGroupName"
[
    "/aws/lambda/hellodockerarm",
    "/aws/lambda/xraystarter-BucketNotificationsHandler050a0587b754-z8E1DEE5a2Zy",
    "/aws/lambda/xraystarter-LogRetentionaae0aa3c5b4d4f87b02d85b201-2QZ9sjniwX2i",
    "/aws/lambda/xraystarter-go",
    "/aws/lambda/xraystarter-py",
    "/aws/lambda/xraystarter-ts"
]
```

## The tool

`orphan` shows the leftover "orphaned" Log Groups and with an additional parameter `--no-dry-run` it will delete all Log Groups *fast*. Because it`s GO.

See https://github.com/megaproaktiv/orphan and download the binary release, if you like. *Use at your own risk!* 


```bash
orphan
/aws/lambda/hellodockerarm
/aws/lambda/xraystarter-BucketNotificationsHandler050a0587b754-z8E1DEE5a2Zy
/aws/lambda/xraystarter-LogRetentionaae0aa3c5b4d4f87b02d85b201-2QZ9sjniwX2i
/aws/lambda/xraystarter-go
/aws/lambda/xraystarter-py
/aws/lambda/xraystarter-ts
```

```bash
orphan --no-dry-run
Deleted: /aws/lambda/hellodockerarm
Deleted: /aws/lambda/xraystarter-BucketNotificationsHandler050a0587b754-z8E1DEE5a2Zy
Deleted: /aws/lambda/xraystarter-LogRetentionaae0aa3c5b4d4f87b02d85b201-2QZ9sjniwX2i
Deleted: /aws/lambda/xraystarter-go
Deleted: /aws/lambda/xraystarter-py
Deleted: /aws/lambda/xraystarter-ts
```

```bash
aws logs describe-log-groups --query "logGroups[].logGroupName"
[]
```

## More AWS Tools i made to make my life easy

### switch
https://github.com/megaproaktiv/switchaws
Working together with leapp or similar sso supporting tools to switch aws profiles and perform other actions when switching the profile

### cdkstat

https://github.com/megaproaktiv/cdkstats
Show state of CloudFormation stacks managed by the AWS CDK Cloud Development Kit

## Conclusion

If you need consulting for your serverless project, don't hesitate to contact the sponsor of this blog, [tecRacer](https://www.tecracer.com/kontakt/).

For more AWS development stuff, follow me on dev https://dev.to/megaproaktiv.

## Thanks to

Photo by <a href="https://unsplash.com/@bernardojbp?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Bernardo Ramonfaur</a> on <a href="https://unsplash.com/s/photos/cowboy-gun?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>