AWS EC2 : Essential Commands

AWS Commands

Get Current User Details

To retrieve information about the current AWS user, use the following command:

aws sts get-caller-identity

#To view the configured AWS CLI settings, including access key, secret key, and region, run:
aws configure list

List AWS User Profiles

To see a list of AWS CLI named profiles on your system, execute:

aws configure list-profiles

Add a New User Profile

To add a new AWS CLI user profile, use the following command, replacing with your desired profile name:

aws configure --profile <profile_name>

Ubuntu

Update Ubuntu and Install Git

Ensure your system is up-to-date and install Git with the following commands:

sudo apt-get update
sudo apt-get install git

Install and Activate NVM (Node Version Manager)

Install NVM to manage multiple Node.js versions. Run these commands:

sudo su -
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

Install Latest Node.js

Use NVM to install the latest version of Node.js:

nvm install node

List Users

To view a list of users on your Ubuntu system, use:

cat /etc/passwd | cut -d: -f1

Switch to Root/User

Switch to the root user:

sudo su -

#Switch to a specific user:
sudo su - username

Set Environment Variables on AWS EC2

Set an environment variable:

export ENV_NAME="value"

#To persistently set environment variables, edit the .bashrc file:
nano ~/.bashrc

#Add your variable and save. Then, refresh the file:
source ~/.bashrc

Screen Commands

Create a new screen:

screen -S your-screen-name

#List screens:
screen -ls

#Resume a screen session:
screen -r your-screen-name

#Terminate a command within a screen:
Ctrl-c
exit

Nginx Configuration

For Nginx configuration files, navigate to the following directory:

# install cmd
sudo apt install nginx
sudo apt remove nginx

#Ngnix config file
cd /etc/nginx/sites-enabled

#Ensure your Nginx configuration is correct:
sudo nginx -t

#Reload Nginx:
sudo systemctl reload nginx

#Restart Nginx:
sudo systemctl restart nginx

#Check Nginx status:
sudo systemctl status nginx

#demo file

server {
    listen 80;
    listen [::]:80 default_server;
    server_name example.example.com d4i1vtix75wsmg.cloudfront.net;

    location / {
        proxy_pass http://1.11.111.111:8081;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        # Use the actual domain or IP address of your backend server
        proxy_set_header Host example.example.com;
        proxy_set_header X-Forwarded-Cookie $http_cookie;
    }

   # Second location option for path "/auth/callback", "/api/*".

   location ~ ^/(auth|api|auth/callback) {
        # Additional handling for specific paths
        # For example, you may add custom headers or modify cookies here
        proxy_pass http://1.11.111.111:8081;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host example.example.com;
        proxy_set_header X-Forwarded-Cookie $http_cookie;
    }
}