How can a variable be defined inside a resource or provisioner?

I have a block of code inside a resource of aws_instance with an inline remote-exec provisioner. This same block of code is identical on many EC2s except for the string value for the hostname of the instance. I want to define a variable in the resource, or the provisioner to set the value of the hostname so there is only one line to change between the resources.

It looked like locals was going to be the answer but when I try to add locals to the resource TF dies with a message about the identifier is reserved for future use.

I can work around this with shell magic but I would really like to know if there is a way to set a variable that is only local to the resource. The documentation shows locals defined outside the resource block, that is not what I want.