

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Infrastructure Composer で外部ファイルを参照する
<a name="using-composer-external-files"></a>

( AWS Serverless Application Model AWS SAM) テンプレートで外部ファイルを使用して、繰り返しコードを再利用し、プロジェクトを整理できます。たとえば、 OpenAPI仕様で説明されている複数の Amazon API Gateway REST API リソースがあるとします。テンプレートでOpenAPI仕様コードをレプリケートする代わりに、1 つの外部ファイルを作成し、リソースごとに参照できます。

AWS Infrastructure Composer では、次の外部ファイルのユースケースがサポートされています。
+ 外部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)`。

**注記**  
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>

**ローカル同期**モードでローカル IDE で Infrastructure Composer を使用する場合、ローカル 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) などのアクセス可能な場所にアップロードする必要があります。CLI AWS SAM を使用して、このプロセスを自動的に促進できます。詳細については、「 *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 がプロジェクトのルートフォルダに接続するように指示することを確認する 

ブラウザが File System Access API をサポートしている場合、Infrastructure Composer はプロジェクトのルートフォルダに接続するように求めるプロンプトを表示します。Infrastructure Composer は、外部ファイルをサポートするために**ローカル同期**モードでプロジェクトを開きます。参照される外部ファイルがサポートされていない場合は、エラーメッセージが表示されます。エラーメッセージの詳細については、「」を参照してください[トラブルシューティング](ref-troubleshooting.md)。

**Toolkit for VS Code から**

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 project folder window with our project folder selected and the Create ready to be selected。\]](http://docs.aws.amazon.com/ja_jp/infrastructure-composer/latest/dg/images/aac_use_ex_15.png)

Infrastructure Composer は、外部ステートマシン定義ファイルを自動的に検出してロードします。**StockTradingStateMachine** リソースを選択し、**詳細**を選択して**リソースプロパティ**パネルを表示します。ここでは、Infrastructure Composer が外部ステートマシン定義ファイルに自動的に接続されていることを確認できます。

![\[API Gateway リソースプロパティパネルが表示された Infrastructure Composer キャンバスビュー。外部リファレンスファイルの設定が表示されます。\]](http://docs.aws.amazon.com/ja_jp/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 Activate local sync window with a demo project folder selected and the Activate button is ready to be selected。\]](http://docs.aws.amazon.com/ja_jp/infrastructure-composer/latest/dg/images/aac_use_ex_11.png)


次に、Amazon API Gateway カードをキャンバスにドラッグします。**詳細**を選択して、**リソースプロパティ**パネルを表示します。

![\[リソースプロパティパネルが開いたキャンバス上の API Gateway リソース。\]](http://docs.aws.amazon.com/ja_jp/infrastructure-composer/latest/dg/images/aac_use_ex_12.png)


**リソースプロパティ**パネルから、以下を設定して**保存します**。
+ **API 定義に外部ファイルを使用する**オプションを選択します。
+ **外部ファイルへの相対パス**`./api-spec.yaml`として入力する

![\[「API 定義に外部ファイルを使用する」とマークされたチェックボックスと、定義された外部ファイルへの相対パスを示すウィンドウ。\]](http://docs.aws.amazon.com/ja_jp/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 **Template** ビューでは、Infrastructure Composer が外部ファイルを参照するようにテンプレートを自動的に更新したことを確認できます。

![\[外部ファイルを参照するように設定されたインフラストラクチャコードを示す Infrastructure Composer テンプレートビュー。\]](http://docs.aws.amazon.com/ja_jp/infrastructure-composer/latest/dg/images/aac_use_ex_07.png)
