0

i lanched an ec2 instance (ubuntu), and i have a script which looks like :

#!/bin/bash

# works fine (even directly in terminal or via user_data)
hostnamectl set-hostname ${hostname}

# works fine as well (even directly in terminal or via user_data)
echo "127.0.0.1 ${hostname}" >> /etc/hosts

# it works in terminal BUT NOT via user_data, why ?
curl -LsSf https://astral.sh/uv/install.sh | sh

i want to install uv (python package) with last code in my script above BUT it does not works using user_data, why ?

Update : This script named and located at remote-servers-script/installer.sh, and referenced in a terraform module which looks like :

  module "virtual_machine" {
  source        = "./virtual_machine"
  ami_id        = var.ec2_ami_id
  instance_type = "c7i-flex.large"

  for_each                      = toset(var.vms_names)
  public_key                    = var.public_key
  tag_name                      = each.key
  enable_public_ip_address      = each.key == "jump-server" ? true : false
  sg_for_jump_server            = each.key == "jump-server" ? [module.security_group.sg_ec2_jump_server] : [module.security_group.sg_ec2_clickhouse_instances]
  subnet_id                     = each.key == "jump-server" ? tolist(module.networking.free_iliad_public_subnets)[0] : tolist(module.networking.free_iliad_private_subnets)[0]
  user_data_install_jump_server = templatefile("./remote-servers-script/hostname.sh", { hostname = each.key }) # <== referenced here
}
3
  • 1
    edit your question to tell is in what way "it does not work", show the contents of user_data (which I assume is a bash variable but idk). and show how you're trying to use it in your script. Also, copy/paste your script into shellcheck.net and fix the issues it tells you about. Commented Oct 22 at 12:00
  • user_data runs as root in a different environment setting than the user when you ssh into an instance, this might cause issues with the script. You can check the cloud init logs to see how it fails that might help troubleshoot. Once you do you can update the questions with more details. Commented Oct 22 at 12:32
  • i have updated my post if the details can helps Commented Oct 23 at 21:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.