【发布时间】:2021-12-07 14:03:44
【问题描述】:
我在 docker 容器中部署了一个微服务,用于管理和执行 terraform commads 以在 AWS 上创建基础设施。支持的terraform模板如下:
provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_default_vpc" "default" {
tags = {
Name = "Default VPC"
}
}
resource "aws_security_group" "se_security_group" {
name = "test-sg"
description = "secure soft edge ports"
vpc_id = aws_default_vpc.default.id
tags = {
Name = "test-sg"
}
}
resource "aws_instance" "web" {
ami = "ami-*********"
instance_type = "t3.micro"
tags = {
Name = "test"
}
depends_on = [
aws_security_group.se_security_group,
]
}
有了这个系统,当 terraform 进程正在执行(创建 EC2 实例)时,如果 docker 容器崩溃,那么状态文件将没有关于正在创建的 EC2 资源的条目。在容器重启时,如果 terraform 进程在同一个状态文件上重启,最终会创建一个全新的 EC2 实例,从而导致资源泄漏。
- terraform 中的崩溃场景通常如何处理?
- 有没有办法在状态文件没有 EC2 条目的情况下回滚之前的事务?
请帮我解决这个问题。谢谢
【问题讨论】:
-
这通常不会自动处理,因为它不会发生(经常发生)。如果 terraform 崩溃,需要手动处理状态文件和创建的资源,删除资源或将其导入状态。
-
如果 terraform 使用身份验证令牌访问状态并且令牌在申请中途过期,它也可能崩溃。我经常遇到这种情况。
标签: amazon-web-services terraform terraform-provider-aws terraform-provider-azure