Bird
0
0

You want to create an EC2 instance that runs a script at launch to install Apache and then writes the instance's private IP to a file. Which user data script correctly achieves this?

hard📝 Application Q8 of 15
AWS - EC2 Fundamentals
You want to create an EC2 instance that runs a script at launch to install Apache and then writes the instance's private IP to a file. Which user data script correctly achieves this?
A#!/bin/bash sudo yum install -y httpd PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start httpd
B#!/bin/bash sudo yum install -y httpd PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start httpd
C#!/bin/bash sudo yum install -y nginx PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start nginx
D#!/bin/bash sudo apt-get install -y apache2 PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start apache2
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct package manager and service for Amazon Linux

    Amazon Linux uses yum and httpd for Apache.
  2. Step 2: Check metadata IP type

    Private IP is at local-ipv4, so #!/bin/bash sudo yum install -y httpd PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start httpd uses correct metadata path.
  3. Step 3: Verify service start and file path

    #!/bin/bash sudo yum install -y httpd PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start httpd starts httpd and writes IP to /var/www/html/ip.txt correctly.
  4. Final Answer:

    #!/bin/bash sudo yum install -y httpd PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP > /var/www/html/ip.txt sudo systemctl start httpd -> Option A
  5. Quick Check:

    Use local-ipv4 for private IP in metadata [OK]
Quick Trick: Use local-ipv4 metadata for private IP, yum for Amazon Linux [OK]
Common Mistakes:
  • Using public-ipv4 instead of private IP
  • Using wrong package manager for OS
  • Installing nginx instead of Apache

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AWS Quizzes