1

How do I find the name of "this" Lightsail instance. "This" being the instance that the aws command is being executed. My below script isn't working, since I thought Lightsail is just another EC2 instance.

#!/bin/bash
InstanceId=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
echo $InstanceId

Region=`aws configure get region`
echo $Region

InstanceName=$(aws ec2 describe-tags --region $Region --filters "Name=resource-id,Values=$InstanceId" "Name=key,Values=Name" --output text | cut -f5)
echo $InstanceName 
Les
  • 330
  • 5
  • 15
  • [What is difference between Lightsail and EC2?](https://stackoverflow.com/a/40932906/174777) suggests that the instance metadata should be available. What happens if you `curl http://169.254.169.254/latest/meta-data/` on the command-line? – John Rotenstein Feb 14 '19 at 21:12
  • The InstanceId does print out. It's the instance name that fails to print - just empty output. – Les Feb 15 '19 at 08:44

1 Answers1

3

The name of a Lightsail instance can be obtained with:

aws lightsail get-instances --query instances[].name

In my case, this was the auto-assigned name when I started the instance via the Lightsail management console. I couldn't see a way to change the name during launch.

Interestingly, I could not find a way to use the AWS CLI to list tags associated with a Lightsail instance. For example, I could not retrieve the Name tag that I manually added to an instance, and which appears in the Lightsail console.

Update:

After discussion in comments, I got this working:

aws lightsail get-instances --query "instances[?contains(supportCode,'`curl -s http://169.254.169.254/latest/meta-data/instance-id`')].name" --output text
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • That command gives me ALL the names of the Lightsail instances in my region. I'm looking for a single name - the name of the instance that the aws command is being issued. Much like I got the InstanceId of the instance I used the curl command. – Les Feb 16 '19 at 07:30
  • Oh! In that case, I couldn't see an easy way to do it. You can use `curl http://169.254.169.254/latest/meta-data/instance-id` to get the EC2 instance ID. However, `aws lightsail get-instances` only shows the ID as part of `supportCode`, so it would take some fiddling to match it. – John Rotenstein Feb 16 '19 at 11:23
  • This is actually helpful, now I just need to figure out how to match substrings in jq since the instanceName in the supportCode comes prepended with some numbers (don't know what they represent). This worked for me: aws lightsail get-instances | jq '.[] | map(select(.supportCode == "1234567890/i-0abcdefgh")) | .[].name' Now, I need to figure out how to do select(.supportCode contains instanceId) in jq – Les Feb 16 '19 at 15:52
  • Thanks, posted my solution based on your suggestions. Do you know whether the supportCode always has the format "1234567/i-abdcefg"? – Les Feb 16 '19 at 17:01
  • Added a solution. – John Rotenstein Feb 17 '19 at 22:20