# 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) ![](https://i.imgur.com/z5ydtZC.png) * 使用 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 ![](https://i.imgur.com/Xu84pRV.png) * Edit default role permission, add "AmazonDynamoDBFullAccess" ![](https://i.imgur.com/ReO0Cff.png) ![](https://i.imgur.com/WwHHLhx.png) * 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 ![](https://i.imgur.com/K7vb0J9.png) * Modify existing policy for the second function to write logs in CloudWatch ![](https://i.imgur.com/bCGkRdR.png) * 在起始的 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://i.imgur.com/uWXrrBJ.png) * 從 [這裡](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 ![](https://i.imgur.com/02zfOxe.png) * 設定 API POST >> Generate-ShortURL Proxy Get >> Redirect-to-OriginalURL * Deploy API to name **api** stage ![](https://i.imgur.com/g476FvS.png) * 修改 S3 裡面的 Web bucket 裡面的 index, 把 API endpoint 改成剛剛 deploy 的網址 * 因為 API Getway 與 S3 的網址不同,會有 CORS 的問題,因此要 Enable CORS ![](https://i.imgur.com/jxvURyY.png) ## CloudFront * Create Distribution for Web in S3 * Add API Getway endpoint in the same distribution ![](https://i.imgur.com/XbkN9MB.png) ![](https://i.imgur.com/9pVNUZ7.png) Warning: would not fill origin path in this case, the "/api" uri will be handled by behaviors * Create Cache Behavior ![](https://i.imgur.com/jlAWBv4.png) * 修改 S3 index.html 的 API endpoint 並重新丟上 S3