Gaurav Tandon
Gaurav Tandon

Reputation: 131

Terraform Apply Failed - InvalidUserData.Malformed: Invalid BASE64 encoding of user data

Question: How do you use base64encode Terraform Function in cdktf?

From what I could find, these Terraform Function are not supported yet. Or are they now?

My usecase is to "load BASE64 encoded userData for an ec2 launchTemplate"

When I try using a nodejs method to do the same:

import { readFileSync } from 'fs';`
.
.
const userTemplate = readFileSync('./user_template.txt', {encoding: 'utf-8'});
.
.
const launchTemplate = new LaunchTemplate(this, "launchTemplate", {
.
.
userData: userTemplate,
.

It fails with this error:

Error: InvalidUserData.Malformed: Invalid BASE64 encoding of user data.

Any suggestions how I could pass userData to LaunchTemplate using cdktf?

Upvotes: 1

Views: 6182

Answers (1)

Gaurav Tandon
Gaurav Tandon

Reputation: 131

Figured out the issue. It seems object created by readFileSync returns buffer or String based on what you pass. When I pass it like this

const userTemplate = readFileSync('./user_template.txt', 'base64');

It returns string encoded in base64. That is exactly what I was after.

Upvotes: 1

Related Questions