スキップしてコンテンツを表示

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?

質問済み 2年前2013ビュー

2回答
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
}

回答済み 2年前

  • 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")
}
エキスパート

回答済み 2年前

エキスパート

レビュー済み 2年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

関連するコンテンツ