Skip to main content

base64encode Function

base64encode applies Base64 encoding to a string.

OpenTofu uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.

Strings in the OpenTofu language are sequences of unicode characters rather than bytes, so this function will first encode the characters from the string as UTF-8, and then apply Base64 encoding to the result.

The OpenTofu language applies Unicode normalization to all strings, and so passing a string through base64decode and then base64encode may not yield the original result exactly.

While we do not recommend manipulating large, raw binary data in the OpenTofu language, Base64 encoding is the standard way to represent arbitrary byte sequences, and so resource types that accept or return binary data will use Base64 themselves, and so this function exists primarily to allow string data to be easily provided to resource types that expect Base64 bytes.

base64encode is, in effect, a shorthand for calling textencodebase64 with the encoding name set to UTF-8.

Examples

Code Block
> base64encode("Hello World")
SGVsbG8gV29ybGQ=
  • base64decode performs the opposite operation, decoding Base64 data and interpreting it as a UTF-8 string.
  • textencodebase64 is a more general function that supports character encodings other than UTF-8.
  • base64gzip applies gzip compression to a string and returns the result with Base64 encoding all in one operation.
  • filebase64 reads a file from the local filesystem and returns its raw bytes with Base64 encoding, without creating an intermediate Unicode string.