# AWS Set up
###### tags: AppWorks
> [name=pohanlu]
>
> [time=Mon, Sep 5, 2022]
[TOC]
# Create account
1. Click 👉 [here](https://portal.aws.amazon.com/billing/signup#/start/email) 👈 to sign up AWS account
2. Follow the steps to finish Identity authentication
3. Log in your AWS account
# Create a key pair
:::info
- AWS uses public-key cryptography to secure the login information for your instance.
- You specify the name of the key pair when you launch your instance, then provide the private key when you log in using SSH.
:::
1. Click 👉 [here](https://ap-northeast-1.console.aws.amazon.com/ec2/home?region=ap-northeast-1#KeyPairs:) 👈 and choose **Create key pair**!
2. Follow the graph to fill and choose
- For Name, enter a descriptive name for the key pair.
- Amazon EC2 associates the public key with the name that you specify as the key name.

:::danger
- Private key file is automatically downloaded by your browser.
- This is the only chance for you to save the private key file.
:::
3. If you will use an SSH client on a macOS or Linux computer to connect to your Linux instance, use the following command to set the permissions of your private key file.
```bash=
chmod 400 key-pair-name.pem
```
# Create a security group
:::info
- Security groups act as a firewall for associated instances, controlling both inbound and outbound traffic at the instance level.
- You must add rules to a security group that enable you to connect to your instance from your IP address using SSH.
- If you plan to launch instances in multiple Regions, you'll need to create a security group in each Region.
:::
1. Click 👉 [here](https://console.aws.amazon.com/ec2/) 👈 to open Amazon EC2 console
2. From the top navigation bar, select a Region for the security group.

3. After choose region, next is to [**Create security group**](https://ap-northeast-1.console.aws.amazon.com/ec2/home?region=ap-northeast-1#SecurityGroups:)

:::danger
- For Inbound rules, create rules that allow specific traffic to reach your instance.
- Use the following rules for a web server that accepts HTTP and HTTPS traffic.
- Adding following rules for **Inbound rules**
| Type | Source |
| ----- | -------- |
| HTTP | Anywhere |
| HTTPS | Anywhere |
| SSH | My IP |
- For security, do not choose Anywhere for Source for SSH.
- For Outbound rules, keep the default rule, which allows all outbound traffic.
:::
4. Then, choose **Create security group**
# Create instance
:::info
AppWorks environment setup
- Instance type should be t2.micro.
- Machine image should be Amazon Linux 2 AMI.
- Attach 8GB General Purpose SSD storage at least.
:::
1. Click 👉 [here](https://console.aws.amazon.com/ec2/) 👈 to open Amazon EC2 console
2. Choose **Launch instance** and choose what you need
- From **Amazon Machine Image (AMI)**, select an HVM version of Amazon Linux 2
- From **Instance type**, choose the t2.micro instance type

3. Under **Key pair (login)**, choose the key pair that you created when getting set up.
4. Next to **Network settings**, choose Select existing security group.
5. Final is **Configure storage**, choose `1x 8 GiB gp2 Root volume`
6. Review your instance configuration in the Summary panel (right hand side)
7. Then, choose **Launch instance**
# Connect to instance
1. Click 👉 [here](https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#Instances:) 👈 to check the instance
2. Choose the instance and click **Connect**

3. Choose **SSH Client** and follow the steps

# Install Node.js on instance
1. Connect to your Linux instance as `ec2-user` using SSH
2. Install node version manager (nvm)
```bash=
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
```
3. Activate nvm
```bash=
. ~/.nvm/nvm.sh
```
4. Use nvm to install the latest version of Node.js
```bash=
nvm install --lts
```
5. Check Node.js is installed or not
```bash=
node -e "console.log('Running Node.js ' + process.version)"
```
# Install MySQL Server
1. Connect to your Linux instance as `ec2-user` using SSH
2. Need to update privileges to root user before installing the MySql
```bash=
sudo su
```
:::warning
若出現下列錯誤:`you need to be root to perform this command`
- Sol. 輸入 `su`,按下 enter,並輸入密碼
- 忘記密碼時,輸入 `sudo passwd`,重置密碼,再輸入`su`
:::
3. Updates all packages to the latest version
```bash=
sudo yum update -y
```
4. Install MySQL
```bash=
sudo yum install -y mariadb-server
```
:::warning
To Enable mysql to start when system reboots
Enter `sudo systemctl enable mariadb`
:::
5. We need to secure the installation
```bash=
sudo mysql_secure_installation
```
:::warning
Then, it will ask you a few questions,
- Q: Enter current password for root ( enter for none ) :
- A: Just press enter
- Q: Set Root password? [Y/n]
- A: Type ‘y’ and then press enter
- Q: Remove anonymous user? [Y/n]
- A: type ‘y’ and press enter
- Q: Disallow root login remotely? [Y/n]
- A: type ‘y’ and press enter
- Q: Remove test database and access to it? [Y/n]
- A: Type ‘y’ and press enter
- Q: Reload privilege tables now? [Y/n]
- A: Type ‘y’ and press enter
:::
6. Connect to MySQL, and you will see `Welcome to the MariaDB monitor`
```bash=
mysql -uroot -p
```