# AWS CLI & Utility Functions

## **Overview**

The **Portico AWS Utility Functions** is a comprehensive bash script that simplifies common AWS operations for Portico development team. It provides an intuitive command-line interface for AWS profile management, EC2 operations, S3 file operations, and SSM session management.

### **Core Functions**

*   [**awsutils**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#awsutils) - Interactive menu launcher
    
*   [**aws-switch**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#aws-switch) - Select and activate AWS profile
    
*   [**aws-login**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#aws-login) - Login to AWS SSO
    
*   [**awswho**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#awswho) - Show current identity
    
*   [**ec2list**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#ec2list) - List EC2 instances
    
*   [**ssm**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#ssm) - Connect to EC2 via SSM
    
*   [**s3ls**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#s3ls) - List S3 buckets/objects
    
*   [**s3cp**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#s3cp) - Copy files to/from S3
    
*   [**aws-utils-help**](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#aws-utils-help) - Comprehensive help reference
    

AWS Vault Setup: [Portico AWS CLI Utility | AWS Vault and Local Credential Access Programmatically:](https://ithaka.atlassian.net/wiki/spaces/CM/pages/606076935/Portico+AWS+CLI+Utility#AWS-Vault-and-Local-Credential-Access-Programmatically%3A)

### **Key Features**

*   🔐 Easy AWS profile switching and SSO authentication
    
*   🖥️ EC2 instance management and SSH-less connectivity via SSM
    
*   📦 S3 bucket browsing and file operations
    
*   🎨 Color-coded output for better readability
    
*   📖 Interactive menu and comprehensive help system
    
*   ⚡ Quick shortcuts for common AWS tasks
    

## **Installation & Setup**

### 1\. **AWS CLI Installation**

*   Install AWS CLI v2 using PowerShell.
    

```plaintext
irm https://awscli.amazonaws.com/v2/install.ps1 | iex
```

*   Verify installation:
    

```plaintext
aws --version
```

*   Check installation path:
    

```plaintext
where.exe aws
```

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/be575f00-82b0-459f-9e79-62aa1ece725d.png align="center")

### 2\. **Config Setup**

Create the `.aws` directory and config file:

```plaintext
mkdir ~/.aws
cd ~/.aws
vi config
```

Paste the provided team-specific config (e.g., Apex team):

```plaintext
[profile portico-setup]
sso_start_url=https://ithaka-sso.awsapps.com/start
sso_region=us-east-1
sso_account_id=875219073553
sso_role_name=PorticoApexV1
region=us-east-1
cli_auto_prompt=off

[profile portico-apex]
sso_start_url=https://ithaka-sso.awsapps.com/start
sso_region=us-east-1
sso_account_id=720294270740
sso_role_name=PorticoApexV1
region=us-east-1
cli_auto_prompt=off
```

*   Paste the contents of the provided `.txt` file into `config`.
    
*   Save and exit (`:wq` in `vi`).
    

### 3\. **What is a Profile?**

*   A **profile** is like a workspace or environment.
    
*   It tells AWS CLI **which account, region, and role** to use.
    
*   Example:
    
    *   `portico-setup` → used when working in setup environment.
        
    *   `portico-apex` → used when working in Apex environment.
        
*   You switch profiles depending on where you’re working.
    

### 4\. **What is SSO?**

**SSO (Single Sign-On)** means you log in once and gain access to multiple AWS accounts without re‑entering credentials each time.

*   It uses your organization’s identity provider (like Okta or AWS SSO) to authenticate you securely.
    
*   In our config file:
    
    *   `sso_start_url` → the login page where authentication begins.
        
    *   Example: [`https://ithaka-sso.awsapps.com/start`](https://ithaka-sso.awsapps.com/start) is the starting point for login.
        

### 5\. **Explaining Config File Contents**

Each line in the config has meaning:

*   profile name → portico-setup or portico-apex (environment identifier)
    
*   sso\_start\_url → where login starts
    
*   sso\_region → region for authentication
    
*   sso\_account\_id → AWS account you’re logging into
    
*   sso\_role\_name → IAM role assigned to you
    
*   region → default AWS region for commands
    
*   cli\_auto\_prompt → whether CLI prompts interactively
    

## 6\. **Working with Multiple Profiles**

*   If you’re working in **setup environment**, use `portico-setup`.
    
*   If you’re working in **apex environment**, use `portico-apex`.
    

Switch profiles using:

```plaintext
aws-switch
```

Then log in with:

```plaintext
aws-login
```

## Installation Steps

### 1\. **Download the Script**

You created the utility script file using `vi`:

```plaintext
vi portico-aws-utils.sh
```

*   `vi` opens a text editor in the terminal.
    
*   Here you paste the script content and save it.
    
*   This file contains all the custom AWS utility functions.
    

### 2\. **Check Current Shell**

```plaintext
echo $SHELL
```

*   This command shows which shell you’re currently using.
    
*   Output: `/usr/bin/bash` confirms you’re already in Bash.
    
*   If not Bash, you would switch using:
    

```plaintext
chsh -s /bin/bash
```

### 3\. **Make Script Executable**

```plaintext
chmod +x portico-aws-utils.sh
```

*   `chmod +x` gives **execute permission** to the script.
    
*   Without this, you can’t run it as a program.
    

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/32e9309e-04ad-4891-aa37-e3a64b8dea91.png align="center")

### 4\. Download Session Manager Plugin with Curl

```plaintext
curl -L -o ~/Downloads/SessionManagerPlugin.zip "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/windows/SessionManagerPlugin.zip"

unzip ~/Downloads/SessionManagerPlugin.zip -d ~/Downloads/SessionManagerPlugin
```

*   `curl -L -o` → downloads the file and saves it as [`SessionManagerPlugin.zip`](http://SessionManagerPlugin.zip).
    
*   `unzip` → extracts the contents into a folder.
    

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/d49b6c1f-ce9f-4618-9fb4-b78cc2c71e86.png align="center")

## Session Manager Plugin Installation Explained

### 1\. **ls**

```plaintext
ls
```

*   Lists the files in the current directory.
    
*   Useful to confirm that [`package.zip`](http://package.zip) is present before extracting.
    

### 2\. **unzip -l** [**package.zip**](http://package.zip)

```plaintext
unzip -l package.zip
```

*   Shows the **contents of the zip file** without extracting.
    
*   You saw files like `LICENSE`, [`README.md`](http://README.md), and `bin/session-manager-plugin.exe`.
    
*   This confirms the plugin executable is inside.
    

### 3\. **unzip** [**package.zip**](http://package.zip)

```plaintext
unzip package.zip
```

*   Extracts all files from the archive into the current directory.
    
*   After this, the plugin files become available for use.
    

### 4\. **pwd**

```plaintext
pwd
```

*   Prints the **current working directory**.
    
*   Helps you confirm where the files were extracted (e.g., `~/Downloads/SessionManagerPlugin`)
    

### 5\. **ls -l**

```plaintext
ls -l
```

*   Lists files with **detailed information** (permissions, owner, size, modification date).
    
*   You saw `bin/session-manager-plugin.exe` with executable permissions.
    
*   This confirms the plugin is ready to run.
    

### 6\. **Running the Plugin**

```plaintext
~/session-manager-plugin/bin/session-manager-plugin.exe
```

*   Executes the plugin directly.
    
*   Output:  
    *“The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.”*
    
*   This means installation worked correctly.
    

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/f2fb47cd-70e4-40ad-acf5-4436d619e40e.png align="center")

### 6\. **Add Plugin to PATH**

```plaintext
export PATH="$HOME/session-manager-plugin/bin:$PATH"
which session-manager-plugin
```

*   Adds plugin location to your PATH so it can be run globally.
    
*   `which` confirms the location.
    
*   Running `session-manager-plugin` shows:  
    *“The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.”*
    

### 7\. Make PATH Permanent

```plaintext
echo 'export PATH="$HOME/session-manager-plugin/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
which session-manager-plugin
```

*   Adds the PATH setting to `.bashrc` so it persists after reboot.
    
*   Reloads `.bashrc` with `source`.
    
*   Confirms plugin location again.
    

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/e6d30432-ad5c-4dbb-8f98-f0d7d65d65e6.png align="center")

### 8\. Source Utility Script

Finally, source the utility script and start the menu:

```plaintext
source ~/portico-aws-utils.sh
source ~/.bash_profile
awsutils
```

*   `source` loads the script into your shell.
    
*   `awsutils` launches the interactive menu with all AWS utility functions.
    

![](https://cdn.hashnode.com/uploads/covers/686d39c1d15c5de72abaf85e/c0cf9103-01ba-456d-981b-66f91587a813.png align="center")
