

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

# 在 中佈建 Raspberry Pi AWS IoT
<a name="iot-dc-install-provision"></a>

本節中的程序會從已安裝 和 AWS IoT 裝置用戶端的已儲存 microSD AWS CLI 映像開始，並建立佈建 Raspberry Pi AWS IoT 的資源和裝置憑證 AWS IoT。

## 在 Raspberry Pi 中安裝 microSD 卡
<a name="iot-dc-install-dc-restore"></a>

此程序會安裝已載入 Raspberry Pi 並設定必要軟體的 microSD 卡，並設定您的 ， AWS 帳戶 以便您可以繼續此學習路徑中的教學課程。

使用 [(選用) 儲存 microSD 卡映像](iot-dc-install-download.md#iot-dc-install-dc-save) 中的 microSD 卡，其中包含此學習路徑中的練習和教學課程所需的軟體。

**在 Raspberry Pi 安裝 microSD 卡**

1. 在 Raspberry Pi 斷開電源後，將 microSD 卡插入 Raspberry Pi。

1. 將電源連接至 Raspberry Pi。

1. 大約一分鐘後，在本機主機電腦上重新啟動終端機視窗工作階段，然後登入 Raspberry Pi。

1. 在本機主機電腦的終端機視窗中，使用 Raspberry Pi 的 **Access Key ID** (存取金鑰 ID) 和 **Secret Access Key** (私密存取金鑰) 憑證：

   1. 使用此命令執行 AWS configure 應用程式：

      ```
      aws configure
      ```

   1. 出現提示時，輸入您的 AWS 帳戶 登入資料和組態資訊：

      ```
      AWS Access Key ID [****************YXYX]: your Access Key ID
      AWS Secret Access Key [****************YXYX]: your Secret Access Key
      Default region name [us-west-2]: your AWS 區域 code
      Default output format [json]: json
      ```

還原 AWS 帳戶 登入資料後，您就可以繼續 [在 中佈建您的裝置 AWS IoT Core](#iot-dc-install-dc-provision)。

## 在 中佈建您的裝置 AWS IoT Core
<a name="iot-dc-install-dc-provision"></a>

本節中的程序會建立佈建 Raspberry Pi AWS IoT 的資源 AWS IoT。建立這些資源時，系統會要求您記錄各種資訊。裝置 AWS IoT 用戶端組態會在下一個程序中使用此資訊。

若要讓您的 Raspberry Pi 使用 AWS IoT，必須進行佈建。佈建是建立和設定支援 Raspberry Pi 作為 IoT 裝置所需的 AWS IoT 資源的程序。

在 Raspberry Pi 開啟電源和重新啟動之後，請在本機主機電腦上的終端機視窗中連接至 Raspberry Pi 並完成這些程序。

**Topics**
+ [建立並下載裝置憑證檔案](#iot-dc-install-dc-provision-certs)
+ [建立 AWS IoT 資源](#iot-dc-install-dc-provision-resources)

### 建立並下載裝置憑證檔案
<a name="iot-dc-install-dc-provision-certs"></a>

此程序會建立此示範的裝置憑證檔案。

**為 Raspberry Pi 建立並下載裝置憑證檔案**

1. 在本機主機電腦的終端機視窗中，輸入這些命令來建立裝置的裝置憑證檔案。

   ```
   mkdir ~/certs/testconn
   aws iot create-keys-and-certificate \
   --set-as-active \
   --certificate-pem-outfile "~/certs/testconn/device.pem.crt" \
   --public-key-outfile "~/certs/testconn/public.pem.key" \
   --private-key-outfile "~/certs/testconn/private.pem.key"
   ```

   此命令會傳回類似以下的回應。記錄 `certificateArn` 值，供之後使用。

   ```
   {
       "certificateArn": "arn:aws:iot:us-west-2:57EXAMPLE833:cert/76e7e4edb3e52f52334be2f387a06145b2aa4c7fcd810f3aea2d92abc227d269",
       "certificateId": "76e7e4edb3e52f5233EXAMPLE7a06145b2aa4c7fcd810f3aea2d92abc227d269",
       "certificatePem": "-----BEGIN CERTIFICATE-----\nMIIDWTCCAkGgAwIBAgI_SHORTENED_FOR_EXAMPLE_Lgn4jfgtS\n-----END CERTIFICATE-----\n",
       "keyPair": {
           "PublicKey": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BA_SHORTENED_FOR_EXAMPLE_ImwIDAQAB\n-----END PUBLIC KEY-----\n",
           "PrivateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQE_SHORTENED_FOR_EXAMPLE_T9RoDiukY\n-----END RSA PRIVATE KEY-----\n"
       }
   }
   ```

1. 輸入下列命令來設定憑證目錄及其檔案的許可。

   ```
   chmod 745 ~
   chmod 700 ~/certs/testconn
   chmod 644 ~/certs/testconn/*
   chmod 600 ~/certs/testconn/private.pem.key
   ```

1. 執行此命令來檢閱憑證目錄和檔案的許可。

   ```
   ls -l ~/certs/testconn
   ```

   命令的輸出應該與您在此處看到的相同，但檔案日期和時間會有所不同。

   ```
   -rw-r--r-- 1 pi pi 1220 Oct 28 13:02 device.pem.crt
   -rw------- 1 pi pi 1675 Oct 28 13:02 private.pem.key
   -rw-r--r-- 1 pi pi  451 Oct 28 13:02 public.pem.key
   ```

此時，您已在 Raspberry Pi 上安裝了裝置憑證檔案，可以繼續進行 [建立 AWS IoT 資源](#iot-dc-install-dc-provision-resources)。

### 建立 AWS IoT 資源
<a name="iot-dc-install-dc-provision-resources"></a>

此程序 AWS IoT 透過建立裝置存取 AWS IoT 功能和服務所需的資源，在 中佈建您的裝置。

**在 中佈建您的裝置 AWS IoT**

1. 在本機主機電腦的終端機視窗中輸入下列命令，取得 AWS 帳戶裝置資料端點的地址。

   ```
   aws iot describe-endpoint --endpoint-type IoT:Data-ATS
   ```

   先前步驟的命令會傳回類似以下的回應：記錄 `endpointAddress` 值，供之後使用。

   ```
   {
       "endpointAddress": "a3qjEXAMPLEffp-ats.iot.us-west-2.amazonaws.com"
   }
   ```

1. 輸入此命令來建立 Raspberry Pi 的 AWS IoT 物件資源。

   ```
   aws iot create-thing --thing-name "DevCliTestThing"
   ```

   如果您的 AWS IoT 物件資源已建立，命令會傳回類似這樣的回應。

   ```
   {
       "thingName": "DevCliTestThing",
       "thingArn": "arn:aws:iot:us-west-2:57EXAMPLE833:thing/DevCliTestThing",
       "thingId": "8ea78707-32c3-4f8a-9232-14bEXAMPLEfd"
   }
   ```

1. 在終端機視窗中：

   1. 開啟文字編輯器，例如 `nano`。

   1. 複製此 JSON 政策文件並將其貼入開啟的文字編輯器中。  
****  

      ```
      {
          "Version":"2012-10-17",		 	 	 
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "iot:Publish",
                      "iot:Subscribe",
                      "iot:Receive",
                      "iot:Connect"
                  ],
                  "Resource": [
                      "*"
                  ]
              }
          ]
      }
      ```
**注意**  
本政策文件會慷慨地授予每個資源許可，以便進行連線、接收、發佈和訂閱。通常，政策只會授予特定資源許可，以便執行特定動作。不過，對於初始裝置連線能力測試，這個過度寬鬆的政策能夠在此測試期間盡力降低出現存取問題的機率。在後續教學課程中，將使用範圍較窄的政策文件來示範政策設計的更佳實務。

   1. 將文字編輯器中的檔案儲存為 **\$1/policies/dev\$1cli\$1test\$1thing\$1policy.json**。

1. 執行此命令以使用先前步驟的政策文件來建立 AWS IoT 政策。

   ```
   aws iot create-policy \
   --policy-name "DevCliTestThingPolicy" \
   --policy-document "file://~/policies/dev_cli_test_thing_policy.json"
   ```

   如果建立此政策，此命令會傳回類似以下的回應。

   ```
   {
       "policyName": "DevCliTestThingPolicy",
       "policyArn": "arn:aws:iot:us-west-2:57EXAMPLE833:policy/DevCliTestThingPolicy",
       "policyDocument": "{\n    \"Version\": \"2012-10-17\",		 	 	 \n    \"Statement\": [\n        {\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"iot:Publish\",\n                \"iot:Subscribe\",\n                \"iot:Receive\",\n                \"iot:Connect\"\n            ],\n            \"Resource\": [\n                \"*\"\n            ]\n        }\n    ]\n}\n",
       "policyVersionId": "1"
   }
   ```

1. 執行此命令，將此政策連接至裝置憑證。使用之前儲存的 `certificateArn` 值來取代 `certificateArn`

   ```
   aws iot attach-policy \
   --policy-name "DevCliTestThingPolicy" \
   --target "certificateArn"
   ```

   若成功，此命令不會傳回任何內容。

1. 執行此命令，將裝置憑證連接至 AWS IoT 物件資源。使用之前儲存的 `certificateArn` 值來取代 `certificateArn`

   ```
   aws iot attach-thing-principal \
   --thing-name "DevCliTestThing" \
   --principal "certificateArn"
   ```

   若成功，此命令不會傳回任何內容。

成功在 中佈建裝置後 AWS IoT，您就可以繼續 [設定 AWS IoT 裝置用戶端以測試連線](iot-dc-install-configure.md)。