By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I send user-data output to the console logs on an EC2 instance for Amazon Linux 1, Amazon Linux 2, or Amazon Linux 2023?

3 minute read
1

I want to log the user data invocation and ship it to the console logs on my Amazon Elastic Compute Cloud (Amazon EC2) Linux instance.

Short description

To troubleshoot issues with your EC2 instance, modify your user-data bash script to redirect all output to the console. Redirect the output to both /var/log/user-data.log and /dev/console. When the script runs, you can view the user-data invocation logs directly in your console.

Note: This resolution is only for Amazon Linux 1, Amazon Linux 2, and Amazon Linux 2023. For information on instances that run RHEL 7 or RHEL 8, see How can I send user-data output to the console logs on an EC2 instance that's running RHEL 7 or RHEL 8?

Resolution

Complete the following steps:

  1. Stop the instance.

  2. Choose Actions, choose Instance Settings, and then choose Edit User Data.

  3. Add the following script, and then choose Save.

    Content-Type: multipart/mixed; boundary="//
    "MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash -xe
    
    exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
    cat /etc/os-release | grep PRETTY_NAME
    echo "Hello from user-data!"
    
    --//--

    Note: The script to redirect user-data output begins with the line #!/bin/bash -xe. The previous script is cloud-init configuration data that runs a shell script every time the instance starts up. For more information, see How can I utilize user data to automatically run a script with every restart of my Amazon EC2 Linux instance? In the previous command, the following line redirects the user-data output:

    exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

    The following is an example console output from an Amazon Linux 2023 instance:

    <13>Aug 13 18:43:39 user-data: + cat /etc/os-release
    <13>Aug 13 18:43:39 user-data: + grep PRETTY_NAME
    <13>Aug 13 18:43:39 user-data: PRETTY_NAME="Amazon Linux 2023.5.20240805"
    <13>Aug 13 18:43:39 user-data: + echo 'Hello from user-data!'
    <13>Aug 13 18:43:39 user-data: Hello from user-data!

    Note: The posted system (console) log output isn't updated continuously. Instances built on the Nitro system support the retrieval of the latest serial console output. For more information, see Instance console output.

Related information

Run commands when you launch an EC2 instance with user data input

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago