skip to Main Content

I’m trying to get ec2 instance id and assign it into environment variable with a script.
I found some command that supposed to give me the id but they don’t work.

The curl command returns nothing:
curl command,
I tried this way also: export INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id, but the variable is empty.

The cat /sys/devices/virtual/dmi/id/board_asset_tag command prints the id, but when I try to assign it to the variable, I get Permission Denied error.

Any ideas why curl returns nothing, or any other way to get the instance id?
Thanks

2

Answers


  1. If you have cloud-utils on your machine, you can try

    INSTANCE_ID=$(ec2metadata --instance-id)
    
    Login or Signup to reply.
  2. As per comment the problem was solved by using the ec2-metadata tool

    ec2-metadata -i
    

    On the Amazon Linux AMI, there is a tool already pre-installed called ec2-metadata which can be run from the command line interface (CLI).

    If its other than amazon ami then install it first and then use it

    # Download the ec2-metadata script
    wget http://s3.amazonaws.com/ec2metadata/ec2-metadata
    
    # Modify the permission to execute the bash script
    chmod +x ec2-metadata
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search