# AWS Training SAA
1. 檔案位置 https://github.com/QCloudMentor/2020FEB-AWS-SAA
2. 使用 CLI 上傳檔案
```
aws s3 cp . s3://aws-hp-training-20200910 --recursive
```
3. 修改 Permission
```
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::aws-hp-training-20200910/*"
]
}
```
4. 測試 http://aws-hp-training-20200910.s3-website-us-east-1.amazonaws.com
## 建立 DynamoDB
* Table name: TableURLShortener
* Partition key: OriginalURL, String
* Add sort key: ShortURL, String
* Uncheck "Use default settings"
* Uncheck Auto Scaling and change capacity units to 1 (Becase of free tier)

* 使用 Default 加密
* Create Table
## Lambda Function
#### Generate-ShortURL
* Create function
* Use "Author from scratch"
* Function name: Generate-ShortURL
* Runtime: Python 3.7
* Select "Create a new role with basic Lambda permissions"
* 起始的 lambda function 貼上[這個 Source code](https://github.com/QCloudMentor/2020FEB-AWS-SAA/blob/master/Projec1_URL_Shortener/Projec1_URLShortener_Lambda/Generate-ShortURL.py)
* Set environment variables
API_endpoint = https://xxx.cloudfront.net/api
DBTableName = TableURLShortener
* Create t1 test

* Edit default role permission, add "AmazonDynamoDBFullAccess"


* Add [hashids.py](https://github.com/QCloudMentor/2020FEB-AWS-SAA/blob/master/Projec1_URL_Shortener/Projec1_URLShortener_Lambda/hashids.py) in the default folder [檔案在這裡](https://github.com/QCloudMentor/2020FEB-AWS-SAA/blob/master/Projec1_URL_Shortener/Projec1_URLShortener_Lambda/hashids.py)
#### Redirect-to-OriginalURL
* Create the second funtion
* Function name: Redirect-to-OriginalURL
* Runtime: Python 3.7
* Select existing role

* Modify existing policy for the second function to write logs in CloudWatch

* 在起始的 lambda function 貼上[另一個 source code](https://github.com/QCloudMentor/2020FEB-AWS-SAA/blob/master/Projec1_URL_Shortener/Projec1_URLShortener_Lambda/Redirect-to-OriginalURL.py)
* Set environment variables
URLShortenerWebsite = https://xxx.cloudfront.net/
DBTableName = TableURLShortener
* 第二個 lambda function 不用寫測試
## API Gateway
* Import REST API (Public)

* 從 [這裡](https://github.com/QCloudMentor/2020FEB-AWS-SAA/blob/master/Projec1_URL_Shortener/Project1_APIGateway/TinyURLWebsite_API_Gateway_Settings.yaml) 下載 yaml 檔 import 進 API Getway 設定
* 可能因為版本關係,會產生 ANY,先刪除 ANY Method

* 設定 API POST >> Generate-ShortURL
Proxy Get >> Redirect-to-OriginalURL
* Deploy API to name **api** stage

* 修改 S3 裡面的 Web bucket 裡面的 index, 把 API endpoint 改成剛剛 deploy 的網址
* 因為 API Getway 與 S3 的網址不同,會有 CORS 的問題,因此要 Enable CORS

## CloudFront
* Create Distribution for Web in S3
* Add API Getway endpoint in the same distribution


Warning: would not fill origin path in this case, the "/api" uri will be handled by behaviors
* Create Cache Behavior

* 修改 S3 index.html 的 API endpoint 並重新丟上 S3