AWS EC2 User Data

feature image

AWS EC2 User Data

User data means the launch script for your EC2. For example, you can create a User Data to download a website from git and put it to the web folder. So you do not need to do any setup by SSH after launch.

Another function is do some basic setup so we do not need to do it every time when we launch like update and install some common packages.


Ubuntu

Update

Run:

sudo apt update
sudo apt -y upgrade
sudo apt install -y unzip jq

Amazon Linux 2

Update

Run:

sudo yum update
sudo yum install -y amazon-efs-utils jq

To update all packages on an Amazon Linux instance

(Optional) Start a screen session in your shell window. Sometimes you might experience a network interruption that can disconnect the SSH connection to your instance. If this happens during a long software update, it can leave the instance in a recoverable, although confused state. A screen session allows you to continue running the update even if your connection is interrupted, and you can reconnect to the session later without problems.

  1. Execute the screen command to begin the session.

Run:

screen
  1. If your session is disconnected, log back into your instance and list the available screens.

Run:

screen -ls

Output:

17793.pts-0.ip-12-34-56-78	(Detached)
1 Socket in /var/run/screen/S-ec2-user.
  1. Reconnect to the screen using the screen -r command and the process ID from the previous command.

Run:

screen -r 17793
  1. When you are finished using screen, use the exit command to close the session.

Run:

exit

Output:

[screen is terminating]

, ,