L
L
Leonid2022-02-22 15:56:52
Amazon Web Services
Leonid, 2022-02-22 15:56:52

Terraform does not respond to user_data changes. What's wrong?

There is a regular ec2 instance with user_data that is already working:

resource "aws_instance" "myWebServer" {
  ami                    = "ami-0c6ebbd55ab05f070" # Ubuntu Linux AMI
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.webServerSG.id]
  user_data = templatefile("externalScripts/user_data.tpl", {
    name  = "Leo",
    names = ["Vasya", "Petya"]
  })

  tags = {
    Name = "myWebServer"
  }

If you add something to user_data, then with terraform plan, instead of offering to delete the old instance and replace it with a new one, it offers to make only 1 change - change the hash from user_data, because he became different. As a result, after launch, it will change absolutely nothing on the server itself. what could be the problem? If it can be important, then elastic ip is still attached to this instance.
An example of adding data to user_data:
resource "aws_instance" "myWebServer" {
  ami                    = "ami-0c6ebbd55ab05f070" # Ubuntu Linux AMI
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.webServerSG.id]
  user_data = templatefile("externalScripts/user_data.tpl", {
    name  = "Leo",
    names = ["Vasya", "Petya", "MASHA"]
  })

  tags = {
    Name = "myWebServer"
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2022-02-25
@botiyava1

Most likely, here is your problem: https://github.com/hashicorp/terraform-provider-aw...
in short: in version 4.0 of the AWS provider for terraform, the following behavior is implemented: user data can be changed on the created (but turned off) instance and changing it should not cause the instance to be recreated. The documentation says that the instance should stop and start, but, as far as I understand, this does not happen for you.
Presumably, adding a random resource should help you solve the problem (for the resource re-creation trigger) https://registry.terraform.io/providers/hashicorp /... with the keeper as the result of the userdata template, and specifying it in depends_on for the instance resource

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question