跳到主要內容

發表文章

目前顯示的是有「AWS」標籤的文章

AWS CloudWatch Synthetics

原本想用 DataDog 的 Sythetics API Test 做 e2e monitoring & alarm 但是遇到 multi-part form-data 沒辦法在 Request Body 使用 \r\n 的問題 curl -i -X  --trace-ascii - -F 'key=key1' -F 'key2=key2' https://whatever.totest.com/auth 你可以看到送出的 Content-Length 和 Request Body 是 Content-Length: YYY Content-Type: multipart/form-data; boundary=xxxx --xxxx Content-Disposition: form-data; name="key1" key1 --xxxx Content-Disposition: form-data; name="key2" key2 --xxxx-- 但是使用 DataDog 送出去的話只會用 \n 和 curl 送出的一比較,就發現 Content-Length 比較小,但是 DataDog 似乎沒辦法改.. 試了一下 AWS CloudWatch Synthetics Canary 發現他其實就是 deploy 一個 nodejs lambda layer 裡面執行 Puppeteer, Chromium 和 AWS SDK 但是又整合了 cloudwatch dashoboard, cloudwatch logs 和 cloudwatch alarm,所以不只可以跑 script 還可以透過 chromium 去抓網頁的 screenshot 彈性超大! 另外一個好處是可以編輯要送出去的 postData,一開始我看他預設也是送 \n 會造成 invalid request 但是只要改成 \r\n 就好囉!

Publish lambda layer to regions from s3 bucket?

boto3 有個  publish_layer_version  API,你可以使用 S3 bucket + key or zipfile 來發布 lambda layer. 使用 zipfile 的話就是從命令執行端上傳,所以假設你的 lambda packet 是 20MB 然後你要發佈到 16 個 AWS region 的話,就看你命令執行端的網路上傳速度了。 使用 S3 bucket 來放要發佈的 zipfile 檔似乎比較合理,因為這樣子檔案的複製就是在 AWS 的網路內發生。 But... 很機車的是,如果你的 zipfile 是放在 us-west-1 的 S3 bucket 那麼你就只能發佈到 us-west-1 .... 當你要發佈到其他 region 會出現 error botocore.errorfactory.InvalidParameterValueException: An error occurred (InvalidParameterValueException) when calling the PublishLayerVersion operation: Error occurred while GetObject. S3 Error Code: PermanentRedirect. S3 Error Message: The bucket is in this region: us-east-1. Please use this region to retry the request S3 bucket name 是 global unique 但其實 bucket 位置是有 region 的... 目前還沒有看到什麼比較好的方式來發佈 lambda layer 到全部的 region.... replicate 所有的 zipfile 到所有的 region, 然後每個都有自己的 bucket name 似乎蠻蠢的?....

Application Load Balancer lambda endpoint healthy check will be charged

AWS 還是有蠻多坑的... 如果 ALB 的 TargetGroup 使用 lambda endpoint 那麼為了避免 code start issue 可能會使用 provisioned concurrency 另外 lambda endpoint 在 update stack 的時候會出現 Load Balancer not able to stabilizied 的問題。AWS support 目前給的work around就是開啟 ALB healthy check 預設是每 35 秒會做一次 healthy check 然後如果 ALB 跨3個 AZ 的話,healthy check count 就 x3 .... 然後每次的 lambda execution 都是照 lambda function usage 來收費 另外ALB的 healthy check 不會有完整的 request header 如果你的 framework 不預期有這種不正常的 header 沒有去 handle 的話 可能會一直狂噴500... 如果你還沒有把預設的 lambda error retry 關掉的話...... 這樣子用 lambda endpoint 真的比較省嗎?....

Access private API gateway from another account

最近在做一個 PoC 驗證說怎麼在現有的架構下,把一些 public API 移到 VPC 裡面,只讓特定網路的人可以存取。 當然最直覺的想法就是,建一個新的 VPC ,加上 execute-api VPC endpoint 然後這個 VPC 再跟特定網路做 Peering,建一個 Private API Gateway 在新的 VPC 裡面。 這樣新的 VPC 可以透過 VPC endpoint 去存取 API Gateway,然後再加一個 ALB ,裡面的 target group 指向 VPC endpoint 的 IP和 443 port,ALB 設定好 Domain Listener,API Gateway 也加上相同的 Custom Domain Name,這樣子 ALB 就可以當成特定網路 access 的 entry point,一但 Peering 完成後,從特定網路來的 request 就可以經由 ALB -> API GW 跨 VPC 存取原本環境的 backend resource。 但我一直在想,Peering 是必要的嗎? 沒想到隔天就看到這個教學  How can I access an API Gateway private REST API in another account using an interface VPC endpoint?  裡面的做法突破了原本我直覺上的盲點! 就是即使是 Internal API Gateway 似乎只要設定好了 resource policy 就可以允許其他 account 的 VPC endpoint 跨帳號存取! 所以例子裡面的 private API Gateway 是建立在 B 帳號裡面,A 帳號的 VPC 只是啟用了 VPC endpoint,一但 B 帳號的 private API Gateway 在 resource policy 設定好允許 A 帳號的 VPC endpoint 存取,即使兩個帳號之間沒有 peering 也是可以互通的! 雖然說這樣子要帶 hostname header or api-gw-id 但是 原本架構上的 ALB 也是為了 VPC endpoint 可以知道要 forward request 給哪個 API Gateway ...

Lambda version with provisioned concurrency in CloudFormation update stack fail

如果在 CloudFormation 裡面直接使用  "AWS::Lambda::Version" 加上  ProvisionedConcurrencyConfig 接著 update stack 更新 ConcurrentExecutions 就會出現 update error 因為 version 不能夠 update 但是如果把 ProvisionedConcurrency 放在  "AWS::Lambda::Alias" 裡面,update stack 卻又可以了... lol... 不過一個 provisioned concurrent lambda 一個月要額外多 $5 USD 的費用還不包含 lambda execution 的費用,一個 1 vCPU + 512MB 的 fargate container 也不過大概是 $31 USD .... Update: 再仔細看了一下,update stack 失敗的原因有兩個一個是因為 Application Load Balancer Target Group 指向的 lambda 無法 stabilized 所以 update 失敗導致 rollback。其中一個原因是因為 ALB 沒有權限 invoke lambda function。LambdaPermission resource 還沒執行時,就先執行了 ALB TargetGroup 設定,所以在 TargetGroup resource 加了 DependsOn LambdaPermission 後,就少了 一個錯誤,但剩下的 ALB 無法 stabilized 的錯誤目前還是無解.... 只能把 stack 刪掉重新 deploy,接下來的 update 好像就都沒事。只是說在刪除 stack 的時候,會出現等待 lambda ENI 刪除的 waiting... 要等 8 ~ 20 min? 據說是後來 aggregate ENI 給 VPC 內的 lambda 後導致的 side effect.... CloudFormation 印象中也是越來越聰明,當 Resource A 有 !Ref Resource B 的時候,就會自動做 DependsOn 調整執行順序,但是像今天這個...

AWS Autos Scaling Group termination policy

今天遇到了一個有趣的問題,是有個 build job 說他的 build node 會被隨機 terminate 掉。 大致去看了一下 Auto Scaling Group 的設定,當 idle node > 1 的時候, ASG 就會開始 scale in ,當有 build queue 不為空的時候,就 step scaling out 研究一下他的做法是在 jenkins cronjob 每分鐘去 query jenkins api 回報 build queue 和目前狀態是 idle or offline 的 build node number 去 CloudWatch metrics 然後 CloudWatch metrics 再去觸發 ASG 的 scale in or out 這時候我想到的問題是,那當 scale in 被觸發時,是哪台 instance 要被 terminate? 當初的設計很聰明,他利用了 lifecycle hook,用意是說當 build node 被選上要被 terminate 時,會進入 Terminate Wait 的狀態,直到 ASG 收到 complete-lifecycle-action 的指令時,才會真的 terminate 掉。 但這個 lifecycle hook 是有預設 timeout 時間 3600 sec !!  也就是說一但 instance 進入 Terminate Wait 即使你沒有用 cron job 去 complete-lifecycle-action 過了一小時 waiting 時間它還是會被關掉.... 原本我以為 ASG 應該會挑 CPU 閒置的機器下手吧?後來再去翻一下文件發現, default termination policy 的策略依序是盡量讓機器分散在不同 AZ >  Allocation Policy > Oldest Launch Template or Config 最後是開機時間最接近整數小時的機器。 其他可以選的 termination policy 也都不是根據 instance usage 來判定。 原本因為不想花太多時間,只是暫時先把 timeout 時間設長,讓他有機會即使不幸被選上,還是...

Import VMware VM to EC2 and support T3/M5/C5 ENA and Nitro Hypervisor

If you need to update an CentOS AMI to support AWS new generation instance type such as T3/T3a/C5/M5, Enhanced Networking Adaptor (ENA) or EBS Optimized disk. You will need to include drivers into initramfs, otherwise instance will boot fail. The first thing is enable and install enhanced networking driver. You can refer document:  https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking-ena.html#enhanced-networking-ena-linux However, after rebuild initramfs via dracut and reboot, your instance might boot fail. Because of the new instance type supports ENA usually will also need to support EBS optimized volume.  To troubleshoot I will suggest turn on console logging for your AMI first, so that you can troubleshoot via Get Instance Screenshot. You need to update grub.cfg remove "rhgb quiet" and add "console=tty0" into /etc/default/grub and then install it via grub2-mkconfig -o /boot/grub2/grub.cfg The second thing is to make initramfs ...

Secure SSH access with AWS EC2 instance connect

要如何安全的存取 EC2 instance 呢? 管理和交換 EC2 ssh key pair 是一件很麻煩的事 除了透過 system manager access instance console 外, 另外一各有趣的方式是透過 IAM send ssh key + instance connect 原理是如果 EC2 instance server 端安裝 instance connect script, 則 sshd 會去 instance metadata 拉 one-time ssh key 來允許 user ssh login. client 端可以把自己的 ssh key 透過 aws cli push 上去,或者是安裝 pip install ec2instanceconnectcli , 這個 command wrapper 會動態的產生 ssh key 然後再 push 上去 ec2 instance. 因為 key 在 instance metadata 只會存在 60 秒,基本上都是依靠 IAM 來控管權限,唯一的缺點就是,不是所有的 EC2 instance 預設都有 安裝好 instance connect . 目前只有 Amazon Liunx 2 和 Ubuntu 16.04 later 預設先安裝好了...

AWS codecommit pricing by any different access id

The AWS codecommit pricing determine a valid user by any unique access identities. Q: What is the definition of an active user in AWS CodeCommit? An active user is any unique AWS identity (IAM user/role, federated user, or root account) that accesses AWS CodeCommit repositories during the month, either through Git requests or by using the AWS Management Console. A server accessing CodeCommit using a unique AWS identity counts as an active user. I created a repo from web console with my AWS root account. For my desktop I create an IAM user with Access Key ID A, and for my Macbook Pro, I add second Access Key ID for the same IAM user. Then I saw its counted as 3 user access, charged with $1 USD x 3 / per month for the repo... lol

AWS session manager and ssm-agent

Its quite a cool feature! https://aws.amazon.com/blogs/aws/new-session-manager/ No more exposed open port for remote access! No more shared ssh key! Just attach SSM role to your running instances and install latest ssm-agent! I think ssm-agent will getting more and more popular just like vmtools in VMware! What else can ssm-agent do? Thats wait and see...

AWS Machine Learning Workshop

Machine Learning Concepts Apply AWS ML to problems you have existing samples of actual answers For example, to predict if new email is spam or not, you need to collect examples of spam and non-spam. Binary classification (true / false) Is spam or not spam, churn, will customer accept campaign? Multiclass classification (one of more than two outcomes) Regression (numeric number) Building a Machine Learning Application Frame the core ML problems Collect, clean and prepare data Features from raw data Feed to learning algorithm to build models Use the model to generate predictions for new data Linear Models Leaning process computes one weight for each feature to form a model that can predict the target value For example, estimated target = 0.2 + 5 * age + 0.00003 * income Learning Algorithm Learn the weights of the model Loss function: penalty when estimate target provide by the model not equal exact result Optimization technique: minimize the loss (Stochastic G...

why create at least two subnets in different AZs is a must have practice?

今天遇到了一個有趣的問題,在 AWS VPC 裡要 create RDS DB instance 遇到了一個無解的問題。 原本的 VPC 規劃時只有兩個 subnet,而且無法再加進其他 subnet。 VPC CIDR: 192.168.1.0/24 Subnet 1 CIDR: 192.168.1.0/25 Subnet 2 CIDR: 192.168.1.128/25 更悲劇的是,這兩個 subnet 都是屬於同一個 Availability Zone (AZ)..... 雖然在 VPC 裡 create RDS instance 時可以選擇 "Multiple AZ deployment = No" 但接下來卻還是一定要 create DB subnet group. 而 DB subnet group 則是至少要有兩個 subnet 要在不同的 AZ, 我原本以為如果我選擇只要 single AZ deployment,那 create DB subnet group 應該就不是必要的,但看起來 AWS 是不允許的。去翻了一下 Working with an Amazon RDS DB Instance in a VPC 第一條真的寫了 " Your VPC must have at least one subnet in at least two of the Availability Zones in the region where you want to deploy your DB instance. A subnet is a segment of a VPC's IP address range that you can specify and that lets you group instances based on your security and operational needs. " 雖然我不知道怎麼只有一個 subnet 但是可以有兩個 AZ,但就我的理解應該就是指,你的 VPC 下要有兩個以上的 subnet,然後至少有兩個 subnet 是屬於不同 AZ。 這個故事告訴我們: 創建 VPC 時,可大,不可以小。雖然目前只有兩個 su...

AWS CloudWatch Estimated Charge Alarm

如果有在用 AWS, 建議可以在 CloudWatch 裡面依照 usage estimation 設定 Alarm. 目前可以根據 EC2 usage, s3 usage, data transfer usage 設定 Alarm 通知. 例如你平常每個月差不多只會有 1000GB 的 data transfer 費用約 USD 89, 那你可以設一個 Alarm 當 data transfer 費用超過USD 100的時候發一封 mail 通知你. 或者是你的 AWS account 每個月 budget 或者平均費用是 USD 100, 那你也可以設定一個 Alarm 在超過你預計的費用時發一封通知信. 沒有設定 Alarm 的話,你可能在收到帳單時才發現在 AWS 上花的錢超過你的預估 反之收到 Alarm 通知,可以讓你即早發現帳戶內的異常行為和非預期的費用 CloudWatch 的 Alarm 計費是 0.1 per alarm per month 我們最近上了一堂 USD 6000 元的課... 就是被 Data Transfer 的費用陰了... 當然一定還有其他的方法可以避免你在 AWS 上花冤枉錢 但是花幾分鐘設定 Alarm 我覺得 CP 值很大.... See Also: Monitor estimated charge with AWS CloudWatch

AWS PHP SDK for New Region eu-central-1

其實在 AWS 對外正式 announce 這個 region 之前, 我們已經有先接到了通知,並且要事先準備好 patch 來支援這個新的 region. 因為覺得很有趣,所以在看到 AWS 正式對外的 announce 後 發現裡面提到新的 region 只支援 "v4" 的 signature, 而我們用的 AWS PHP SDK 版本似乎過舊,所以認證都會失敗... 當晚馬上在測試環境更換2.7.1後,認證過了 可是卻出現 invalid endpoint 之類的錯誤訊息. 原來在 2.7.2 之前, 所有的 endpoint 都是內建在 SDK 的 resource file 裡面 所以即使 AWS 對外 announce 了新的 endpoint , 還是需要上 patch 幫 SDK 加新的 endpoint 隔天睡醒後,同事看到有更新的 2.7.2  而且發現 不需要手動 patch 加 endpoint 就可以 access 新的 eu-central-1 region 看了一下發現原來 2.7.2  以後 SDK 會自動去抓 AWS 最新的 Region & Edpoint List 所以以後有新的 region announce 的話, 就不用再去 patch AWS PHP SDK 啦~

AWS Certified Solutions Architect - Professional Level beta exam

收到白老鼠實驗感謝卡了.... beta exam 是 120 題 185 分鐘 .... 希望正式版的考試題目數量可以少一點...一些太長的題目可以拿掉... 不然每題不到兩分鐘的答題時間,看完題目就過一半了.... 考三小時後面幾乎呈現腦死狀態.... <quote> Thank you again for taking the Beta AWS Certified Solutions Architect - Professional Level beta exam. Unfortunately, you were unsuccessful in this attempt. You will receive a voucher within the next 5 business days, for a free retake. </quote> 讀書去....

Amazon EC2 Public IP Ranges

https://forums.aws.amazon.com/ann.jspa?annID=1701# <quote> Dear Amazon EC2 customer, We are pleased to announce that as part of our ongoing expansion, we have added new public IP ranges. The current Amazon EC2 public address ranges are:  US East (Northern Virginia):  72.44.32.0/19 (72.44.32.0 - 72.44.63.255)  67.202.0.0/18 (67.202.0.0 - 67.202.63.255)  75.101.128.0/17 (75.101.128.0 - 75.101.255.255)  174.129.0.0/16 (174.129.0.0 - 174.129.255.255)  204.236.192.0/18 (204.236.192.0 - 204.236.255.255)  184.73.0.0/16 (184.73.0.0 - 184.73.255.255)  184.72.128.0/17 (184.72.128.0 - 184.72.255.255) 184.72.64.0/18 (184.72.64.0 - 184.72.127.255)  50.16.0.0/15 (50.16.0.0 - 50.17.255.255) 50.19.0.0/16 (50.19.0.0 - 50.19.255.255) 107.20.0.0/14 (107.20.0.0 - 107.23.255.255) 23.20.0.0/14 (23.20.0.0 - 23.23.255.255) 54.242.0.0/15 (54.242.0.0 - 54.243.255.255) 54.234.0.0/15 (54.234.0.0 - 54.235.255.255) 54.236.0.0/15 (54.236.0.0 - ...

upload single ssh public key for all ec2 regions

Prerequisite: EC2-API-Tools ssh-keygen to create ssh key pairs on linux Parameters:   region names: such as ap-southeast-2,.... keypair-names: for example, my-ec2-keypairs for r in us-east-1 ues-east-2 us-west-1 us-west-2 ap-southeast-1 ap-southeast-2 eu-west-1; do ec2-import-keypair --region $r your-keypair-name --public-key-file ~/.ssh/id_rsa.pub ; done REF: http://aws.typepad.com/aws/2010/09/new-amazon-ec2-feature-bring-your-own-keypair.html http://www.pyrosoft.co.uk/blog/2010/10/27/import-your-own-rsa-ssh-key-into-amazon-ec2/