AWS CodeDeploy:修订间差异

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
 
(未显示同一用户的58个中间版本)
第3行: 第3行:
== 簡介 ==
== 簡介 ==


== 設定 ==
  傳統在CI/CD上面(像是[[Travis CI]]或是[[GitHub Actions]])透過[[SSH]]或是類似的方式,將新版的程式碼推到伺服器上的作法會是綜合性的:
  軟體本身 有兩段設定,一 段是Deploy 發起端 透過[[awscli]] 呼叫 外一 是在<code>appspec.yml</code>定 在伺服器 的行為。
 
* 開機時取得最新版的程式碼。
* 在CI/CD服務上先抓取目前的機器列表,然後連進去更新。
 
但這個作法會產生幾個問題,會需要更複雜的方式解決:
 
* 有兩 種不同的觸發路徑,一種是開機時去抓,另外一種是外部連進來機器裡面抓,要注意開機時同時發生的衝突問題。
** 這點不算難解,<code>flock</code>可以拿來用。
* 需要對CI/CD服務的機器網 開啟SSH,代表其他人有機會透過同樣的CI/CD服務接觸到你伺服器的SSH。
** 這個部分可以在執行CI/CD時抓取本身的IP address,動態 設定[[Firewall]](像是[[AWS]]的Security Group)以降低風險(但不是完全阻隔) 等到任務結束後再從Firewall上移除。但用這個方式又會衍生出其他問題:
*** 大多數的CI/CD服務是透過NAT的方式連外,但不能保證每次連外的IP是一樣的(雖然目前的應該都會 樣,但這沒有保證,屬於side effect)。
*** 另外必須考慮到多隻程式操作Firewall設定時可能會有衝突 問題,這點如果Firewall沒有支援CAS類的atomic操作,會 透過 外部的distributed lock避免。
 
AWS CodeDeploy是一套伺服器軟體佈署的服務,跟傳統CI/CD佈署不同的點在於:
 
* 都是由伺服器端主動取得檔案並且佈署。
* 不需要額外開SSH讓CI/CD服務連入操作。
 
== AWS端設定 ==
除了CodeDeploy設定外,還會需要建立[[S3]] bucket:
 
<syntaxhighlight lang="bash">
aws --profile default --region us-east-1 deploy create-application --application-name my-test
aws --profile default --region us-east-1 s3 mb "s3://gslin-codedeploy-us-east-1-my-test/"
</syntaxhighlight>
 
== 伺服器端安裝 ==
在伺服器端需要安裝Agent 這隻Agent會負責取得檔案並且執行設定的步驟<ref>{{Cite web |url=https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html |title=Install or Reinstall the AWS CodeDeploy Agent - AWS CodeDeploy}}</ref>。
 
以[[Ubuntu]]的環境為例子來說<ref>{{Cite web |url=https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html |title=Install or reinstall the AWS CodeDeploy agent for Ubuntu Server - AWS CodeDeploy}}</ref>,會需要先安裝Ruby 2.0(在14.04下)或Ruby 2.3(在16.04下)後,取得安裝設定檔執行:
 
<syntaxhighlight lang="bash">
sudo apt -y install ruby
cd /tmp
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod 755 install
sudo ./install auto
</syntaxhighlight>
 
在非[[EC2]]的機器上可能會跑比較久(需要等<code>169.254.169.254</code>的timeout,在EC2的環境裡,這個IP會有HTTP服務提供資訊給Instance使用)。
 
然後看Agent是否有啟動:
 
<syntaxhighlight lang="bash">
sudo service codedeploy-agent status
</syntaxhighlight>
 
如果沒有的話可以用<code>start</code>啟動:
 
<syntaxhighlight lang="bash">
sudo service codedeploy-agent start
</syntaxhighlight>
 
=== 外部機器的額 步驟 ===
當機器不在[[EC2]]上時,有幾種方法可以註冊到CodeDeploy的系統上,會被稱為On-Premises Instance。這邊我們介紹的方法是一台機器給 個IAM user的方式。
 
首先先在一般的機器上產生出對應的權限與設定檔(不需要在需要註冊的機器上),因為要建立IAM權限,通常會 由管理員建立(有<code>AdministratorAccess</code>的人):
 
<syntaxhighlight lang="bash">
aws deploy register --instance-name api-example-1 --region us-east-1
</syntaxhighlight>
 
然後把生出的<code>.yml</code>檔案傳到要註冊的機器上:
 
<syntaxhighlight lang="bash">
scp codedeploy.onpremises.yml api-example-1:/tmp/
</syntaxhighlight>
 
然後 要註冊的機器上執行<code>aws deploy install</code>:
 
<syntaxhighlight lang="bash">
cd /tmp
sudo aws deploy install --config-file codedeploy.onpremises.yml --region us-east-1
</syntaxhighlight>
 
上面的指令目前會因為他想裝<code>ruby2.0</code> 但系統沒有而顯示錯誤訊息,但我們已經裝好CodeDeploy的檔案了,這個指令的目的只是要他設 檔塞進系統。
 
如果是在某些有提供<code>http://169.254.169.254/</code>服務的VPS上執行(像是Vultr),會有<code>Amazon EC2 instances are not supported.</code>這類的錯誤訊息,這時候就需要用iptables暫時性擋掉對169.254.169.254的Port 80連線了。理論上用這個指令在重開後就會失效,對其他應用程式比較不會有副作用:
 
<syntaxhighlight lang="bash">
sudo iptables -I OUTPUT -d 169.254.169.254 -p tcp --dport 80 -j DROP
</syntaxhighlight>
 
都跑起來後(建議直接重開機測試)要記得加上Tag讓後續設定可以抓到機器:
 
<syntaxhighlight lang="bash">
aws deploy add-tags-to-on-premises-instances --instance-name api-example-1 --tags Key=Name,Value=api-example-1
</syntaxhighlight>
 
== 發佈 ==
通常會有兩個指令:
* 將現在的目錄打包起來傳到[[S3]]上。可能會使用<code>--ignore-hidden-files</code>避免<code>.git</code>或是<code>.svn</code>被包進去,但這個方式會使得<code>.htaccess</code>不會被包進去,對於使用[[Apache]]的使用者來說要注意。
* 要求CodeDeploy送指令到各機器上抓檔案。
 
<syntaxhighlight lang="bash">
export NOW=$(date -u +%Y%m%d-%H%M%S)
export S3_KEY=${APPLICATION_NAME}/${GIT_BRANCH}-${NOW}-${GIT_HASH}
aws deploy push \
  --application-name "${APPLICATION_NAME}" \
  --profile "${AWS_PROFILE}" \
  --region "${AWS_REGION}" \
  --s3-location "s3://${S3_BUCKET}/${S3_KEY}"
aws deploy create-deployment \
  --application-name "${APPLICATION_NAME}" \
  --deployment-group-name "${GIT_BRANCH}" \
  --profile "${AWS_PROFILE}" \
  --region "${AWS_REGION}" \
  --s3-location bucket="${S3_BUCKET},key=${S3_KEY},bundleType=zip"
</syntaxhighlight>
 
這邊可以看到故意放一些資訊到檔案名稱上,讓後續維護起來(找問題時)比較輕鬆。
 
=== 伺服器端 ===
在伺服器 端要進行 的行為 是被定義在<code>appspec.xml</code>內。最簡單的設定就是指定要將這包檔案解到哪邊:
 
<syntaxhighlight lang="yaml">
version: 0.0
os: linux
files:
  - source: /
   destination: /srv/www.example.com
</syntaxhighlight>
 
== 其他 ==
官方有提供把AWS CodeDeploy的Alarm轉到[[Slack]]上的Blueprint,而這個[[AWS Lambda]]程式可以把AWS CodeDeploy的Trigger(而非Alarm)轉到Slack上 有兩個環境變數要設定:
 
* <code>kmsEncryptedHookUrl</code>
* <code>slackChannel</code>
 
<syntaxhighlight lang="python">
import boto3
import json
import logging
import os
 
from base64 import b64decode
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
 
 
# The base-64 encoded, encrypted key (CiphertextBlob) stored in the kmsEncryptedHookUrl environment variable
ENCRYPTED_HOOK_URL = os.environ['kmsEncryptedHookUrl']
# The Slack channel to send a message to stored in the slackChannel environment variable
SLACK_CHANNEL = os.environ['slackChannel']
 
HOOK_URL = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED_HOOK_URL))['Plaintext'].decode('utf-8')
 
logger = logging.getLogger()
logger.setLevel(logging.INFO)
 
def lambda_handler(event, context):
   logger.info("Event: " + str(event))
   message = json.loads(event['Records'][0]['Sns']['Message'])
   logger.info("Message: " + str(message))
 
   region = message['region']
   account_id = message['accountId']
   event_trigger_name = message['eventTriggerName']
   application_name = message['applicationName']
   deployment_id = message['deploymentId']
   deployment_group_name = message['deploymentGroupName']
   create_time = message['createTime']
   complete_time = message['completeTime']
   status = message['status']
 
   if status == 'FAILED':
     slackColor = 'danger'
   else:
     slackColor = 'good'
 
   slack_message = {
     'channel': SLACK_CHANNEL,
     'attachments': [
       {
         'author': application_name,
         'fallback': ', '.join([status, deployment_group_name, deployment_id, create_time]) + '.',
         'color': slackColor,
         'title': 'Execute AWS CodeDeploy',
         'fields': [
           {
             'title': 'status',
             'value': status
           },
           {
             'title': 'deploymentGroupName',
             'value': deployment_group_name
           },
           {
             'title': 'deploymentId',
             'value': deployment_id
           },
           {
             'title': 'createTime',
             'value': create_time
           }
         ]
       }
     ]
   }
 
   req = Request(HOOK_URL, json.dumps(slack_message).encode('utf-8'))
   try:
     response = urlopen(req)
     response.read()
     logger.info("Message posted to %s", slack_message['channel'])
   except HTTPError as e:
     logger.error("Request failed: %d %s", e.code, e.reason)
   except URLError as e:
     logger.error("Server connection failed: %s", e.reason)
</syntaxhighlight>
 
== 參考資料 ==
 
{{Reflist|2}}
 
== 相關連結 ==
 
* [[AWS CodePipeline]]


== 外部連結 ==
== 外部連結 ==

2024年6月5日 (三) 02:00的最新版本

AWS CodeDeployAWS提供的服务之一,用于发布服务器端的软件。

简介

传统在CI/CD上面(像是Travis CI或是GitHub Actions)透过SSH或是类似的方式,将新版的程式码推到服务器上的作法会是综合性的:

  • 开机时取得最新版的程式码。
  • 在CI/CD服务上先抓取目前的机器列表,然后连进去更新。

但这个作法会产生几个问题,会需要更复杂的方式解决:

  • 有两种不同的触发路径,一种是开机时去抓,另外一种是外部连进来机器里面抓,要注意开机时同时发生的冲突问题。
    • 这点不算难解,flock可以拿来用。
  • 需要对CI/CD服务的机器网段开启SSH,代表其他人有机会透过同样的CI/CD服务接触到你服务器的SSH。
    • 这个部分可以在执行CI/CD时抓取本身的IP address,动态设定Firewall(像是AWS的Security Group)以降低风险(但不是完全阻隔),等到任务结束后再从Firewall上移除。但用这个方式又会衍生出其他问题:
      • 大多数的CI/CD服务是透过NAT的方式连外,但不能保证每次连外的IP是一样的(虽然目前的应该都会一样,但这没有保证,属于side effect)。
      • 另外必须考虑到多只程式操作Firewall设定时可能会有冲突的问题,这点如果Firewall没有支援CAS类的atomic操作,会透过外部的distributed lock避免。

AWS CodeDeploy是一套服务器软件布署的服务,跟传统CI/CD布署不同的点在于:

  • 都是由服务器端主动取得档案并且布署。
  • 不需要额外开SSH让CI/CD服务连入操作。

AWS端设定

除了CodeDeploy设定外,还会需要建立S3 bucket:

aws --profile default --region us-east-1 deploy create-application --application-name my-test
aws --profile default --region us-east-1 s3 mb "s3://gslin-codedeploy-us-east-1-my-test/"

服务器端安装

在服务器端需要安装Agent,这只Agent会负责取得档案并且执行设定的步骤[1]

Ubuntu的环境为例子来说[2],会需要先安装Ruby 2.0(在14.04下)或Ruby 2.3(在16.04下)后,取得安装设定档执行:

sudo apt -y install ruby
cd /tmp
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod 755 install
sudo ./install auto

在非EC2的机器上可能会跑比较久(需要等169.254.169.254的timeout,在EC2的环境里,这个IP会有HTTP服务提供资讯给Instance使用)。

然后看Agent是否有启动:

sudo service codedeploy-agent status

如果没有的话可以用start启动:

sudo service codedeploy-agent start

外部机器的额外步骤

当机器不在EC2上时,有几种方法可以注册到CodeDeploy的系统上,会被称为On-Premises Instance。这边我们介绍的方法是一台机器给一个IAM user的方式。

首先先在一般的机器上产生出对应的权限与设定档(不需要在需要注册的机器上),因为要建立IAM权限,通常会是由管理员建立(有AdministratorAccess的人):

aws deploy register --instance-name api-example-1 --region us-east-1

然后把生出的.yml档案传到要注册的机器上:

scp codedeploy.onpremises.yml api-example-1:/tmp/

然后在要注册的机器上执行aws deploy install

cd /tmp
sudo aws deploy install --config-file codedeploy.onpremises.yml --region us-east-1

上面的指令目前会因为他想装ruby2.0但系统没有而显示错误讯息,但我们已经装好CodeDeploy的档案了,这个指令的目的只是要他设定档塞进系统。

如果是在某些有提供http://169.254.169.254/服务的VPS上执行(像是Vultr),会有Amazon EC2 instances are not supported.这类的错误讯息,这时候就需要用iptables暂时性挡掉对169.254.169.254的Port 80连线了。理论上用这个指令在重开后就会失效,对其他应用程序比较不会有副作用:

sudo iptables -I OUTPUT -d 169.254.169.254 -p tcp --dport 80 -j DROP

都跑起来后(建议直接重开机测试)要记得加上Tag让后续设定可以抓到机器:

aws deploy add-tags-to-on-premises-instances --instance-name api-example-1 --tags Key=Name,Value=api-example-1

发布

通常会有两个指令:

  • 将现在的目录打包起来传到S3上。可能会使用--ignore-hidden-files避免.git或是.svn被包进去,但这个方式会使得.htaccess不会被包进去,对于使用Apache的使用者来说要注意。
  • 要求CodeDeploy送指令到各机器上抓档案。
export NOW=$(date -u +%Y%m%d-%H%M%S)
export S3_KEY=${APPLICATION_NAME}/${GIT_BRANCH}-${NOW}-${GIT_HASH}
aws deploy push \
  --application-name "${APPLICATION_NAME}" \
  --profile "${AWS_PROFILE}" \
  --region "${AWS_REGION}" \
  --s3-location "s3://${S3_BUCKET}/${S3_KEY}"
aws deploy create-deployment \
  --application-name "${APPLICATION_NAME}" \
  --deployment-group-name "${GIT_BRANCH}" \
  --profile "${AWS_PROFILE}" \
  --region "${AWS_REGION}" \
  --s3-location bucket="${S3_BUCKET},key=${S3_KEY},bundleType=zip"

这边可以看到故意放一些资讯到档案名称上,让后续维护起来(找问题时)比较轻松。

服务器端

在服务器端要进行的行为是被定义在appspec.xml内。最简单的设定就是指定要将这包档案解到哪边:

version: 0.0
os: linux
files:
  - source: /
    destination: /srv/www.example.com

其他

官方有提供把AWS CodeDeploy的Alarm转到Slack上的Blueprint,而这个AWS Lambda程式可以把AWS CodeDeploy的Trigger(而非Alarm)转到Slack上。有两个环境变数要设定:

  • kmsEncryptedHookUrl
  • slackChannel
import boto3
import json
import logging
import os

from base64 import b64decode
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError


# The base-64 encoded, encrypted key (CiphertextBlob) stored in the kmsEncryptedHookUrl environment variable
ENCRYPTED_HOOK_URL = os.environ['kmsEncryptedHookUrl']
# The Slack channel to send a message to stored in the slackChannel environment variable
SLACK_CHANNEL = os.environ['slackChannel']

HOOK_URL = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED_HOOK_URL))['Plaintext'].decode('utf-8')

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info("Event: " + str(event))
    message = json.loads(event['Records'][0]['Sns']['Message'])
    logger.info("Message: " + str(message))

    region = message['region']
    account_id = message['accountId']
    event_trigger_name = message['eventTriggerName']
    application_name = message['applicationName']
    deployment_id = message['deploymentId']
    deployment_group_name = message['deploymentGroupName']
    create_time = message['createTime']
    complete_time = message['completeTime']
    status = message['status']

    if status == 'FAILED':
        slackColor = 'danger'
    else:
        slackColor = 'good'

    slack_message = {
        'channel': SLACK_CHANNEL,
        'attachments': [
            {
                'author': application_name,
                'fallback': ', '.join([status, deployment_group_name, deployment_id, create_time]) + '.',
                'color': slackColor,
                'title': 'Execute AWS CodeDeploy',
                'fields': [
                    {
                        'title': 'status',
                        'value': status
                    },
                    {
                        'title': 'deploymentGroupName',
                        'value': deployment_group_name
                    },
                    {
                        'title': 'deploymentId',
                        'value': deployment_id
                    },
                    {
                        'title': 'createTime',
                        'value': create_time
                    }
                ]
            }
        ]
    }

    req = Request(HOOK_URL, json.dumps(slack_message).encode('utf-8'))
    try:
        response = urlopen(req)
        response.read()
        logger.info("Message posted to %s", slack_message['channel'])
    except HTTPError as e:
        logger.error("Request failed: %d %s", e.code, e.reason)
    except URLError as e:
        logger.error("Server connection failed: %s", e.reason)

参考资料

相关连结

外部链接