

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

# 在 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 中。