> For the complete documentation index, see [llms.txt](https://asiabots.gitbook.io/learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://asiabots.gitbook.io/learning/aws.md).

# AWS

## Connecting to Linux Instance from Windows Using PuTTY

convert AWS .pem to .ppk for putty/WinSCP

{% embed url="<https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html>" %}

**To convert your private key**

1. Start PuTTYgen (for example, from the **Start** menu, choose **All Programs > PuTTY > PuTTYgen**).
2. Under **Type of key to generate**, choose **RSA**.![
   &#x9;						RSA key in PuTTYgen
   &#x9;					](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/images/puttygen-key-type.png)

   If you're using an older version of PuTTYgen, choose **SSH-2 RSA**.
3. Choose **Load**. By default, PuTTYgen displays only files with the extension `.ppk`. To locate your `.pem` file, select the option to display files of all types.![
   &#x9;						Select all file types
   &#x9;					](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/images/puttygen-load-key.png)
4. Select your `.pem` file for the key pair that you specified when you launched your instance, and then choose **Open**. Choose **OK** to dismiss the confirmation dialog box.
5. Choose **Save private key** to save the key in the format that PuTTY can use. PuTTYgen displays a warning about saving the key without a passphrase. Choose **Yes**.

   Note

   A passphrase on a private key is an extra layer of protection, so even if your private key is discovered, it can't be used without the passphrase. The downside to using a passphrase is that it makes automation harder because human intervention is needed to log on to an instance, or copy files to an instance.
6. Specify the same name for the key that you used for the key pair (for example, `my-key-pair`). PuTTY automatically adds the `.ppk` file extension.

Your private key is now in the correct format for use with PuTTY. You can now connect to your instance using PuTTY's SSH client.

## Check Opened/Closed port

* `-l` or `--listening` shows only the sockets currently listening for incoming connection.
* `-a` or `--all` shows all sockets currently in use.
* `-t` or `--tcp` shows the tcp sockets.
* `-u` or `--udp` shows the udp sockets.
* `-n` or `--numeric` shows the hosts and ports as numbers, instead of resolving in DNS and looking in /etc/services.

```bash
netstat -atn           # For tcp
netstat -aun           # For udp
netstat -atun          # For both
```

## Check which specific processes (Python scripts) are running

```bash
pgrep -lf python
```

This, however, does not list the whole command line. If you have a recent version of pgrep you can use -a to do this:

```bash
pgrep -af python
```

### Kill running processes

e.g. Kill all running python processes

```bash
pkill -9 python
```

## Python Installation

In order to use "sudo pip XXX"

```bash
sudo apt-get update
sudo apt-get install python-pip
```

```bash
sudo pip install XXX
```

## Running a Flask app on AWS EC2

<https://www.datasciencebytes.com/bytes/2015/02/24/running-a-flask-app-on-aws-ec2/>

## Running a Flask app with Linux startup

### Create service configuration

```bash
sudo nano /etc/init/flask.conf
```

{% code title="/etc/init/flask.conf" %}

```bash
description "flask"

start on stopped rc RUNLEVEL=[2345]
respawn
exec python /home/user/server.py
```

{% endcode %}

### Create flask.service

```bash
sudo nano /lib/systemd/system/flask.service
```

{% code title="flask.service" %}

```bash
[Unit]
Description=Flask web server

[Install]
WantedBy=multi-user.target

[Service]
User=root
Group=root
PermissionsStartOnly=true
ExecStart=/usr/bin/python3 /home/ubuntu/NLPv3/main.py
TimeoutSec=600
Restart=on-failure

```

{% endcode %}

### Run flask.service

```bash
sudo systemctl start flask
sudo systemctl stop flask
sudo systemctl status flask
```

REF:

{% embed url="<https://askubuntu.com/questions/666423/run-script-at-startup-in-etc-init-d>" %}

sudo systemctl start flask.

REF:

{% embed url="<https://blog.csdn.net/liang19890820/article/details/74011983>" %}

{% embed url="<https://medium.com/@manivannan_data/how-to-deploy-the-flask-app-as-ubuntu-service-399c0adf3606>" %}

## Running Python in AWS Lambda

<https://stackoverflow.com/questions/53686252/api-gateway-lambda-proxy-python-internal-server-error>

### Example Code

#### AWS serverless app example in github

<https://github.com/aws-samples/serverless-app-examples/tree/master/python>

<https://github.com/crazyfan5566/aws_lambda_for_python_example/tree/master/example>

#### access dynamo database

&#x20;<https://github.com/aws-samples/serverless-app-examples/blob/master/python/microservice-http-endpoint-python3/lambda_function.py>

### building fb messenger chatbot

<https://www.r-bloggers.com/tutorial-facebook-messenger-chatbot-with-aws-lambda-python-written-by-an-r-programmer/>

<https://github.com/AdamSpannbauer/aws_python_messenger/blob/master/parrot_lambda_env/lambda_function.py>

### MySQL

<https://www.isc.upenn.edu/accessing-mysql-databases-aws-python-lambda-function>
