User data terraform

0

Hello, I followed this terraform documentation https://registry.terraform.io/providers/serverscom/serverscom/latest/docs/guides/user-data

This is my user data, user_data =<<-EOF #!/bin/bash apt update -y apt install -y apache2 systemctl enable apache2 systemctl start apache2 echo "<h1>Welcome to Structure AWS with Terraform </h1>" | sudo tee /var/www/html/index.html EOF

But I keep getting this error, │ Error: creating EC2 Launch Template (weblt): InvalidUserData.Malformed: Invalid BASE64 encoding of user data. │ status code: 400, request id: 3683f5ca-aaf7-4019-b90b-971a66870cff │ │ with aws_launch_template.weblt, │ on Weblaunchtemplate.tf line 1, in resource "aws_launch_template" "weblt": │ 1: resource "aws_launch_template" "weblt" {

what could be the issue?

Edy
asked 20 days ago43 views
2 Answers
0

maybe try this way:

resource "aws_instance" "example" {
  # other instance configuration...

  user_data = base64encode(local.user_data_script)
}

locals {
  user_data_script = <<EOF
#!/bin/bash
apt update -y
apt install -y apache2
systemctl enable apache2
systemctl start apache2
echo "<h1>Welcome to Structure AWS with Terraform </h1>" > /var/www/html/index.html
EOF
}

profile picture
answered 20 days ago
  • Change user_data to user_data_base64 if its base64 encoded as per your example

0

Place your UserData into a Shell script file and then reference it instead of placing it in the Raw terraform:-

/script.sh

#!/bin/bash
apt update -y
apt install -y apache2
systemctl enable apache2
systemctl start apache2
echo "<h1>Welcome to Structure AWS with Terraform </h1>" | tee /var/www/html/index.html
resource "aws_instance" "web" {
  ami                    = var.image_id
  instance_type          = var.instance_type
  key_name               = aws_key_pair.key-tf.key_name
  vpc_security_group_ids = ["${aws_security_group.allow_tls.id}"]
  tags = {
    Name = "first-tf-instance"
  }
  user_data = file("${path.module}/script.sh")
}
profile picture
EXPERT
answered 20 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions