

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

# 处理适用于 AWS PCS 的 Amazon EC2 用户数据
<a name="working-with_ec2-user-data"></a>

您可以在实例启动时`cloud-init`运行的启动模板中提供 EC2 用户数据。内容类型的用户数据块在实例向 AWS PCS API 注册之前`cloud-config`运行，而内容类型的用户数据块在注册完成后但在 Slurm 守护程序启动之前`text/x‑shellscript`运行。有关内容类型的更多信息，请参阅 [cloud-init](https://cloudinit.readthedocs.io/en/latest/explanation/format.html) 文档。

我们的用户数据可以执行常见的配置场景，包括但不限于以下情况：
+  [包括用户或群组](https://cloudinit.readthedocs.io/en/latest/topics/examples.html#including-users-and-groups) 
+  [安装软件包](https://cloudinit.readthedocs.io/en/latest/topics/examples.html#install-arbitrary-packages) 
+  [创建分区和文件系统](https://cloudinit.readthedocs.io/en/latest/topics/examples.html#create-partitions-and-filesystems) 
+  挂载网络文件系统 

 启动模板中的用户数据必须采用 [MIME 多部分存档格式](https://cloudinit.readthedocs.io/en/latest/topics/format.html#mime-multi-part-archive)。这是因为您的用户数据与在节点组中配置节点所需的其他 AWS PCS 用户数据合并。您可以将多个用户数据块合并到一个 MIME 分段文件中。

 MIME 分段文件包含以下组成部分：
+  内容类型和段边界声明：`Content-Type: multipart/mixed; boundary="==BOUNDARY=="`
+  MIME 版本声明：`MIME-Version: 1.0`
+  一个或多个用户数据块，其包含以下组成部分：
  +  开口边界，表示用户数据块的开头：`--==BOUNDARY==`必须将此边界之前的行留空。
  +  区块的内容类型声明：`Content-Type: text/cloud-config; charset="us-ascii"`或`Content-Type: text/x-shellscript; charset="us-ascii"`。必须将内容类型声明之后的行留空。
  +  用户数据的内容，例如，Shell 命令或 `cloud-config` 指令的列表。
+  封闭边界，表示 MIME 分段文件的结尾：`--==BOUNDARY==--`必须将此闭合边界之前的行留空。

**注意**  
 如果您在 Amazon EC2 控制台的启动模板中添加用户数据，则可以将其粘贴为纯文本。或者，您可以从文件上传它。如果您使用 AWS CLI 或 AWS SDK，则必须先对用户数据进行 base64 编码，并在调用时将该字符串作为`UserData`参数值提交 [CreateLaunchTemplate](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateLaunchTemplate.html)，如此 JSON 文件所示。

```
{
    "LaunchTemplateName": "base64-user-data",
    "LaunchTemplateData": {
        "UserData": "ewogICAgIkxhdW5jaFRlbXBsYXRlTmFtZSI6ICJpbmNyZWFzZS1jb250YWluZXItdm9sdW..."
    }
}
```

**示例**
+ [示例：从软件包存储库安装软件](working-with_ec2-user-data_repo.md)
+ [示例：从 S3 存储桶运行脚本](working-with_ec2-user-data_s3.md)
+ [示例：设置全局环境变量](working-with_ec2-user-data_env.md)
+ [在 AWS PCS 上使用网络文件系统](working-with_file-systems.md)
+ [示例：使用 EFS 文件系统作为共享主目录](working-with_ec2-user-data_efs.md)