

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在明確代理伺服器中執行 CodeBuild
<a name="run-codebuild-in-explicit-proxy-server"></a>

 若要 AWS CodeBuild 在明確代理伺服器中執行 ，您必須設定代理伺服器以允許或拒絕進出外部網站的流量，然後設定 `HTTP_PROXY`和 `HTTPS_PROXY`環境變數。

**Topics**
+ [

## 將 Squid 設定為明確代理伺服器
](#use-proxy-server-explicit-squid-configure)
+ [

## 建立 CodeBuild 專案
](#use-proxy-server-explicit-create-acb-project)
+ [

## 明確代理伺服器 `squid.conf` 檔案範例
](#use-proxy-server-explicit-sample-squid-conf)

## 將 Squid 設定為明確代理伺服器
<a name="use-proxy-server-explicit-squid-configure"></a>

 若要將 Squid 代理伺服器設定為明確，您必須將其 `/etc/squid/squid.conf` 檔案修改如下：
+  移除以下預設的存取控制清單 (ACL) 規則。

  ```
  acl localnet src 10.0.0.0/8     
  acl localnet src 172.16.0.0/12  
  acl localnet src 192.168.0.0/16 
  acl localnet src fc00::/7       
  acl localnet src fe80::/10
  ```

   新增以下規則來取代您移除的預設 ACL 規則。第一行允許來自 VPC 的請求。接下來兩行授予許可給您的代理伺服器存取 AWS CodeBuild可能使用的目的地 URL。編輯最後一行中的規則表達式，以在 AWS 區域中指定 S3 儲存貯體或 CodeCommit 儲存庫。例如：
  + 如果您的來源是 Amazon S3，請使用 命令**acl download\$1src dstdom\$1regex .\$1s3\$1.us-west-1\$1.amazonaws\$1.com**授予 `us-west-1`區域中 S3 儲存貯體的存取權。
  +  如果您的來源是 AWS CodeCommit，請使用 `git-codecommit.<your-region>.amazonaws.com`將 AWS 區域新增至允許清單。

  ```
  acl localnet src 10.1.0.0/16 #Only allow requests from within the VPC
  acl allowed_sites dstdomain .github.com #Allows to download source from GitHub
  acl allowed_sites dstdomain .bitbucket.com #Allows to download source from Bitbucket
  acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from Amazon S3 or CodeCommit
  ```
+  將 `http_access allow localnet` 換成下列項目：

  ```
  http_access allow localnet allowed_sites
  http_access allow localnet download_src
  ```
+ 如果您想要建置上傳日誌和成品，請執行以下其中一項：

  1. 在 `http_access deny all` 陳述式之前，插入以下陳述式。它們允許 CodeBuild 存取 CloudWatch 和 Amazon S3。需要存取 CloudWatch，以便 CodeBuild 可以建立 CloudWatch 日誌。需要存取 Amazon S3 才能上傳成品和 Amazon S3 快取。
     + 

       ```
       https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
       acl SSL_port port 443
       http_access allow SSL_port
       acl allowed_https_sites ssl::server_name .amazonaws.com
       acl step1 at_step SslBump1
       acl step2 at_step SslBump2
       acl step3 at_step SslBump3
       ssl_bump peek step1 all
       ssl_bump peek step2 allowed_https_sites
       ssl_bump splice step3 allowed_https_sites
       ssl_bump terminate step2 all
       ```
     + 儲存 之後`squid.conf`，請執行下列命令：

       ```
       sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130
       sudo service squid restart
       ```

  1.  將 `proxy` 新增至您的 buildspec 檔案。如需詳細資訊，請參閱[Buildspec 語法](build-spec-ref.md#build-spec-ref-syntax)。

     ```
     version: 0.2
     proxy:
       upload-artifacts: yes
       logs: yes
     phases:
       build:
         commands:
           - command
     ```

**注意**  
如果您收到 RequestError 逾時錯誤，請參閱[在代理伺服器中執行 CodeBuild 時發生 RequestError 逾時錯誤](troubleshooting.md#code-request-timeout-error)。

如需詳細資訊，請參閱本主題後面部分的 [明確代理伺服器 `squid.conf` 檔案範例](#use-proxy-server-explicit-sample-squid-conf)。

## 建立 CodeBuild 專案
<a name="use-proxy-server-explicit-create-acb-project"></a>

 若要 AWS CodeBuild 使用明確代理伺服器執行 ，請使用您為代理伺服器建立的 EC2 執行個體私有 IP 地址`HTTP_PROXY`和專案層級的連接埠 3128 來設定其 和 `HTTPS_PROXY`環境變數。私有 IP 地址看起來像是 `http://your-ec2-private-ip-address:3128`。如需詳細資訊，請參閱[在 中建立建置專案 AWS CodeBuild](create-project.md)及[在 中變更建置專案設定 AWS CodeBuild](change-project.md)。

 使用以下命令來檢視 Squid 代理存取日誌：

```
sudo tail -f /var/log/squid/access.log
```

## 明確代理伺服器 `squid.conf` 檔案範例
<a name="use-proxy-server-explicit-sample-squid-conf"></a>

 以下是為明確代理伺服器設定的 `squid.conf` 檔案的範例。

```
  acl localnet src 10.0.0.0/16 #Only allow requests from within the VPC
  # add all URLS to be whitelisted for download source and commands to be run in build environment
  acl allowed_sites dstdomain .github.com    #Allows to download source from github
  acl allowed_sites dstdomain .bitbucket.com #Allows to download source from bitbucket
  acl allowed_sites dstdomain ppa.launchpad.net #Allows to run apt-get in build environment
  acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from S3 or CodeCommit
  acl SSL_ports port 443
  acl Safe_ports port 80		# http
  acl Safe_ports port 21		# ftp
  acl Safe_ports port 443		# https
  acl Safe_ports port 70		# gopher
  acl Safe_ports port 210		# wais
  acl Safe_ports port 1025-65535	# unregistered ports
  acl Safe_ports port 280		# http-mgmt
  acl Safe_ports port 488		# gss-http
  acl Safe_ports port 591		# filemaker
  acl Safe_ports port 777		# multiling http
  acl CONNECT method CONNECT
  #
  # Recommended minimum Access Permission configuration:
  #
  # Deny requests to certain unsafe ports
  http_access deny !Safe_ports
  # Deny CONNECT to other than secure SSL ports
  http_access deny CONNECT !SSL_ports
  # Only allow cachemgr access from localhost
  http_access allow localhost manager
  http_access deny manager
  # We strongly recommend the following be uncommented to protect innocent
  # web applications running on the proxy server who think the only
  # one who can access services on "localhost" is a local user
  #http_access deny to_localhost
  #
  # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
  #
  # Example rule allowing access from your local networks.
  # Adapt localnet in the ACL section to list your (internal) IP networks
  # from where browsing should be allowed
  http_access allow localnet allowed_sites
  http_access allow localnet download_src
  http_access allow localhost
  # Add this for CodeBuild to access CWL end point, caching and upload artifacts S3 bucket end point
  https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
  acl SSL_port port 443
  http_access allow SSL_port
  acl allowed_https_sites ssl::server_name .amazonaws.com
  acl step1 at_step SslBump1
  acl step2 at_step SslBump2
  acl step3 at_step SslBump3
  ssl_bump peek step1 all
  ssl_bump peek step2 allowed_https_sites
  ssl_bump splice step3 allowed_https_sites
  ssl_bump terminate step2 all
  # And finally deny all other access to this proxy
  http_access deny all
  # Squid normally listens to port 3128
  http_port 3128
  # Uncomment and adjust the following to add a disk cache directory.
  #cache_dir ufs /var/spool/squid 100 16 256
  # Leave coredumps in the first cache dir
  coredump_dir /var/spool/squid
  #
  # Add any of your own refresh_pattern entries above these.
  #
  refresh_pattern ^ftp:		1440	20%	10080
  refresh_pattern ^gopher:	1440	0%	1440
  refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
  refresh_pattern .		0	20%	4320
```