

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

# 使用 CloudFormation 创建亚马逊 OpenSearch 无服务器集合

你可以使用 CloudFormation 创建亚马逊 OpenSearch 无服务器资源，例如集合、安全策略和 VPC 终端节点。为了全面 OpenSearch 无服务器 CloudFormation 参考，见 [亚马逊 OpenSearch 无服务器](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_OpenSearchServerless.html) 在 *CloudFormation 用户指南*。

**注意**  
要阅读馆藏并更改其标签， CloudFormation 需要 OpenSearch 无服务器标记权限。把它们交给校长 CloudFormation 使用：您的证书，或者堆栈的服务角色（如果您指定）。漂移检测始终使用您自己的证书。有关权限和需要权限的操作的信息，请参阅 [为集合添加标签所需的权限](tag-collection.md#collection-tag-permissions)。

以下示例 CloudFormation 模板创建了简单的数据访问策略、网络策略和安全策略以及匹配的集合。这是在亚马逊上快速启动和运行的好方法 OpenSearch 无服务器，并提供创建和使用集合所需的元素。

**重要**  
此示例使用公共网络访问权限，建议不要将其用于生产工作负载。我们建议使用 VPC 访问来保护您的集合。欲了解更多信息，请参阅 [AWS::OpenSearchServerless::VpcEndpoint](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-opensearchserverless-vpcendpoint.html) 和 [通过以下方式访问数据平面 AWS PrivateLink](serverless-vpc.md)。

```yaml
AWSTemplateFormatVersion: 2010-09-09
Description: 'Amazon OpenSearch Serverless template to create an IAM user, encryption policy, network policy, data access policy and collection'
Resources:
  IAMUSer:
    Type: 'AWS::IAM::User'
    Properties:
      UserName:  aossadmin
  DataAccessPolicy:
    Type: 'AWS::OpenSearchServerless::AccessPolicy'
    Properties:
      Name: quickstart-access-policy
      Type: data
      Description: Access policy for quickstart collection
      Policy: !Sub >-
        [{"Description":"Access for cfn user","Rules":[{"ResourceType":"index","Resource":["index/*/*"],"Permission":["aoss:*"]},
        {"ResourceType":"collection","Resource":["collection/quickstart"],"Permission":["aoss:*"]}],
        "Principal":["arn:aws:iam::${AWS::AccountId}:user/aossadmin"]}]
  NetworkPolicy:
    Type: 'AWS::OpenSearchServerless::SecurityPolicy'
    Properties:
      Name: quickstart-network-policy
      Type: network
      Description: Network policy for quickstart collection
      Policy: >-
        [{"Rules":[{"ResourceType":"collection","Resource":["collection/quickstart"]}, {"ResourceType":"dashboard","Resource":["collection/quickstart"]}],"AllowFromPublic":true}]
  EncryptionPolicy:
    Type: 'AWS::OpenSearchServerless::SecurityPolicy'
    Properties:
      Name: quickstart-security-policy
      Type: encryption
      Description: Encryption policy for quickstart collection
      Policy: >-
        {"Rules":[{"ResourceType":"collection","Resource":["collection/quickstart"]}],"AWSOwnedKey":true}
  Collection:
    Type: 'AWS::OpenSearchServerless::Collection'
    Properties:
      Name: quickstart
      Type: TIMESERIES
      Description: Collection to holds timeseries data
    DependsOn: EncryptionPolicy
Outputs:
  IAMUser:
    Value: !Ref IAMUSer
  DashboardURL:
    Value: !GetAtt Collection.DashboardEndpoint
  CollectionARN:
    Value: !GetAtt Collection.Arn
```