내용으로 건너뛰기

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년 전2천회 조회

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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

관련 콘텐츠