The example you’ve shared here includes a document marker, which is part of the YAML syntax for encoding a series of documents into a stream, and can separately be used to avoid some ambiguity that might otherwise arise if a single document contains Directives.
Terraform’s yamlencode function can only produce Bare Documents, because its input is a single data structure which corresponds with a single document in YAML’s terms. yamlencode also never generates YAML Directives, and so there is no risk of the syntax ambiguity that can arise in a single document with directives.
Before getting into ways you might create the effect of multiple documents even though Terraform’s function isn’t designed for that, I want to note that if the system you are sending this message to only requires a single document anyway then it may work to just send the yamlencode function’s output directly, even though it doesn’t include a document marker. The YAML specification does not describe any difference in result for a bare document vs. single Explicit Document, and so lots of YAML-parsing software will accept a single document in both forms.
If you find that the software you’re sending this message to does require an single explicit document, or if you need to generate a YAML Stream containing multiple explicit documents, Terraform does not have any YAML-specific mechanism for doing so but you can get the effect of doing so by inserting the --- sequence explicitly yourself using template syntax. For example: