Create ec2 with snapshot

Hi,

I’m tried to create a EC2 from snapshot, but I don’t attach the new volume to new vm via terraform script.

data "aws_ebs_volume" "prod_volume" {
  most_recent = true

  filter {
    name   = "volume-id"
    values = ["vol-xxxxxxxxxxxx"]
  }
}

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

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

resource "aws_instance" "non_production" {
    ami               = "ami-0b898040803850657"
    availability_zone = "us-east-1a"
    instance_type     = "t2.micro"
    key_name = "mykey"
    vpc_security_group_ids = [ "sg-xxxxxx","sg-xxxxxxrr" ]
    tags = {
    Name   = "MyNew-Server"
    Server = "MyNew-Server"
  }
}

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

I can’t see anything obviously wrong with the code. what happens when you run it, are there any error messages?

What’s the TF output from the creation of the volume attachment resource?

Tks Bentterp!

I resolved it with other options…

resource “aws_ami” “my_new_ami” {
name = “new-ami”
virtualization_type = “hvm”
root_device_name = “/dev/xvda”

ebs_block_device {
device_name = “/dev/xvda”
# The last Snapshot ID I want
snapshot_id = “snap-xxxxxxxx”
volume_size = 20
}
tags = {
Name = “My-Snap-ami”
}
}

resource “aws_instance” “newmachine” {
ami = “${aws_ami.my_new_ami.id}”
instance_type = “t2.micro”
key_name = “my_key”
vpc_security_group_ids = [ “sg-6xxx71e”,“sg-exxxxx97” ]

tags = {
Name = “MyNew-VM”
}
}