Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
user_data not getting executed over instance creation

I have provided an .sh file as user_data in my Terraform script:

resource "aws_instance" "alpha-terraform"{
    ami=var.amis[var.region]
    instance_type="t2.micro"
    vpc_security_group_ids=[aws_security_group.alpha-terraform-sg.id]
    user_data = file("user_data_bootstrap.sh")
    key_name = "alpha-terraform"

    tags={
        type="alpha"
        purpose="terraform"        
    }
}

The .sh file contains:

#Jenkins Installation:
sudo apt update
sudo apt install openjdk-8-jdk --yes
wget –q –O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add –
sudo sh -c 'echo deb https://pkg.jenkins.io/debian binary/ >> /etc/apt/sources.list'
sudo apt update
sudo apt install Jenkins
sudo systemctl status jenkins
find /usr/lib/jvm/java-1.8* | head -n 3

My instance is getting created all right. However, I believe my user_data is not getting executed. I can see the user_data is being copied to /var/lib/cloud/instance/ as user-data.txt and user-data.txt.i but still not getting executed.

Why is this happening and how can I debug it further?

Update

I tried accessing the server through and executing each command one by one but getting following error while updating apt packages:

ubuntu@ip-172-31-52-**:~$ apt update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
ubuntu@ip-172-31-52-**:~$ sudo apt update
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I verified the script on my sandbox account.

It had mistakes. The corrected version is as follows:

#!/bin/bash -xe

#Jenkins Installation:
apt update
apt install openjdk-8-jdk --yes
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
echo "deb https://pkg.jenkins.io/debian binary/" >> /etc/apt/sources.list
apt update
apt install -y jenkins
systemctl status jenkins
find /usr/lib/jvm/java-1.8* | head -n 3

The changes were:

  • removal of sudo
  • fixing – to -
  • fixing apt install Jenkins to apt install -y jenkins

I can confirm that it works now with terraform on Ubuntu 18.04 instance.

Upon launching the instance, jenkins is available at http://<instance-ip>:8080:

enter image description here

over 4 years ago · Santiago Trujillo Denunciar

0

You're missing #!/bin/bash at the beginning of the file. From the documentation:

User data shell scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash). For a great introduction on shell scripting, see the BASH Programming HOW-TO at the Linux Documentation Project (tldp.org).

over 4 years ago · Santiago Trujillo Denunciar

0

I will prefer templatefile over file where you have more control and pass variable to user data as well.

templatefile reads the file at the given path and renders its content as a template using a supplied set of template variables where file reads the contents of a file at the given path and returns them as a string.

templatefile(path, vars)
data "template_file" "backend_cloud_init" {
  template = "${file("user_data_bootstrap.sh")}"
  vars = {
    JDK_VERSION = "openjdk-8-jdk"
  }
}

then use the template_file in a userdata section

resource "aws_instance" "alpha-terraform"{
    ami=var.amis[var.region]
    instance_type="t2.micro"
    vpc_security_group_ids=[aws_security_group.alpha-terraform-sg.id]
    user_data                   = "${data.template_file.backend_cloud_init.rendered}"
    key_name = "alpha-terraform"

    tags={
        type="alpha"
        purpose="terraform"        
    }
}

Also, add the hashbang in the file with debug

#!/usr/bin/env bash
set -x
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda