In the code above:
We define a local block by using the keyword locals and then an opening {.
We define each local on a new line by giving it a name. The first local we define is called first_part.
We then follow it with an = and give it a value. For the first_part local, we give it the value of the string literal hello.
For the second local second_part, we are use the value "${local.first_part}-there".
As the whole expression is inside quotes, we need to use the ${ and } around our expression so Terraform evaluates it.
To reference a local, we use the expression syntax local.local_identifier.
The second_part local will be set to “hello-there”. In the bucket_name local, we are using the second_part local in the expression "${local.second_part}-how-are-you-today" which will evaluate to hello-there-how-are-you-today.
At the bottom of the project, we define an S3 bucket and set the name to local.bucket_name. This will create an S3 bucket with the name hello-there-how-are-you-today.
đź“ťNote: We do not need the ${ and } as we are not inside quotes here.
We could also have set the bucket to "${local.bucket_name}", which would be evaluated to the same thing.
In Terraform version 1.0.0, though, we can now omit the ${ and } for a single line expression where we are using the whole value. This makes the code cleaner and easier to read. Easily read code becomes vital when we define our infrastructure as code.