星期六, 2月 29, 2020

使用 AWS Cloudwatch 通知 IAM 新增使用者小記

使用 AWS Cloudwatch 通知 IAM 新增使用者小記

想法: 如果新增 IAM 使用者, 系統管理者會收到通知

架構圖如下


  • 建立 CloudTrail 的 Trail , 使用 CloudWatch 的 Event Rule 監控 IAM 的 API call: CreateUser, 利用 SNS 發e-mail 通知管理者
  • 因為要以 IAM 服務爲 Event Rule 服務類型, 以及 SNS 要進行通知, 所以 CloudWatch 與 SNS 的 Region 要設定在 N.Virginia ( us-east-1 )
  • 優點
    • IAM 新增使用者可以即時通知到管理者
  • 缺點
    • CloudTrail 可能會產生費用
    • SNS Notify email 未必很直覺

開始進行實作

登入 AWS console

建立 CloudTrail

在 AWS Console 點選 Services -- > 輸入或是搜尋 CloudTrail 找到 CloudTrail 服務並點選


Region 的部分因為 SNS 與後面的 CloudWatch 都會在 N.Virginia , 所以我 Region 也選 N.Virginia

點選左方選單的 Trails -- > 點選 Create trail


輸入 Trail 名稱
其他的部分保持預設
  • Apply trail to all regions: Yes
  • Read/Write events: All
  • Log AWS KMS events: Yes
  • Data Events: 保持沒有勾選
Storage location
  • 輸入要新增的 S3 bucket 名稱

點選 Create 建立 


建立 SNS Topic

在 AWS Console 點選 Services -- > 輸入或是搜尋 SNS 找到 Amazon SNS 服務並點選


Region 的部分選 N.Virginia


在 Amazon SNS 點選左側的 Topics

點選 Create Topic

輸入 Topic 名稱 -- > Create topic


建立 Topic 完成之後, 在該頁面點選 Create subscription


因為是在該 Topic 建立 Subscription, 所以 Topic ARN 就會自動被帶入
Protocol 的部分選擇 Email
Endpoint 輸入要通知的 email 
點選 Create subscription

接下來到信箱確認訂閱




建立 CloudWatch Event

點選 Services -- > 搜尋 cloudwatch 進入 Cloudwatch 服務


Region 的部分選 N.Virginia


點選左方選單的 Rules


點選 Create rule

Service Name: 選取 IAM
Event Type: 選取 AWS API Call via CloudTrail
點選 Specific operations(s)
輸入 CreateUser

右方的 Targets
點選 Add target*


下拉式選單: 選取 SNS topic 
Topic: 選取剛剛建立的 topic


點選 Configure details

輸入 Rule 名稱
點選 Create rule

這樣就完成設定

接下來嘗試在 IAM 建立使用者, 就會收到 SNS 寄來的通知


這樣算是多前進了一步, 但是考量到收費問題, 後續也有可能使用 aws cli 或是 python boto3 的方式來進行

向 AWS 前進一小步
~ enjoy it


Reference:

星期五, 2月 28, 2020

nginx with openSUSE Leap 15.1 in Azure 小記 Part 2

nginx with openSUSE Leap 15.1 in Azure 小記 Part 2

OS: openSUSE Leap 15.1 in Azure
Nginx: 1.14.2

上次練習 nginx 靜態網頁以及 proxy, 今天來實驗 HTTP Load Balancing

架構想法如下


實驗環境
OS: openSUSE Leap 15.1 in Azure x 3 VM
  • 前面有 1 台 openSUSE Leap 15.1 執行 nginx 
  • 後面有 2 台 openSUSE Leap 15.1 使用 container 執行 Web 服務

參考上次的文章

首先建立 2 台 openSUSE Leap 15.1 in Azure, 執行以下動作
  • 啟動 docker 服務
    • # systemctl  start docker
  • 執行 container 然後 port 開在 80
    • # docker  run -d -p  80:80 russmckendrick/cluster
  • Azure 該 VM 的網路設定內, 設定 port 80 可以連線
  • 設定該 VM DNS 名稱
  • 確認都可以進行存取





建立 1 台 openSUSE Leap 15.1 in Azure, 執行以下動作
  • 使用 zypper 指令 安裝 nginx
    • # zypper  install  nginx
  • 啟動 nginx 服務
    • # systemctl start nginx
    • # systemctl  enable  nginx
  • 設定 DNS 名稱

上面的動作前一篇文章就已經有了, 所以不贅述

接下來進行 HTTP Load Balancing 實驗

修改 nginx 設定檔

# vi   /etc/nginx/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include conf.d/*.conf;
    upstream backend {
            server test2020022801.eastus.cloudapp.azure.com;
            server test2020022802.eastus.cloudapp.azure.com;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /srv/www/htdocs/;
            index  index.html index.htm;
            proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /srv/www/htdocs/;
        }
    }
    
    include vhosts.d/*.conf;
}

  • 新增 upstream backend 區段, 指向剛剛建立的 2 台 Web 服務
  • 新增 proxy_pass 指向 http://backend

善用 #nginx -t 檢查語法
  • 一開始是寫獨立的 http 區段, 結果就被警告 http 重複設定

將服務 reload 

# systemctl  reload  nginx

進行測試
在瀏覽器開啟 http://testnginx.eastus.cloudapp.azure.com


  • 確認會導向後端不同的 Web 服務

Load Balancing 的方式有 6 種
  • Nginx open source 支援其中4種, nginx plus 支援 6 種
    • Round Robin ( 預設 )
    • Least Connections
    • IP Hash
    • Generic Hash
    • Least Time ( NGINX Plus only )
    • Random

接下來多實驗一個設定 Server Weight

剛剛有提到, 預設使用 Round Robin 方式將連線平均的分配到後端的服務
那如果 VM 他的規格大小不一樣, 或是想要測試金絲雀佈署這樣的改版測試呢?

這個時候考慮使用 Server Weight 權重方式來因應

修改 nginx 設定檔

# vi   /etc/nginx/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include conf.d/*.conf;
    upstream backend {
           server test2020022801.eastus.cloudapp.azure.com weight=3;
           server test2020022802.eastus.cloudapp.azure.com;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /srv/www/htdocs/;
            index  index.html index.htm;
            proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /srv/www/htdocs/;
        }
    }
    
    include vhosts.d/*.conf;
}

  • 針對剛剛後端服務其中一台設定 weight=3
    • 沒有設定的話, 權重設定預設是 1, 目前有 2 台機器服務, 所以總共是 3 + 1 = 4, 也就是說 test2020022801 被存取的次數就會佔 3/4 ( 75% ), 另外一台是 1/4 ( 25% )

將服務 reload 

# systemctl  reload  nginx

進行測試
在瀏覽器開啟 http://testnginx.eastus.cloudapp.azure.com

  • 觀察 2 台機器服務的顯示比例

這樣算是朝向 nginx 的一小步  :)

~ enjoy it


Reference