

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在中配置你的树莓派 Amazon IoT
预调配 Raspberry Pi

本节中的步骤从安装了 Amazon CLI 和 Amazon IoT 设备客户端的保存的 microSD 映像开始，然后创建用于配置 Raspberry Pi 的 Amazon IoT 资源和设备证书。 Amazon IoT

## 在您的 Raspberry Pi 中安装 microSD 卡


此过程将 microSD 卡安装在 Raspberry Pi 中已加载和配置的必要软件，并对其进行配置， Amazon Web Services 账户 以便您可以继续学习本学习路径中的教程。

使用来自 [（可选）保存 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. 在本地主机上、终端窗口中以及**访问密钥 ID**和您的 Raspberry Pi 上的**密钥访问密钥**凭证：

   1. 使用以下命令运行 Amazon 配置应用程序：

      ```
      aws configure
      ```

   1. 出现提示时，输入您的 Amazon Web Services 账户 凭据和配置信息：

      ```
      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 Amazon Web Services 区域 code
      Default output format [json]: json
      ```

恢复 Amazon Web Services 账户 凭证后，就可以继续操作了[在中配置您的设备 Amazon IoT Core](#iot-dc-install-dc-provision)。

## 在中配置您的设备 Amazon IoT Core


本节中的步骤创建了在中配置 Raspberry Pi 的 Amazon IoT 资源 Amazon IoT。创建这些资源时，系统将要求您记录各种信息。 Amazon IoT 设备客户端配置将在下一个步骤中使用此信息。

要使用你的 Raspberry Pi Amazon IoT，必须对其进行配置。配置是创建和配置支持你的 Raspberry Pi 作为物联网设备所需的 Amazon IoT 资源的过程。

启动并重新启动 Raspberry Pi 后，将本地主机上的终端窗口连接到 Raspberry Pi 上并完成这些步骤。

**Topics**
+ [

### 创建并下载设备证书文件
](#iot-dc-install-dc-provision-certs)
+ [

### 创建 Amazon IoT 资源
](#iot-dc-install-dc-provision-resources)

### 创建并下载设备证书文件


此过程为此演示创建设备证书文件。

**要为 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 上安装了设备证书文件，可以继续 [创建 Amazon IoT 资源](#iot-dc-install-dc-provision-resources)。

### 创建 Amazon IoT 资源


此过程 Amazon IoT 通过创建设备访问 Amazon IoT 功能和服务所需的资源来配置您的设备。

**要在中配置您的设备 Amazon IoT**

1. 在本地主机的终端窗口中，输入以下命令获取您的 Amazon Web Services 账户设备数据端点的地址。

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

   上面步骤的命令会返回类似以下内容的响应。记录 `endpointAddress`值以供将来使用。

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

1. 输入此命令为您的 Raspberry Pi 创建 Amazon IoT 事物资源。

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

   如果您的 Amazon 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. 运行此命令使用前面步骤中的策略文档来创建 Amazon 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. 运行此命令将设备证书附加到 Amazon IoT 事物资源。将 `certificateArn`替换为 您之前保存的 `certificateArn`值。

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

   如果成功，该命令不返回任何内容。

在中成功配置设备后 Amazon IoT，就可以继续[配置 Amazon IoT 设备客户端以测试连通性](iot-dc-install-configure.md)操作了。