hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.8k stars 9.15k forks source link

[Bug]: User data update is triggering a start stop but the output block is giving the older public ip address of the ec2 instance #33726

Open sanjeevkr1 opened 1 year ago

sanjeevkr1 commented 1 year ago

Terraform Core Version

1.5.7

AWS Provider Version

5.19.0

Affected Resource(s)

Affected resource - aws_instance.

Expected Behavior

The updated value should be given in output

Actual Behavior

Previous IP address is given in output

Relevant Error/Panic Output Snippet

I am on below terraform and aws provider version $ terraform --version Terraform v1.5.7 on windows_386

I have the below code sample code(variables are substituted properly )

resource "aws_instance" "ec2-demo1" {
  ami           = var.amzn-ami-id
  instance_type = var.instance_type
  key_name      = var.key_name

  tags = {
    Name = "ec2-demo"
  }

  user_data = file("${path.module}/app1-install.sh")
  //user_data_replace_on_change = true

}
output "ec2-instance-public-ip" {
  description = "Print the public ip of the created EC2 instance"
  value       = aws_instance.ec2-demo1.public_ip
}

Issue is that whenever I update the user-data sh script, the ec2 resource is stopped and restarted but in output the previous instance's public address is printed. Below is what is see in the state file

"outputs": {
    "ec2-instance-public-ip": {
      "value": "54.173.141.184",
      "type": "string"
    }

// elsewhere in same state file 
"public_dns": "ec2-54-165-159-38.compute-1.amazonaws.com",
 "public_ip": "54.165.159.38",

--- Output from stdout

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Outputs:

ec2-instance-public-ip = "54.173.141.184"

When i set the flag user_data_replace_on_change, the correct IP address is given in output

Terraform Configuration Files

resource "aws_instance" "ec2-demo1" {
  ami           = var.amzn-ami-id
  instance_type = var.instance_type
  key_name      = var.key_name

  tags = {
    Name = "ec2-demo"
  }

  user_data                   = file("${path.module}/app1-install.sh")
 // user_data_replace_on_change = true

}
output "ec2-instance-public-ip" {
  description = "Print the public ip of the created EC2 instance"
  value       = aws_instance.ec2-demo1.public_ip
}

variable "instance_type" {
  default = "t2.micro"
  type    = string

}

variable "amzn-ami-id" {
  default = "ami-03a6eaae9938c858c"
  type    = string
}

variable "key_name" {
  type    = string
  default = "terraform-key1"
}
# =============================
# app1-install.sh
# =============================

#! /bin/bash
# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
sudo yum update -y
sudo yum install -y httpd
sudo systemctl enable httpd
sudo service httpd start  
sudo echo '<h1>Welcome to StackSimplify - APP-1</h1>' | sudo tee /var/www/html/index.html
sudo mkdir /var/www/html/app1
sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);"> <h1>Welcome to Sanjeev's Terraform practice - APP-1</h1> <p>Terraform Demo</p> <p>Application Version: V1</p> </body></html>' | sudo tee /var/www/html/app1/index.html
#sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html
sudo TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` 
sudo curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html

Steps to Reproduce

  1. Run the above terraform config file which will create an ec2 instance using the sh file
  2. the output shows the public ip address of the ec2 instance
  3. change the user data in .sh file(any minor change)
  4. apply the changes with terraform apply
  5. the output message shows the previous public IP instead of the updated one.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue