I'm using terraform. I need to create ec2 instance with user data (that install several packages any other things). From this ec2 I need to create ami with same user data. Does terraform "wait" before create ami, so the ami has user data?
I've done a test where user data install docker, and every things works fine, but, what happen if the user data script is too heavy or the connection is too slow?
Here my code
resource "aws_instance" "golden_docker" {
ami = "ami-ID"
disable_api_termination = false
ebs_optimized = true
instance_type = "t3.medium"
root_block_device {
volume_size = 8
}
user_data = <<EOF
${file("${path.module}/init_golden.sh")}
EOF
}
resource "aws_ami_from_instance" "golden_ami" {
name = "ami-docker"
source_instance_id = aws_instance.golden_docker.id
}
EDIT: With a user_data like this:
cd /
sudo touch daniele
sudo chown ubuntu:ubuntu daniele
sudo echo $(date) > daniele
sudo echo "start" >> daniele
sleep 30
sudo touch "daniele-1"
sudo echo $(date) >> daniele
sleep 30
sudo touch "daniele-2"
sudo echo $(date) >> daniele
sleep 30
sudo touch "daniele-3"
sudo echo $(date) >> daniele
sleep 60
sudo touch "daniele-4"
sudo echo $(date) >> daniele
sudo touch "daniele-finito"
the script stop to execute when ami starts to build. So when ec2 status check is "passed". If my user_data is too heavy, the ami will not contains all commands, and the execution of user_data will stop.
Thank you