We are using Terraform to create the following:
- EC2 using the templatefile function to run a bash script to install software and do to configuration.
- Create an ami off the instance build in (1.)
- Build another EC2 instance in another VPC
Problem, the AMI seems to create before Number 1 instance has finished running the example.sh.tpl script, causing the VPC_BUILD EC2 instance not to have all the software, configuration and directories of the first instance build_example
Is there a way in Terraform to have the first EC2 instance to stop be for the aws_ami_from_instance resource starts?
resource "aws_instance" "build_example" {
ami = "ami-0e763a959ec839f5e"
instance_type = "t2.micro"
userdata = templatefile(example.sh.tpl",{ name = "John" })
tags = {
Name = "Build01"
}
}
resource "aws_ami_from_instance" "example_2" {
name = "terraform-example_2"
source_instance_id = "aws_instance.build_example.id"
tags = {
Name = "Build01-AMI"
}
}
resource "aws_instance" "another_vpc_example" {
ami = "aws_ami_from_instance.example_2.id"
instance_type = "t2.micro"
userdata = templatefile(example.sh.tpl",{ name = "John" })
tags = {
Name = "VPC_BUILD"
}
}