使用terraform destroy删除日志组和流。

huangapple go评论63阅读模式
英文:

Deleting log groups and streams with a terraform destroy

问题

我看到Terraform在执行销毁操作后不会删除日志组和流(AWS提供程序)。我知道这可能对某些情况很有必要,但在我的情况下不需要,它会在日志组中造成一些混乱。我经常需要构建和销毁,手动删除不方便。是否有办法在销毁操作上启用删除这些组和流?

英文:

I see that Terraform does not delete log groups and streams (AWS provider) after performing the destroy operation. I know that it might be needed for some points, but in my case it is not and it makes a little mess in the log groups. I have to build and destroy quite often and removing it manually is not comfortable.

Is there any way to enable deleting these groups and streams on destroy action?

答案1

得分: 1

如果您在terraform中管理它们,那么terraform也会销毁它们。

它们经常保留的原因是很多日志组是由服务自动创建的,例如Lambda。如果您不明确地先创建日志组,那么terraform就不知道它们,因此无法销毁它们。

解决方案:在您的terraform配置中创建aws_cloudwatch_log_group并在需要日志组的资源与日志组本身之间设置适当的显式depends_on关系。否则,在terraform apply期间,您可能会陷入这样的情况:Lambda被创建,最终被调用,创建日志组,然后(如果您不幸的话)terraform才尝试创建日志组,然后由于它已经存在而失败。或者在删除期间,terraform首先删除日志组,Lambda被最后调用,重新创建日志组,然后被删除,而日志组仍然存在,因为terraform已经删除了它管理的日志组。

日志组中的日志流将随着日志组本身的自动删除而自动删除。Terraform根本不知道这些流,也不能知道,因为它们只在基础架构的生命周期内创建。

英文:

If you manage them in terraform then terraform will also destroy them.

The reason they stick around often is that a lot of the log groups are created automatically by the service, e.g. Lambda. And if you do not explicitly create the log group first then terraform does not know about and therefore cannot destroy them.

Solution: create the aws_cloudwatch_log_group in your terraform config AND set proper explicit depends_on relations between the resources that needs the log group and the log group itself. Otherwise you might get into a situation where during a terraform apply the lambda gets created, eventually invoked, creates the log group and (if you are unlucky) only then does terraform try to create the log group and will then fail due to it already existing. Or the other way around during deletion, terraform deleting the log group first, lambda getting invoked one last time, recreating the log group, then getting deleted itself and the log group remaining because terraform did already delete its managed log group.

The log streams in the log group will be automatically deleted alongside the log group itself. Terraform does not know about the streams at all and neither can it, because they are only created during the lifetime of the infrastructure.

huangapple
  • 本文由 发表于 2023年6月5日 22:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定