amazon-web-services - Terraform - 创建 EBS 快照,然后将快照转换为 EBS 并附加到 EC2

标签 amazon-web-services terraform terraform-provider-aws

是否可以创建 EBS 卷的快照,获取该快照并将其转换回 EBS 卷并通过 Terraform 将其附加到 EC2?

我目前正在考虑在 AWS 中自动化我们的生产和测试环境,因此它们是相同的,我发现使用 Terraform 非常有用,但我找不到任何关于如何实现这一点的文档。

最佳答案

您可以使用 aws_ebs_volume 从快照创建 EBS 卷并将其附加到实例而不会有太多困难。和 aws_volume_attachment资源。

您还可以使用 aws_ebs_snapshot 创建快照资源或使用 aws_ebs_snapshot 动态获取快照 ID数据源。

一个简单的例子可能是这样的:

data "aws_ebs_volume" "production_volume" {
  most_recent = true

  filter {
    name   = "volume-type"
    values = ["gp2"]
  }

  filter {
    name   = "tag:Name"
    values = ["Production"]
  }
}

resource "aws_ebs_snapshot" "production_snapshot" {
  volume_id = "${data.aws_ebs_volume.prod_volume.id}"

  tags {
    Name = "Production"
  }
}

resource "aws_ebs_volume" "from_production_snapshot" {
  availability_zone = "us-west-2a"
  snapshot_id       = "${aws_ebs_snapshot.production_snapshot.id}"
  size              = 40

  tags {
    Name = "Non-Production"
  }
}

resource "aws_instance" "non_production" {
  ami               = "ami-21f78e11"
  availability_zone = "us-west-2a"
  instance_type     = "t2.micro"

  tags {
    Name = "Non-Production"
  }
}

resource "aws_volume_attachment" "non_production" {
  device_name = "/dev/xvdf"
  volume_id   = "${aws_ebs_volume.from_production_snapshot.id}"
  instance_id = "${aws_instance.non_production.id}"
}

关于amazon-web-services - Terraform - 创建 EBS 快照,然后将快照转换为 EBS 并附加到 EC2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49488416/

相关文章:

amazon-web-services - Grok 模式(正则表达式)用于过滤嵌套括号中的数据

java - 在 Docker 容器中安装 MySQL Connector/J

amazon-web-services - AWS 模块中的 Terraform 条件

amazon-web-services - AWS CDK CI/CD 管道 - 部署的 Lambda 返回 ClassNotFoundException

terraform - 如果我使用 Terraform 导入 AWS IAM 用户,附加到用户的策略和访问 key 是否也会被导入?

azure - terraform 显示返回 4 个 json 对象

database - Liquibase 和 Terraform 最佳实践

amazon-web-services - Terraform-从参数存储中获取值并传递给资源

amazon-web-services - 网络接口(interface)与子网 ID 冲突 - Terraform AWS 提供商

java - (SpringBoot 使用 Amazon S3 存储桶调用 Textract): error The method builder() is undefined for the type Document