

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

# 在基础架构编辑器中引用外部文件
<a name="using-composer-external-files"></a>

您可以将外部文件与 AWS Serverless Application Model (AWS SAM) 模板配合使用，以重复使用代码并组织项目。例如，您可能有多个由OpenAPI规范描述的 Amazon API Gateway REST API 资源。您可以创建一个外部文件并为每个资源引用该文件，而不必在模板中复制OpenAPI规范代码。

AWS 基础架构编辑器 支持以下外部文件用例：
+ 由外部OpenAPI规范文件定义的 API Gateway REST API 资源。
+ AWS Step Functions 由外部状态机定义文件定义的状态机资源。

要了解有关为支持的资源配置外部文件的更多信息，请参阅以下内容：
+ 用于 `AWS::Serverless::Api` 的 `[ DefinitionBody](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-definitionbody)`。
+ 用于 `AWS::Serverless::StateMachine` 的 `[ DefinitionUri](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionuri)`。

**注意**  
要通过基础设施编排控制台使用基础设施编排器引用外部文件，您必须在**本地同步**模式下使用基础设施编排器。有关更多信息，请参阅 [在基础设施编排控制台中本地同步并保存您的项目](using-composer-project-local-sync.md)。

**Topics**
+ [基础设施编排器外部参考文件的最佳实践](using-composer-external-files-best-practices.md)
+ [在基础设施编排器中创建外部文件引用](using-composer-external-files-new.md)
+ [在基础架构编辑器中加载带有外部文件引用的项目](using-composer-external-files-load.md)
+ [在基础架构编排器中创建引用外部文件的应用程序](using-composer-external-files-examples-example3.md)
+ [使用基础设施编排器引用OpenAPI规范外部文件](using-composer-external-files-examples-example1.md)

# 基础设施编排器外部参考文件的最佳实践
<a name="using-composer-external-files-best-practices"></a>

## 在本地 IDE 中使用基础架构编排器
<a name="using-composer-external-files-best-practices-ide"></a>

在**本地同步**模式下将基础设施编排器与本地 IDE 配合使用时，可以使用本地 IDE 来查看和修改外部文件。您的模板中引用的受支持的外部文件中的内容将在基础架构编辑器画布中自动更新。要了解更多信息，请参阅[将基础架构编排控制台与本地 IDE 连接起来](other-services-ide.md)。

## 将外部文件保存在项目的父目录中
<a name="using-composer-external-files-best-practices-directory"></a>

可以在项目的父目录中创建子目录来整理外部文件。Infrastructure Composer 无法访问存储在项目父目录之外的目录中的外部文件。

## 使用部署您的应用程序 AWS SAMCLI
<a name="using-composer-external-files-best-practices-sam"></a>

将应用程序部署到时 AWS 云，需要先将本地外部文件上传到可访问的位置，例如亚马逊简单存储服务 (Amazon S3) Service。您可以使用 AWS SAM CLI 自动简化此过程。要了解更多信息，请参阅《*AWS Serverless Application Model 开发人员指南》*中的[部署时上传本地文件](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/deploy-upload-local-files.html)。

# 在基础设施编排器中创建外部文件引用
<a name="using-composer-external-files-new"></a>

您可以从支持的**资源的资源属性**面板中创建外部文件引用。

**创建外部文件引用**

1. 从 **API Gateway** 或 **Step Fun** ctions 增强组件卡中，选择**详细信息**以打开**资源属性**面板。

1. 找到并选择 “**使用外部文件**” 选项。

1. 指定外部文件的相对路径。这是从您的`template.yaml`文件到外部文件的路径。

   例如，要引用以下项目结构中的`api-spec.yaml`外部文件，请指定`./api-spec.yaml`为相对路径。

   ```
   demo
   ├── api-spec.yaml
   ├── src
   │ └── Function
   │ ├── index.js
   │ └── package.json
   └── template.yaml
   ```
**注意**  
如果外部文件及其指定路径不存在，则基础架构编排器将创建该文件。

1. **保存**您的更改。

# 在基础架构编辑器中加载带有外部文件引用的项目
<a name="using-composer-external-files-load"></a>

按照本页列出的步骤加载带有外部文件引用的基础架构编排器项目。

**从基础设施编排控制台中**

1. 完成 [在基础设施编排控制台中导入现有项目模板](using-composer-project-import-template.md) 中列出的步骤。

1. 确认基础架构 Composer 会提示你连接到项目的根文件夹 

如果您的浏览器支持 File System Access API，Infrastructure Composer 将提示您连接到项目的根文件夹。Infrastructure Composer 将在**本地同步**模式下打开您的项目，以支持您的外部文件。如果不支持引用的外部文件，您将收到一条错误消息。有关错误消息的更多信息，请参阅[问题排查](ref-troubleshooting.md)。

**来自 VS Code 的 Toolkit for**

1. 完成 [从访问基础架构编排器 AWS Toolkit for Visual Studio Code](setting-up-composer-access-ide.md) 中列出的步骤。

1. 在基础设施编辑器中打开要查看的模板。

当您从模板访问基础设施编排器时，基础设施编排器将自动检测您的外部文件。如果不支持引用的外部文件，您将收到一条错误消息。有关错误消息的更多信息，请参阅[问题排查](ref-troubleshooting.md)。

# 在基础架构编排器中创建引用外部文件的应用程序
<a name="using-composer-external-files-examples-example3"></a>

此示例使用创建了一个引用外部文件作为其状态机定义的应用程序。 AWS SAMCLI然后，在基础架构编排器中加载项目，并正确引用外部文件。

**示例**

1. 首先，使用 AWS SAM CLI **sam init** 命令初始化名为的新应用程序`demo`。在交互流程中，选择**多步骤工作流**快速入门模板。

   ```
   $ sam init
   
   ...
   
   Which template source would you like to use?
           1 - AWS Quick Start Templates
           2 - Custom Template Location
   Choice: 1
   
   Choose an AWS Quick Start application template
           1 - Hello World Example
           2 - Multi-step workflow
           3 - Serverless API
           4 - Scheduled task
           ...
   Template: 2
   
   Which runtime would you like to use?
           1 - dotnet6
           2 - dotnetcore3.1
           ...
           15 - python3.7
           16 - python3.10
           17 - ruby2.7
   Runtime: 16
   
   Based on your selections, the only Package type available is Zip.
   We will proceed to selecting the Package type as Zip.
   
   Based on your selections, the only dependency manager available is pip.
   We will proceed copying the template using pip.
   
   Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: ENTER
   
   Would you like to enable monitoring using CloudWatch Application Insights?
   For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: ENTER
   
   Project name [sam-app]: demo
   
       -----------------------
       Generating application:
       -----------------------
       Name: demo
       Runtime: python3.10
       Architectures: x86_64
       Dependency Manager: pip
       Application Template: step-functions-sample-app
       Output Directory: .
       Configuration file: demo/samconfig.toml
       
       Next steps can be found in the README file at demo/README.md
   
   ...
   ```

   此应用程序引用状态机定义的外部文件。

   ```
   ...
   Resources:
     StockTradingStateMachine:
       Type: AWS::Serverless::StateMachine
       Properties:
         DefinitionUri: statemachine/stock_trader.asl.json
   ...
   ```

   外部文件位于我们应用程序的`statemachine`子目录中。

   ```
   demo
   ├── README.md
   ├── __init__.py
   ├── functions
   │   ├── __init__.py
   │   ├── stock_buyer
   │   ├── stock_checker
   │   └── stock_seller
   ├── samconfig.toml
   ├── statemachine
   │   └── stock_trader.asl.json
   ├── template.yaml
   └── tests
   ```

1. 接下来，从控制台在基础设施编排器中加载您的应用程序。在基础设施编排器**主页**上，选择**加载CloudFormation 模板**。

1. 选择我们的`demo`项目文件夹，然后允许提示查看文件。选择我们的`template.yaml`文件并选择 “**创建**”。出现提示时，选择**保存更改**。  
![\[Infrastructure Composer “打开项目文件夹” 窗口，选中我们的项目文件夹，“创建” 准备就绪。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_15.png)

基础架构 Composer 会自动检测外部状态机定义文件并将其加载。选择我们的**StockTradingStateMachine**资源并选择**详细信息**以显示**资源属性**面板。在这里，您可以看到基础设施编排器已自动连接到我们的外部状态机定义文件。

![\[显示了 API Gateway 资源属性面板的 Infrastructure Composer 画布视图，显示了外部参考文件的配置。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_03.png)


对状态机定义文件所做的任何更改都将自动反映在基础设施编排器中。

# 使用基础设施编排器引用OpenAPI规范外部文件
<a name="using-composer-external-files-examples-example1"></a>

此示例使用控制台中的 Infrastructure Composer 来引用定义 API Gateway 的外部OpenAPI规范文件REST API。

首先，从基础设施编排器**主页**创建一个新项目。

**接下来，从**菜单**中选择 “**激活本地同步**”，激活本地同步。**创建一个名为的新文件夹`demo`，允许提示查看文件，然后选择 “**激活**”。出现提示时，选择**保存更改**。

![\[Infrastructure Composer Activate 本地同步窗口已选中演示项目文件夹，“激活” 按钮已准备就绪，可供选择。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_11.png)


接下来，将 Amazon API Gateway 卡片拖到画布上。选择**详细信息**以打开**资源属性**面板。

![\[画布上有一个 API Gateway 资源，资源属性面板处于打开状态。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_12.png)


在**资源属性**面板中，配置以下内容并**保存**。
+ 选择 “**使用外部文件进行 API 定义**” 选项。
+ 输入`./api-spec.yaml`作为**外部文件的相对路径**

![\[一个窗口，显示在 “使用外部文件进行 api 定义” 下标记的复选框以及定义的外部文件的相对路径。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_13.png)


这将在我们的本地计算机上创建以下目录：

```
demo
└── api-spec.yaml
```

现在，你可以在我们的本地计算机上配置外部文件。使用我们的 IDE，打开`api-spec.yaml`位于您的项目文件夹中的。将其内容替换为以下内容：

```
openapi: '3.0'
info: {}
paths:
  /:
    get:
      responses: {}
    post:
      x-amazon-apigateway-integration:
        credentials:
          Fn::GetAtt:
            - ApiQueuesendmessageRole
            - Arn
        httpMethod: POST
        type: aws
        uri:
          Fn::Sub: arn:${AWS::Partition}:apigateway:${AWS::Region}:sqs:path/${AWS::AccountId}/${Queue.QueueName}
        requestParameters:
          integration.request.header.Content-Type: '''application/x-www-form-urlencoded'''
        requestTemplates:
          application/json: Action=SendMessage&MessageBody={"data":$input.body}
        responses:
          default:
            statusCode: 200
      responses:
        '200':
          description: 200 response
```

在基础设施编排器**模板**视图中，您可以看到基础设施编排器已自动更新您的模板以引用外部文件。

![\[“基础设施编排器” 模板视图显示配置为引用外部文件的基础架构代码。\]](http://docs.aws.amazon.com/zh_cn/infrastructure-composer/latest/dg/images/aac_use_ex_07.png)
