7

I've noticed recently Terraform or AWS has made a change EC2 userdata so that changes don't trigger a 'replace' in terraform. Quote Terraform aws_instance docs: Updates to this field will trigger a stop/start of the EC2 instance.

We have automated deployments that rely on userdata changes to trigger replacements, so that the new userdata is ran everytime.

Is there a way Terraform can be told to replace instances is it notices new userdata? Lambda has a similar issue that is solved with 'source_code_hash' Is there something similar I can use here?

Thanks.

2 Answers2

5

Good news, terraform-provider-aws has an open issue ticket where many are reporting this issue. Feedback from individuals who rely on user_data to initiate a replace on their EC2 instances have been reported. This includes CI/CD pipelines that are now broken.

jaredholgate mentions they are planning to add a parameter called user_data_replace_on_change - it will be false by default.

In the meantime, you can use the -replace flag to target your ec2 instance. For example:

terraform plan -replace aws_instance.my_ec2_instance
terraform apply -replace aws_instance.my_ec2_instance

For citation purposes, this issue has been reported at: https://github.com/hashicorp/terraform-provider-aws/issues/23315

wykydtronik
  • 98
  • 1
  • 8
0

The change has come from within a new Terraform AWS provider version

If you are running userdata with cloud-init an easy fix would be to use the cloud-init type x-shellscript-per-boot instead of x-shellscript.

This would mean any userdata updates would be ran every stop/start.