Obtaining Public IP of an EC2 Instance on AWS | Script Configuration

How to Obtain the Public IP of an EC2 Instance for Script Configuration

Question

A Systems Administrator is writing a configuration script that requires the public IP of an Amazon EC2 instance on which it is executed. How can the System Administrator obtain the IP address without using the AWS Management Console?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer: B.

You can access the local IP address of your instance from instance metadata.

The instance metadata is available from your running instance, so no need to use the Amazon EC2 console or the AWS CLI.

Option A is incorrect because OS signifies Windows, Linux, etc doesn't tell anything about the IP address, and no need to follow difficult steps if you are getting the results easily.

Options C and D are incorrect because they require an AWS console to get the requested results.

Instance metadata is data about your instance that you can use to configure or manage the running instance.

You can also use instance metadata to access user data that you specified when launching your instance.

For example, you can specify parameters for configuring your instance, or include a simple script.

Although you can only access instance metadata and user data from within the instance itself, the data is not protected by authentication or cryptographic methods.

Anyone who has direct access to the instance, and potentially any software running on the instance, can view its metadata.

Therefore, you should not store sensitive data, such as passwords or long-lived encryption keys, as user data.

For more information on Instance metadata, please visit the below URL-

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
This example gets the top-level metadata items. For more information, see Instance Metadata Categories.

[ec2-user ~]$ curl http: //169.254.169.254/latest/meta-data/
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname

iam/
instance-action
instance-id
instance-type
local-hostname
local-ipva

mac

metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/

The correct answer is B. From the instance metadata.

Explanation:

Instance metadata is a service provided by the Amazon EC2 instances that allows you to retrieve information about the instance such as the instance ID, hostname, public IP address, and more, without the need to access the instance itself. This metadata can be accessed from within the instance or through the EC2 API.

To retrieve the public IP address of an EC2 instance using the instance metadata, you can make a simple HTTP request to the following URL from within the instance:

http://169.254.169.254/latest/meta-data/public-ipv4

This will return the public IP address of the instance. You can also retrieve the private IP address using the following URL:

http://169.254.169.254/latest/meta-data/local-ipv4

If you are accessing the instance metadata from outside the instance, you can use the EC2 API. For example, you can use the following AWS CLI command to retrieve the public IP address of an instance:

aws ec2 describe-instances --instance-ids <instance-id> --query 'Reservations[].Instances[].PublicIpAddress' --output text

This will return the public IP address of the instance with the specified instance ID.

Option A (From the operating system of the EC2 instance) is not a reliable way to obtain the public IP address of an EC2 instance, as the IP address can change every time the instance is stopped and started again.

Option C (From the user data) is not a valid way to obtain the public IP address of an EC2 instance, as user data is used to pass custom data to an instance during launch and does not contain information about the instance itself.

Option D (From the AMI that was used to launch the EC2 instance) is also not a valid way to obtain the public IP address of an EC2 instance, as the AMI only contains information about the software and configuration used to launch the instance, not information about the instance itself.