

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

# 參考 Infrastructure Composer 中的外部檔案
<a name="using-composer-external-files"></a>

您可以使用外部檔案搭配您的 AWS Serverless Application Model (AWS SAM) 範本，重複使用重複的程式碼並整理您的專案。例如，您可能有規格所述的多個 Amazon API Gateway REST API 資源OpenAPI。您可以建立一個外部檔案，並參考每個資源，而不是複寫範本中的OpenAPI規格程式碼。

AWS Infrastructure Composer 支援下列外部檔案使用案例：
+ 外部OpenAPI規格檔案定義的 API Gateway REST API 資源。
+ AWS Step Functions 外部狀態機器定義檔案定義的狀態機器資源。

若要進一步了解如何為支援的資源設定外部檔案，請參閱下列內容：
+ `[ DefinitionBody](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-definitionbody)` 適用於 `AWS::Serverless::Api`。
+ `[ DefinitionUri](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionuri)` 適用於 `AWS::Serverless::StateMachine`。

**注意**  
若要從 Infrastructure Composer 主控台參考具有 Infrastructure Composer 的外部檔案，您必須在**本機同步**模式下使用 Infrastructure Composer。如需詳細資訊，請參閱[在 Infrastructure Composer 主控台中本機同步和儲存您的專案](using-composer-project-local-sync.md)。

**Topics**
+ [

# Infrastructure Composer 外部參考檔案的最佳實務
](using-composer-external-files-best-practices.md)
+ [

# 在 Infrastructure Composer 中建立外部檔案參考
](using-composer-external-files-new.md)
+ [

# 在 Infrastructure Composer 中使用外部檔案參考載入專案
](using-composer-external-files-load.md)
+ [

# 在 Infrastructure Composer 中建立參考外部檔案的應用程式
](using-composer-external-files-examples-example3.md)
+ [

# 使用 Infrastructure Composer 參考OpenAPI規格外部檔案
](using-composer-external-files-examples-example1.md)

# Infrastructure Composer 外部參考檔案的最佳實務
<a name="using-composer-external-files-best-practices"></a>

## 搭配本機 IDE 使用 Infrastructure Composer
<a name="using-composer-external-files-best-practices-ide"></a>

在本機**同步**模式下使用 Infrastructure Composer 搭配本機 IDE 時，您可以使用本機 IDE 來檢視和修改外部檔案。範本上參考之支援外部檔案的內容，會在 Infrastructure Composer 畫布中自動更新。如需詳細資訊，請參閱 [將 Infrastructure Composer 主控台與本機 IDE 連接](other-services-ide.md)。

## 將外部檔案保留在專案的父目錄中
<a name="using-composer-external-files-best-practices-directory"></a>

您可以在專案的父目錄中建立子目錄，以整理外部檔案。Infrastructure Composer 無法存取存放在專案父目錄外部目錄中的外部檔案。

## 使用 部署您的應用程式 AWS SAM CLI
<a name="using-composer-external-files-best-practices-sam"></a>

將應用程式部署到 時 AWS 雲端，需要先將本機外部檔案上傳到可存取的位置，例如 Amazon Simple Storage Service (Amazon S3)。您可以使用 AWS SAM CLI 自動促進此程序。若要進一步了解，請參閱《 *AWS Serverless Application Model 開發人員指南*》中的在[部署時上傳本機檔案](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/deploy-upload-local-files.html)。

# 在 Infrastructure Composer 中建立外部檔案參考
<a name="using-composer-external-files-new"></a>

您可以從支援**資源的資源屬性**面板建立外部檔案參考。

**建立外部檔案參考**

1. 從 **API Gateway** 或 **Step Functions** 增強型元件卡中，選取**詳細資訊**以顯示**資源屬性**面板。

1. 尋找並選取**使用外部檔案**選項。

1. 指定外部檔案的相對路徑。這是從 `template.yaml` 檔案到外部 檔案的路徑。

   例如，若要參考來自下列專案結構的`api-spec.yaml`外部檔案，請指定 `./api-spec.yaml`做為您的相對路徑。

   ```
   demo
   ├── api-spec.yaml
   ├── src
   │ └── Function
   │ ├── index.js
   │ └── package.json
   └── template.yaml
   ```
**注意**  
如果外部檔案及其指定的路徑不存在，則 Infrastructure Composer 會建立它。

1. **儲存**您的變更。

# 在 Infrastructure Composer 中使用外部檔案參考載入專案
<a name="using-composer-external-files-load"></a>

請依照此頁面列出的步驟，使用外部檔案參考載入 Infrastructure Composer 專案。

**從 Infrastructure Composer 主控台**

1. 完成列於 [在 Infrastructure Composer 主控台中匯入現有的專案範本](using-composer-project-import-template.md) 的步驟。

1. 確認 Infrastructure Composer 會提示您連線到專案的根資料夾 

如果您的瀏覽器支援檔案系統存取 API， Infrastructure Composer 會提示您連線到專案的根資料夾。Infrastructure Composer 將以**本機同步**模式開啟您的專案，以支援您的外部檔案。如果不支援參考的外部檔案，您將會收到錯誤訊息。如需錯誤訊息的詳細資訊，請參閱 [疑難排解](ref-troubleshooting.md)。

**從 Toolkit for VS 程式碼**

1. 完成列於 [從 存取 Infrastructure Composer AWS Toolkit for Visual Studio Code](setting-up-composer-access-ide.md) 的步驟。

1. 開啟您要在 Infrastructure Composer 中檢視的範本。

當您從範本存取 Infrastructure Composer 時， Infrastructure Composer 會自動偵測您的外部檔案。如果不支援參考的外部檔案，您將會收到錯誤訊息。如需錯誤訊息的詳細資訊，請參閱 [疑難排解](ref-troubleshooting.md)。

# 在 Infrastructure Composer 中建立參考外部檔案的應用程式
<a name="using-composer-external-files-examples-example3"></a>

此範例使用 AWS SAM CLI 來建立參考其狀態機器定義的外部檔案的應用程式。然後，在 Infrastructure Composer 中載入專案，並正確參考外部檔案。

**範例**

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. 接著，從主控台在 Infrastructure Composer 中載入您的應用程式。在 Infrastructure Composer **首頁**中，選取**載入 CloudFormation 範本**。

1. 選取我們的`demo`專案資料夾，並允許 提示檢視檔案。選取我們的`template.yaml`檔案，然後選取**建立**。出現提示時，選取**儲存變更**。  
![\[已選取 Infrastructure Composer Open 專案資料夾視窗，並已選取 Create 準備。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ex_15.png)

Infrastructure Composer 會自動偵測並載入外部狀態機器定義檔案。選取我們的 **StockTradingStateMachine** 資源，然後選擇**詳細資訊**以顯示**資源屬性**面板。在這裡，您可以看到 Infrastructure Composer 已自動連接到我們的外部狀態機器定義檔案。

![\[顯示 API Gateway 資源屬性面板的 Infrastructure Composer 畫布檢視，顯示外部參考檔案的組態。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ex_03.png)


對狀態機器定義檔案所做的任何變更都會自動反映在 Infrastructure Composer 中。

# 使用 Infrastructure Composer 參考OpenAPI規格外部檔案
<a name="using-composer-external-files-examples-example1"></a>

此範例使用來自 主控台的 Infrastructure Composer 來參考定義 API Gateway 的外部OpenAPI規格檔案REST API。

首先，從 Infrastructure Composer **首頁**建立新的專案。

接著，從**選單**中選取啟用**本機同步**來**啟用本機同步**。建立名為 的新資料夾`demo`，允許提示檢視檔案，然後選取**啟用**。出現提示時，選取**儲存變更**。

![\[Infrastructure Composer 啟用本機同步視窗，其中已選取示範專案資料夾，且已就緒可選取啟用按鈕。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ex_11.png)


接著，將 Amazon API Gateway 卡拖曳到畫布上。選取**詳細資訊**以顯示**資源屬性**面板。

![\[畫布上的 API Gateway 資源，並開啟資源屬性面板。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ex_12.png)


從**資源屬性**面板中，設定下列項目並**儲存**。
+ 選取**使用外部檔案進行 api 定義**選項。
+ 輸入 `./api-spec.yaml`做為**外部檔案的相對路徑**

![\[視窗顯示標記在使用外部檔案進行 api 定義下的核取方塊，以及定義外部檔案的相對路徑。\]](http://docs.aws.amazon.com/zh_tw/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
```

在 Infrastructure Composer **範本**檢視中，您可以看到 Infrastructure Composer 已自動更新您的範本以參考外部檔案。

![\[Infrastructure Composer 範本檢視，顯示您設定為參考外部檔案的基礎設施程式碼。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ex_07.png)
