

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

# 如何在 中編寫 AWS Infrastructure Composer
<a name="using-composer-basics"></a>

本節涵蓋從 [Infrastructure Composer 主控台](using-composer-console.md)、 [CloudFormation 主控台模式](using-composer-console-cfn-mode.md)和 使用 Infrastructure Composer 的基本概念[AWS Toolkit for Visual Studio Code](using-composer-ide.md)。更具體地說，本節中的主題提供如何使用 Infrastructure Composer 編寫應用程式的關鍵詳細資訊，並包含其他功能和捷徑的詳細資訊。主控台和 VS Code 體驗之間的功能有一些變化，本節中的主題會識別並描述這些變化的發生位置。

編寫應用程式後，您將準備好[將您的 Infrastructure Composer 無伺服器應用程式部署至 AWS 雲端](other-services-cfn.md)檢閱有關部署應用程式的資訊。

**Topics**
+ [將卡片放在 Infrastructure Composer 的視覺化畫布上](reference-navigation.md)
+ [在 Infrastructure Composer 的視覺化畫布上將卡片分組在一起](reference-navigation-gestures-group.md)
+ [在 Infrastructure Composer 的視覺化畫布上連接卡片](reference-navigation-gestures-connect.md)
+ [中斷 Infrastructure Composer 中的卡片連線](reference-navigation-gestures-disconnect.md)
+ [在 Infrastructure Composer 的視覺化畫布上排列卡片](reference-navigation-gestures-arrange.md)
+ [在 Infrastructure Composer 中設定和修改卡片](using-composer-cards.md)
+ [在 Infrastructure Composer 中刪除卡片](using-composer-cards-delete.md)
+ [使用 Infrastructure Composer 中的 Change Inspector 檢視程式碼更新](using-change-inspector.md)
+ [參考 Infrastructure Composer 中的外部檔案](using-composer-external-files.md)
+ [將 Infrastructure Composer 與 Amazon Virtual Private Cloud (Amazon VPC) 整合](using-composer-services-vpc.md)

# 將卡片放在 Infrastructure Composer 的視覺化畫布上
<a name="reference-navigation"></a>

本節說明如何在 Infrastructure Composer [卡](using-composer-connecting.md)的視覺化畫布中選取和拖曳卡片。開始之前，請確定您的應用程式需要哪些資源，以及它們的互動方式。如需執行此作業的秘訣，請參閱 [使用 Infrastructure Composer 建置您的第一個應用程式](getting-started-build.md)。

若要將卡片新增至應用程式，請將其從資源調色盤拖曳到視覺化畫布上。

![\[從資源調色盤選取資源，並將其拖曳至 Infrastructure Composer 畫布。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_ref_05.gif)


您可以從兩種類型的卡中進行選擇：[增強型元件卡](using-composer-cards-component-intro-enhanced.md)和[標準 IaC 資源卡](using-composer-cards-resource-intro.md)。

在視覺化畫布上放置卡片之後，您就可以分組、連線、安排和設定卡片。如需執行此作業的相關資訊，請參閱下列主題：
+ [在 Infrastructure Composer 的視覺化畫布上將卡片分組在一起](reference-navigation-gestures-group.md)
+ [在 Infrastructure Composer 的視覺化畫布上連接卡片](reference-navigation-gestures-connect.md)
+ [在 Infrastructure Composer 的視覺化畫布上排列卡片](reference-navigation-gestures-arrange.md)
+ [在 Infrastructure Composer 中設定和修改卡片](using-composer-cards.md)

# 在 Infrastructure Composer 的視覺化畫布上將卡片分組在一起
<a name="reference-navigation-gestures-group"></a>

本主題包含有關分組增強型元件卡和標準元件卡的詳細資訊。分組卡可協助您分類和組織資源，而無需考慮您需要寫入的程式碼或標記。

## 分組增強型元件卡
<a name="w2aac17c21b7"></a>

有兩種方式可將增強型元件卡分組在一起：
+ 按下 **Shift** 時，選取要分組的卡片。然後，從資源動作功能表中選擇**群組**。
+ 在群組中選取您想要的卡片。從顯示的功能表中，選取**群組**。這會建立您可以拖放其他卡片的群組。

![\[選取多個 Lambda 函數並將其分組在一起。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_ref_07.gif)


## 將標準元件卡分組到另一個
<a name="using-composer-cards-group-standard-component"></a>

下列範例顯示從**資源屬性**面板將標準元件卡分組到另一個卡片的一種方式：

![\[標準元件卡的資源屬性面板。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_cards_17.png)


在**資源屬性面板上的資源組態**欄位中，`Role`已在 Lambda 函數中參考 。 ****這會導致**角色**卡分組到畫布上的**函數**卡。

# 在 Infrastructure Composer 的視覺化畫布上連接卡片
<a name="reference-navigation-gestures-connect"></a>

使用此主題來了解如何在 Infrastructure Composer 中連接卡片。本節包含有關連接增強型元件卡和標準元件卡的詳細資訊。它也提供一些範例，說明卡片連線的不同方式。

## 連接增強型元件卡
<a name="reference-navigation-gestures-connect-enhanced"></a>

在增強型元件卡上，連接埠會以視覺化方式識別可進行連線的位置。
+ 卡片右側的連接埠表示卡片有機會叫用另一張卡片。
+ 卡片左側的連接埠表示卡片有機會被另一張卡片叫用。

按一下一張卡片的右連接埠並將其拖曳至另一張卡片的左連接埠，以將卡片連接在一起。

![\[正在連線至 Lambda 函數的 API Gateway 卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_con_02.png)


當您建立連線時，會顯示一則訊息，讓您知道連線是否成功。選取訊息以查看 Infrastructure Composer 如何變更以佈建連線。如果連線失敗，您可以選取**範本檢視**以手動更新您的基礎設施程式碼來佈建連線。
+ 成功時，請按一下訊息以檢視**變更檢查程式**。在這裡，您可以查看修改了哪些 Infrastructure Composer 來佈建您的連線。
+ 失敗時，會顯示訊息。您可以選取**範本檢視**，並手動更新您的基礎設施程式碼來佈建連線。

![\[從畫面底部調出 Change Inspector。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ci_02.gif)


當您將增強型元件卡連接在一起時， Infrastructure Composer 會自動在範本中建立基礎設施程式碼，以佈建資源之間的事件驅動關係。

## 連接標準元件卡 （標準 IaC 資源卡）
<a name="reference-navigation-gestures-connect-standard"></a>

標準 IaC 資源卡不包含用於建立與其他資源連線的連接埠。在[卡片組態](using-composer-standard-cards.md)期間，您可以在應用程式的範本中指定事件驅動關係， Infrastructure Composer 會自動偵測這些連線，並在卡片之間使用虛線將它們視覺化。以下是標準元件卡與增強型元件卡之間的連線範例：

![\[連接至增強型元件卡的標準元件卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_con_04.png)


下列範例顯示 Lambda 函數如何與 Amazon API Gateway 靜態 API 連線：

```
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyApi:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: MyApi

  ApiGatewayMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      HttpMethod: POST  # Specify the HTTP method you want to use (e.g., GET, POST, PUT, DELETE)
      ResourceId: !GetAtt MyApi.RootResourceId
      RestApiId: !Ref MyApi
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub
          - arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunctionArn}/invocations
          - { LambdaFunctionArn: !GetAtt MyLambdaFunction.Arn }
      MethodResponses:
        - StatusCode: 200

  MyLambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: nodejs14.x
      Code:
        S3Bucket: your-bucket-name
        S3Key: your-lambda-zip-file.zip

  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: 'sts:AssumeRole'
      Policies:
        - PolicyName: LambdaExecutionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - 'logs:CreateLogGroup'
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource: 'arn:aws:logs:*:*:*'
              - Effect: Allow
                Action:
                  - 'lambda:InvokeFunction'
                Resource: !GetAtt MyLambdaFunction.Arn
```

在上述範例中， `ApiGatewayMethod:` 下列出的程式碼片段會`Integration:`指定連接兩張卡片的事件驅動關係。

# 在 Infrastructure Composer 中連接卡片的範例
<a name="using-composer-connecting-examples"></a>

使用本節中的範例來了解如何在 Infrastructure Composer 中連接卡片。

## 當項目放置在 Amazon Simple Storage Service (Amazon S3) 儲存貯體時，叫用 AWS Lambda 函數
<a name="using-composer-connecting-examples-example1"></a>

在此範例中，**Amazon S3 儲存貯**體卡連接到 **Lambda 函數**卡。在 Amazon S3 儲存貯體中放置項目時，會叫用 函數。然後，該函數可用於處理項目或觸發應用程式中的其他事件。

![\[從 Amazon S3 儲存貯體資源右側連接埠到 Lambda 函數資源左側連接埠的連線。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_05.png)


此互動需要為函數定義事件。以下是 Infrastructure Composer 佈建的內容：

```
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    ...
  MyBucketBucketPolicy:
    Type: AWS::S3::BucketPolicy
    ...
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Events:
        MyBucket:
          Type: S3
          Properties:
            Bucket: !Ref MyBucket
            Events:
              - s3:ObjectCreated:* # Event that triggers invocation of function
              - s3:ObjectRemoved:* # Event that triggers invocation of function
```

## 從 Lambda 函數叫用 Amazon S3 儲存貯體
<a name="using-composer-connecting-examples-example2"></a>

在此範例中，**Lambda 函數**卡會叫用 **Amazon S3 儲存貯**體卡。Lambda 函數可用於對 Amazon S3 儲存貯體中的項目執行 CRUD 操作。

![\[從 Lambda 函數資源右側連接埠到 Amazon S3 儲存貯體資源左側連接埠的連線。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_06.png)


此互動需要下列項目，由 Infrastructure Composer 佈建：
+ 允許 Lambda 函數與 Amazon S3 儲存貯體互動的 IAM 政策。
+ 影響 Lambda 函數行為的環境變數。

```
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    ...
  MyBucketBucketPolicy:
    Type: AWS::S3::BucketPolicy
    ...
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Environment:
        Variables:
          BUCKET_NAME: !Ref MyBucket
          BUCKET_ARN: !GetAtt MyBucket.Arn
      Policies:
        - S3CrudPolicy:
          BucketName: !Ref MyBucket
```

# 中斷 Infrastructure Composer 中的卡片連線
<a name="reference-navigation-gestures-disconnect"></a>

在 Infrastructure Composer 中，您可以使用*增強型元件卡*和*標準元件卡*來連接和中斷連接 AWS 資源。本節說明如何中斷這兩種卡片類型的連線。

## 增強型元件卡
<a name="using-composer-connecting-enhanced-disconnect"></a>

若要中斷連線增強型元件卡，請選取該行，然後選擇**中斷連線**。

![\[正在從 Lambda 函數中斷連線的 API Gateway 卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_con_03.png)


Infrastructure Composer 將自動修改您的範本，以從您的應用程式移除事件驅動關係。

## 標準元件卡
<a name="w2aac17c31b7"></a>

標準元件卡不包含用於建立與其他資源連線的連接埠。在[卡片組態](using-composer-standard-cards.md)期間，您可以在應用程式的範本中指定事件驅動關係， Infrastructure Composer 會自動偵測這些連線，並在卡片之間使用虛線將其視覺化。若要中斷連接標準元件卡，請移除應用程式範本中的事件驅動關係。

下列範例顯示與 Amazon API Gateway 靜態 API 連線的 Lambda 函數：

```
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyApi:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: MyApi

  ApiGatewayMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      HttpMethod: POST  # Specify the HTTP method you want to use (e.g., GET, POST, PUT, DELETE)
      ResourceId: !GetAtt MyApi.RootResourceId
      RestApiId: !Ref MyApi
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub
          - arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunctionArn}/invocations
          - { LambdaFunctionArn: !GetAtt MyLambdaFunction.Arn }
      MethodResponses:
        - StatusCode: 200

  MyLambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: nodejs14.x
      Code:
        S3Bucket: your-bucket-name
        S3Key: your-lambda-zip-file.zip

  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: 'sts:AssumeRole'
      Policies:
        - PolicyName: LambdaExecutionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - 'logs:CreateLogGroup'
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource: 'arn:aws:logs:*:*:*'
              - Effect: Allow
                Action:
                  - 'lambda:InvokeFunction'
                Resource: !GetAtt MyLambdaFunction.Arn
```

若要移除兩張卡片之間的連線，請移除在 `ApiGatewayMethod:`下`MyLambdaFunction`列出的 參考`Integration`。

# 在 Infrastructure Composer 的視覺化畫布上排列卡片
<a name="reference-navigation-gestures-arrange"></a>

選取**排列**以視覺化方式排列和整理畫布上的卡片。當畫布上有許多卡片和連線時，使用**排列**按鈕特別有用。

![\[連接四張卡片，然後選取安排按鈕。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_ref_10.gif)


# 在 Infrastructure Composer 中設定和修改卡片
<a name="using-composer-cards"></a>

在 Infrastructure Composer 中，卡片代表您用來設計應用程式架構的資源。當您在 Infrastructure Composer 中設定卡片時，您可以定義應用程式中資源的詳細資訊。這包括卡片**邏輯 ID** 和**分割區索引鍵**等詳細資訊。此資訊的定義方式因**增強型元件卡**和**標準卡**而異。

**增強型元件卡**是 CloudFormation 資源的集合，已合併為單一精選的卡，可增強易用性、功能，並專為各種使用案例而設計。**標準 IaC 資源卡**代表單一 AWS CloudFormation 資源。每個標準 IaC 資源卡一旦拖曳到畫布上，都會標記為**標準元件**。

本主題提供有關設定**增強型元件卡**和**標準元件卡**的詳細資訊。

**注意**  
本主題適用於在 CloudFormation 主控台模式下使用來自 Infrastructure Composer 主控台、 AWS Toolkit for Visual Studio Code 延伸模組和 Infrastructure Composer 的卡片。Lambda 相關卡片 (**Lambda 函式**與 **Lambda Layer**) 需要程式碼建置及套件化解決方案，而這些功能在 CloudFormation 主控台模式的 Infrastructure Composer 中並不提供。如需詳細資訊，請參閱[在 CloudFormation 主控台模式中使用 Infrastructure Composer](using-composer-console-cfn-mode.md)。

**Topics**
+ [Infrastructure Composer 中的增強型元件卡](using-composer-cards-use-enhanced-component.md)
+ [Infrastructure Composer 中的標準卡](using-composer-standard-cards.md)

# Infrastructure Composer 中的增強型元件卡
<a name="using-composer-cards-use-enhanced-component"></a>

若要設定增強型元件卡，基礎設施編寫器會在**資源屬性**面板中提供表單。此表單是專為引導您設定每個增強型元件卡而設計。當您填寫表單時， Infrastructure Composer 會修改您的基礎設施程式碼。

有些增強型元件卡確實具有其他功能。本節會檢閱使用增強型元件卡的基本概念，並提供具有其他功能之卡片的詳細資訊。

如需增強型元件卡的詳細資訊，請參閱 [Infrastructure Composer 中的增強型元件卡](using-composer-cards-component-intro-enhanced.md) 和 [Infrastructure Composer 中的增強型元件卡](using-composer-cards-component-intro-enhanced.md)

# 程序


**資源屬性**面板可簡化組態，並新增可簡化卡片組態的指導方針。若要使用此面板，請執行下列步驟：

1. 按兩下卡片以顯示**資源屬性**面板。

1. 按一下卡片，然後選取**詳細資訊**以顯示資源屬性面板。

1. 針對 Infrastructure Composer AWS 管理主控台，選取 **範本**以顯示您的應用程式程式碼。從這裡直接設定 。

   下圖顯示如何完成此操作：  
![\[選取範本檢視並從該處修改基礎設施程式碼。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_ref_11.gif)

# 搭配 Amazon Relational Database Service (Amazon RDS) 使用 Infrastructure Composer
<a name="using-composer-services-rds"></a>

AWS Infrastructure Composer 具有與 Amazon Relational Database Service (Amazon RDS) 整合的功能。使用 Infrastructure Composer 中的 **RDS 資料庫 （外部）** 增強型元件卡，您可以將應用程式連線到另一個 CloudFormation 或 AWS Serverless Application Model (AWS SAM) 範本上定義的 Amazon RDS DB叢集、執行個體和代理。

**RDS 資料庫 （外部）** 增強型元件卡代表在另一個範本上定義的 Amazon RDS 資源。其中包含：
+ 在另一個範本上定義的 Amazon RDS DB叢集或執行個體
+ Amazon RDS DB代理

**RDS 資料庫 （外部）** 增強型元件卡可從**資源**面板取得。

![\[Amazon RDS 資料庫 （外部） 增強型元件卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_rds_01.png)


若要使用此卡，請將其拖曳到 Infrastructure Composer 畫布上、進行設定，並將其連接到其他資源。

您可以透過 Lambda 函數將應用程式連線至外部 Amazon RDS DB叢集或執行個體。

## 要求
<a name="using-composer-services-rds-requirements"></a>

若要使用此功能，您必須符合下列要求：

1. 您的外部 Amazon RDS DB叢集、執行個體或代理必須使用 AWS Secrets Manager 來管理使用者密碼。若要進一步了解，請參閱《[Amazon RDS 使用者指南》中的使用 Amazon RDS 和 進行密碼管理 AWS Secrets Manager](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-secrets-manager.html)。 **

1. 您在 Infrastructure Composer 中的應用程式必須是新專案，或最初必須在 Infrastructure Composer 中建立。

## 程序
<a name="using-composer-services-rds-connect"></a>

### 步驟 1：設定外部 RDS 資料庫卡
<a name="using-composer-services-rds-connect-step1"></a>

從**資源**面板中，將 **RDS 資料庫 （外部）** 增強型元件卡拖曳到畫布上。

選取卡片，然後選擇**詳細資訊**，或按兩下卡片以顯示**資源屬性**面板。卡片的資源屬性面板將會出現：

![\[RDS 資料庫 （外部） 增強元件卡的資源屬性面板。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_rds_03.png)


您可以在這裡設定下列項目：
+ **邏輯 ID** – 外部 Amazon RDS DB叢集、執行個體或代理的唯一名稱。此 ID 不必符合您外部 Amazon RDS DB 資源的邏輯 ID 值。
+ **資料庫秘密** – 與您的 Amazon RDS DB叢集、執行個體或代理相關聯的 AWS Secrets Manager 秘密識別符。此欄位接受下列值：
  + **靜態值** – 資料庫秘密的唯一識別符，例如秘密 ARN。以下是範例：`arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c`如需詳細資訊，請參閱《AWS Secrets Manager 使用者指南》**中的 [AWS Secrets Manager 概念](https://docs.aws.amazon.com/secretsmanager/latest/userguide/getting-started.html)。
  + **輸出值** – 部署 Secrets Manager 秘密時 AWS CloudFormation，會建立輸出值。您可以使用`[Fn::ImportValue](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html)`內部 函數在此處指定輸出值。例如 `!ImportValue MySecret`。
  + **來自 SSM 參數存放區的值** – 您可以將秘密存放在 SSM 參數存放區，並使用動態參考指定其值。例如 `{{resolve:ssm:MySecret}}`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的 [SSM 參數](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-ssm)。
+ **資料庫主機名稱** – 可用來連線至 Amazon RDS DB叢集、執行個體或代理的主機名稱。此值是在定義 Amazon RDS 資源的外部範本中指定。接受下列值：
  + **靜態值** – 資料庫主機名稱的唯一識別符，例如端點地址。以下是範例：`mystack-mydb-1apw1j4phylrk.cg034hpkmmjt.us-east-2.rds.amazonaws.com`
  + **輸出值** – 部署的 Amazon RDS DB叢集、執行個體或代理的輸出值。您可以使用`[Fn::ImportValue](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html)`內部 函數指定輸出值。例如 `!ImportValue myStack-myDatabase-abcd1234`。
  + **來自 SSM 參數存放區的值** – 您可以將資料庫主機名稱存放在 SSM 參數存放區中，並使用動態參考指定其值。例如 `{{resolve:ssm:MyDatabase}}`。
+ **資料庫連接埠** – 可用來連線至 Amazon RDS DB叢集、執行個體或代理的連接埠號碼。此值是在定義 Amazon RDS 資源的外部範本中指定。接受下列值：
  + **靜態值** – 資料庫連接埠。例如 `3306`。
  + **輸出值** – 部署的 Amazon RDS DB叢集、執行個體或代理的輸出值。例如 `!ImportValue myStack-MyRDSInstancePort`。
  + **來自 SSM 參數存放區的值** – 您可以將資料庫主機名稱存放在 SSM 參數存放區，並使用動態參考指定其值。例如 `{{resolve:ssm:MyRDSInstancePort}}`。

**注意**  
這裡只能設定邏輯 ID 值。您可以視需要在部署時間設定其他屬性。

### 步驟 2：連接 Lambda 函數卡
<a name="using-composer-services-rds-connect-step2"></a>

從**資源**面板中，將 **Lambda 函數**增強型元件卡拖曳到畫布上。

將 **Lambda 函數**卡的左側連接埠連接到 **RDS 資料庫 （外部）** 卡的右側連接埠。

![\[連接至 RDS 資料庫 （外部） 增強型元件卡的 Lambda 函數卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_rds_02.png)


Infrastructure Composer 將佈建您的範本，以促進此連線。

## Infrastructure Composer 如何建立您的連線
<a name="using-composer-services-rds-ref-how"></a>

當您完成上述程序時， Infrastructure Composer 會執行特定動作，將 Lambda 函數連線至資料庫。

### 指定外部 Amazon RDS DB叢集、執行個體或代理時
<a name="using-composer-services-rds-ref-how-specify"></a>

當您將 **RDS 資料庫 （外部）** 卡拖曳至畫布時， Infrastructure Composer 會視需要更新範本的 `Metadata`和 `Parameters`區段。以下是範例：

```
Metadata:
  AWS::Composer::ExternalResources:
    ExternalRDS:
      Type: externalRDS
      Settings:
        Port: !Ref ExternalRDSPort
        Hostname: !Ref ExternalRDSHostname
        SecretArn: !Ref ExternalRDSSecretArn
Parameters:
  ExternalRDSPort:
    Type: Number
  ExternalRDSHostname:
    Type: String
  ExternalRDSSecretArn:
    Type: String
```

[中繼資料](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)是 CloudFormation 範本區段，用於存放範本的詳細資訊。Infrastructure Composer 特有的中繼資料存放在`AWS::Composer::ExternalResources`中繼資料金鑰下。在這裡， Infrastructure Composer 會儲存您為 Amazon RDS DB叢集、執行個體或代理指定的值。

 CloudFormation 範本的[參數](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)區段用於存放可在部署時插入整個範本的自訂值。根據您提供的值類型， Infrastructure Composer 可能會將 Amazon RDS DB叢集、執行個體或代理的值存放在此處，並在範本中指定這些值。

`Metadata` 和 `Parameters`區段中的字串值使用您在 **RDS 資料庫 （外部）** 卡上指定的邏輯 ID 值。如果您更新邏輯 ID，字串值將會變更。

### 將 Lambda 函數連線至資料庫時
<a name="using-composer-services-rds-ref-how-connecting"></a>

當您將 **Lambda 函數**卡連接至 **RDS 資料庫 （外部）** 卡時， Infrastructure Composer 會佈建環境變數和 AWS Identity and Access Management (IAM) 政策。以下是範例：

```
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Environment:
        Variables:
          EXTERNALRDS_PORT: !Ref ExternalRDSPort
          EXTERNALRDS_HOSTNAME: !Ref ExternalRDSHostname
          EXTERNALRDS_SECRETARN: !Ref ExternalRDSSecretArn
      Policies:
        - AWSSecretsManagerGetSecretValuePolicy:
            SecretArn: !Ref ExternalRDSSecretArn
```

[環境](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-environment)變數是可在執行時間由函數使用的變數。若要進一步了解，請參閱《 *AWS Lambda 開發人員指南*》中的[使用 Lambda 環境變數](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html)。

[政策](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-policies)佈建函數的許可。在這裡， Infrastructure Composer 會建立政策，以允許從函數對 Secrets Manager 進行讀取存取，以取得存取 Amazon RDS DB叢集、執行個體或代理的密碼。

# AWS Infrastructure Composer 搭配 使用 AWS Step Functions
<a name="using-composer-services-sf"></a>

AWS Infrastructure Composer 具有與 整合的功能[AWS Step Functions Workflow Studio](https://docs.aws.amazon.com/step-functions/latest/dg/workflow-studio.html)。使用 Infrastructure Composer 執行下列動作：
+ Workflow Studio 直接在 Infrastructure Composer 中啟動 Step Functions。
+ 建立和管理新的工作流程，或將現有的工作流程匯入 Infrastructure Composer。
+ 使用 Infrastructure Composer 畫布將您的工作流程與其他 AWS 資源整合。

下圖是 Step Functions 狀態機器卡

![\[Step Functions 狀態機器卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_01.png)


使用 Infrastructure Composer Workflow Studio中的 Step Functions，您可以在單一位置使用兩個強大的視覺化設計工具的優點。當您設計工作流程和應用程式時， Infrastructure Composer 會建立基礎設施做為程式碼 (IaC)，以引導您進行部署。

**Topics**
+ [IAM 政策](#using-composer-services-sf-use-iam)
+ [Infrastructure Composer Workflow Studio中的 Step Functions 入門](#using-composer-services-sf-gs)
+ [在 Infrastructure Composer Workflow Studio中使用 Step Functions](#using-composer-services-sf-use)
+ [進一步了解](#using-composer-services-sf-learn)

## IAM 政策
<a name="using-composer-services-sf-use-iam"></a>

當您將任務從工作流程連線至 資源時， Infrastructure Composer 會自動建立所需的 AWS Identity and Access Management (IAM) 政策，以授權資源之間的互動。以下是範例：

```
Transform: AWS::Serverless-2016-10-31
Resources:
  StockTradingStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      ...
      Policies:
        - LambdaInvokePolicy:
            FunctionName: !Ref CheckStockValue
      ...
  CheckStockValue:
    Type: AWS::Serverless::Function
    ...
```

如有必要，您可以將更多 IAM 政策新增至範本。

## Infrastructure Composer Workflow Studio中的 Step Functions 入門
<a name="using-composer-services-sf-gs"></a>

若要開始使用，您可以建立新的工作流程或匯入現有的工作流程。

### 建立新的工作流程
<a name="using-composer-services-sf-gs-create"></a>

1. 從**資源**面板中，將 **Step Functions State Machine** 增強型元件卡拖曳到畫布上。  
![\[Step Functions 狀態機器卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_02.png)

   當您將 **Step Functions State 機器**卡拖曳至畫布時， Infrastructure Composer 會建立下列項目：
   + 定義狀態機器`[ AWS::Serverless::StateMachine](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html)`的資源。根據預設， Infrastructure Composer 會建立標準工作流程。若要建立快速工作流程，請將範本中的`Type`值從 變更為 `STANDARD` `EXPRESS`。
   + 為您的狀態機器定義 Amazon CloudWatch 日誌群組`[AWS::Logs::LogGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html)`的資源。

1. 開啟卡片**的資源屬性**面板，然後選取 **Workflow Studio 中的編輯**，以在 Infrastructure Composer Workflow Studio中開啟。

   Step Functions 會在**設計**模式中Workflow Studio開啟。若要進一步了解，請參閱《 *AWS Step Functions 開發人員指南*》中的[設計模式](https://docs.aws.amazon.com/step-functions/latest/dg/workflow-studio-components.html#wfs-interface-design-mode)。
**注意**  
您可以修改 Infrastructure Composer，將狀態機器定義儲存在外部檔案中。如需詳細資訊，請參閱 [使用外部檔案](#using-composer-services-sf-use-external)。

1. 建立您的工作流程，然後選擇**儲存**。若要結束 Workflow Studio，請選擇**返回基礎設施編寫器**。

   Infrastructure Composer 會使用 `AWS::Serverless::StateMachine` 資源的 `Defintion` 屬性來定義您的工作流程。

1. 您可以執行下列任何動作來修改工作流程：
   + Workflow Studio 再次開啟 並修改您的工作流程。
   + 對於從主控台的 Infrastructure Composer，您可以開啟應用程式的**範本**檢視，並修改您的範本。如果使用**本機同步**，您可以在本機 IDE 中修改工作流程。Infrastructure Composer 會偵測您的變更，並在 Infrastructure Composer 中更新您的工作流程。
   + 對於 Toolkit for VS Code 中的 Infrastructure Composer，您可以直接修改範本。Infrastructure Composer 會偵測您的變更，並在 Infrastructure Composer 中更新您的工作流程。

### 匯入現有的工作流程
<a name="using-composer-services-sf-gs-import"></a>

您可以從使用 AWS Serverless Application Model (AWS SAM) 範本定義的應用程式匯入工作流程。使用任何以 `AWS::Serverless::StateMachine` 資源類型定義的狀態機器，它將視覺化為 **Step Functions 狀態機器**增強型元件卡，您可以用來啟動 Workflow Studio。

`AWS::Serverless::StateMachine` 資源可以使用下列任一屬性定義工作流程：
+ `[ Definition](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definition)` – 工作流程在 AWS SAM 範本中定義為 物件。
+ `[ DefinitionUri](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionuri)` – 使用 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) 在外部檔案上定義工作流程。然後使用此屬性指定檔案的本機路徑。

#### 定義屬性
<a name="using-composer-services-sf-gs-import-definition"></a>

**主控台中的 Infrastructure Composer**  
對於使用 `Definition` 屬性定義的工作流程，您可以匯入單一範本或整個專案。  
+ **範本** – 如需匯入範本的指示，請參閱 [在 Infrastructure Composer 主控台中匯入現有的專案範本](using-composer-project-import-template.md)。若要儲存您在 Infrastructure Composer 中所做的變更，您必須匯出範本。
+ **專案** – 當您匯入專案時，您必須啟用**本機同步**。您所做的變更會自動儲存至本機電腦。如需匯入專案的指示，請參閱 [在 Infrastructure Composer 主控台中匯入現有的專案資料夾](using-composer-project-import-folder.md)。

**來自 Toolkit for VS Code 的 Infrastructure Composer**  
對於使用 `Definition` 屬性定義的工作流程，您可以從範本開啟 Infrastructure Composer。如需說明，請參閱[從 存取 Infrastructure Composer AWS Toolkit for Visual Studio Code](setting-up-composer-access-ide.md)。

#### DefinitionUri 屬性
<a name="using-composer-services-sf-gs-import-definitionuri"></a>

**主控台中的 Infrastructure Composer**  
對於使用 `DefinitionUri` 屬性定義的工作流程，您必須匯入專案並啟用**本機同步**。如需匯入專案的指示，請參閱 [在 Infrastructure Composer 主控台中匯入現有的專案資料夾](using-composer-project-import-folder.md)。

**來自 Toolkit for VS Code 的 Infrastructure Composer**  
對於使用 `DefinitionUri` 屬性定義的工作流程，您可以從範本開啟 Infrastructure Composer。如需說明，請參閱[從 存取 Infrastructure Composer AWS Toolkit for Visual Studio Code](setting-up-composer-access-ide.md)。

## 在 Infrastructure Composer Workflow Studio中使用 Step Functions
<a name="using-composer-services-sf-use"></a>

### 建置工作流程
<a name="using-composer-services-sf-use-build"></a>

Infrastructure Composer 使用定義替換，將工作流程任務映射到應用程式中的資源。若要進一步了解定義替換，請參閱《 *AWS Serverless Application Model 開發人員指南*`[ DefinitionSubstitutions](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionsubstitutions)`》中的 。

當您在 中建立任務時Workflow Studio，請為每個任務指定定義替換。然後，您可以將任務連接到 Infrastructure Composer 畫布上的資源。

**在 中指定定義替換 Workflow Studio**

1. 開啟任務的**組態**索引標籤，並找到 **API 參數**欄位。  
![\[中任務的組態索引標籤Workflow Studio。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_05.png)

1. 如果 **API 參數**欄位有下拉式選項，請選擇**輸入 CloudFormation 替代**。然後，提供唯一的名稱。

   對於連接到相同資源的任務，請為每個任務指定相同的定義替換。若要使用現有的定義替換，請選擇**選取 CloudFormation 替換**，然後選取要使用的替換。

1. 如果 **API 參數**欄位包含 JSON 物件，請修改指定資源名稱的項目，以使用定義替換。在下列範例中，我們將 `"MyDynamoDBTable"`變更為 `"${RecordTransaction}"`。  
![\[中任務的組態索引標籤Workflow Studio。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_06.png)

1. 選取**儲存**並**返回基礎設施編寫器**。

工作流程中的任務會在 **Step Functions 狀態機器**卡上視覺化。

![\[Step Functions 狀態機器卡，可將任務視覺化。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_03.png)


### 將資源連接至工作流程任務
<a name="using-composer-services-sf-use-connect"></a>

您可以在 Infrastructure Composer 中，在支援的工作流程任務和支援的 Infrastructure Composer 卡之間建立連線。
+ **支援的工作流程任務** – AWS 服務 針對 Step Functions 最佳化的 任務。若要進一步了解，請參閱《 *AWS Step Functions 開發人員指南*》中的 [ Step Functions 的最佳化整合](https://docs.aws.amazon.com/step-functions/latest/dg/connect-supported-services.html)。
+ **支援的 Infrastructure Composer 卡** – 支援增強型元件卡。若要進一步了解 Infrastructure Composer 中的卡片，請參閱 [在 Infrastructure Composer 中設定和修改卡片](using-composer-cards.md)。

建立連線時，任務和卡片 AWS 服務 的 必須相符。例如，您可以將叫用 Lambda 函數的工作流程任務連接到 **Lambda 函數**增強型元件卡。

若要建立連線，請按一下任務的連接埠，並將其拖曳至增強型元件卡的左側連接埠。

![\[Step Functions 狀態機器卡，具有連接至 Lambda 函數資源卡的任務。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_sf_04.png)


Infrastructure Composer 會自動更新您的`DefinitionSubstitution`值，以定義您的連線。以下是範例：

```
Transform: AWS::Serverless-2016-10-31
Resources:
  StateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Definition:
        StartAt: Check Stock Value
        States:
          Check Stock Value:
            Type: Task
            Resource: arn:aws:states:::lambda:invoke
            Parameters:
              Payload.$: $
              FunctionName: ${CheckStockValue}
            Next: Choice
          ...
      DefinitionSubstitutions:
        CheckStockValue: !GetAtt CheckStockValue.Arn
        ...
  CheckStockValue:
    Type: AWS::Serverless::Function
    Properties:
      ...
```

### 使用外部檔案
<a name="using-composer-services-sf-use-external"></a>

當您從 **Step Functions 狀態機器**卡建立工作流程時， Infrastructure Composer 會使用 `Definition` 屬性將狀態機器定義儲存在範本中。您可以設定 Infrastructure Composer，將狀態機器定義儲存至外部檔案。

**注意**  
若要從 搭配 Infrastructure Composer 使用此功能 AWS 管理主控台，您必須啟用**本機同步**。如需詳細資訊，請參閱[在 Infrastructure Composer 主控台中本機同步和儲存您的專案](using-composer-project-local-sync.md)。

**在外部檔案上儲存您的狀態機器定義**

1. 開啟 **Step Functions 狀態機器**卡**的資源屬性**面板。

1. 選取**使用外部檔案做為狀態機器定義**選項。

1. 為您的狀態機器定義檔案提供相對路徑和名稱。

1. 選擇**儲存**。

Infrastructure Composer 將執行下列動作：

1. 將您的狀態機器定義從 `Definition` 欄位移至外部檔案。

1. 使用 Amazon States Language 將狀態機器定義儲存在外部檔案中。

1. 使用 `DefinitionUri` 欄位修改您的範本以參考外部檔案。

## 進一步了解
<a name="using-composer-services-sf-learn"></a>

若要進一步了解 Infrastructure Composer 中的 Step Functions，請參閱以下內容：
+ 《 *AWS Step Functions 開發人員指南*》[Workflow Studio中的在 Infrastructure Composer](https://docs.aws.amazon.com/step-functions/latest/dg/use-wfs-in-app-composer.html) 中使用 。
+ 《 *AWS Step Functions 開發人員指南*[》中的 AWS SAM 範本中的 DefinitionSubstitutions](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-sam-sfn.html#sam-definition-substitution-eg)。

# Infrastructure Composer 中的標準卡
<a name="using-composer-standard-cards"></a>

所有 CloudFormation 資源都可以用作**資源面板中的標準 IaC 資源卡**。 ****拖曳到視覺化畫布後，**標準 IaC 資源卡**會變成**標準元件卡**。這只是表示卡片是一或多個標準 IaC 資源。如需進一步的範例和詳細資訊，請參閱本節中的主題。

您可以透過**範本**檢視和**資源屬性**視窗修改基礎設施程式碼。例如，以下是`Alexa::ASK::Skill`標準 IaC 資源的啟動範本範例：

```
Resources:
  Skill:
    Type: Alexa::ASK::Skill
    Properties:
      AuthenticationConfiguration:
        RefreshToken: <String>
        ClientSecret: <String>
        ClientId: <String>
      VendorId: <String>
      SkillPackage:
        S3Bucket: <String>
        S3Key: <String>
```

標準 IaC 資源卡啟動範本包含下列項目：
+  CloudFormation 資源類型。
+ 必要或常用屬性。
+ 為每個屬性提供的所需 值類型。

**注意**  
您可以使用 Amazon Q 來產生標準資源卡的基礎設施程式碼建議。如需詳細資訊，請參閱 [AWS Infrastructure Composer 搭配 使用 Amazon Q Developer](using-composer-ide-cw.md)。

## 程序
<a name="using-composer-cards-use-standard-component"></a>

您可以透過資源**屬性**面板修改標準元件卡中每個資源的基礎設施代碼。

**修改標準元件卡**

1. 開啟標準 IaC 元件卡**的資源屬性**面板。

1. 在**編輯**欄位中，從下拉式清單選取要編輯的標準 IaC 資源。

1. 修改您的基礎設施程式碼並**儲存**。

# 在 Infrastructure Composer 中刪除卡片
<a name="using-composer-cards-delete"></a>

本節提供在 中刪除卡片的指示 AWS Infrastructure Composer。

## 增強型元件卡
<a name="using-composer-cards-delete-enhanced-card"></a>

若要刪除增強型元件卡，請選取您已放置在視覺化畫布上的卡片。從**卡片動作**功能表中，選取**刪除**。

![\[顯示增強型元件卡及其卡片動作選單，並顯示刪除選項。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac-enhanced-delete.png)


## 標準元件卡
<a name="using-composer-cards-use-standard-resource-delete"></a>

若要刪除標準元件卡，您必須手動從範本中移除每個 CloudFormation 資源的基礎設施程式碼。以下是完成此操作的簡單方法：

1. 請記下要刪除之資源的邏輯 ID。

1. 在您的範本上，透過 `Resources`或 `Outputs`區段中的邏輯 ID 來尋找資源。

1. 從您的範本刪除資源。這包括資源邏輯 ID 及其巢狀值，例如 `Type`和 `Properties`。

1. 檢查 **Canvas** 檢視，確認資源已從您的畫布中移除。

# 使用 Infrastructure Composer 中的 Change Inspector 檢視程式碼更新
<a name="using-change-inspector"></a>

當您在 Infrastructure Composer 主控台中設計時，系統會自動建立您的基礎設施程式碼。使用 **Change Inspector** 來檢視範本程式碼更新，並了解 Infrastructure Composer 正在為您建立哪些項目。

本主題涵蓋從 AWS 管理主控台 或 AWS Toolkit for Visual Studio Code 延伸模組使用 Infrastructure Composer。

**Change Inspector** 是 Infrastructure Composer 中的視覺化工具，可顯示最近的程式碼更新。
+ 當您設計應用程式時，訊息會顯示在視覺化畫布底部。這些訊息會針對您正在執行的動作提供註解。
+ 支援時，您可以展開訊息來檢視 **Change Inspector**。
+ **Change Inspector** 會顯示來自您最近一次互動的程式碼變更。

下列範例示範變更檢測器的運作方式：

![\[將兩張卡片拖曳到視覺化畫布上，連接它們，然後展開產生的訊息以開啟變更檢查器。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ci_01.gif)


## 變更檢查器的優點
<a name="using-change-inspector-benefits"></a>

**Change Inspector** 是檢視 Infrastructure Composer 為您建立範本程式碼的絕佳方式。這也是學習如何撰寫基礎設施程式碼的好方法。當您在 Infrastructure Composer 中設計應用程式時，請在 **Change Inspector** 中檢視程式碼更新，以了解佈建設計所需的程式碼。

## 程序
<a name="using-change-inspector-how"></a>

**使用變更檢查器**

1. 展開訊息以叫用**變更檢查器**。  
![\[從畫面底部開啟 Change Inspector。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ci_01.gif)

1. 檢視為您自動編寫的程式碼。  
![\[顯示由 Infrastructure Composer 自動更新的基礎設施程式碼。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ci_03.png)

   1. 反白為**綠色**的程式碼表示新增的程式碼。

   1. 反白**紅色**的程式碼表示新移除的程式碼。

   1. **行號**表示範本中的位置。

1. 更新範本的多個區段時，**Change Inspector** 會組織這些區段。選取**上**一個和**下一個**按鈕以檢視所有變更。  
![\[選取下一步以檢視多個變更。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_ci_04.gif)

**注意**  
對於從主控台的 Infrastructure Composer，您可以使用範本檢視，在整個範本的內容中**檢視**程式碼變更。您也可以將 Infrastructure Composer 與本機 IDE 同步，並在本機電腦上檢視整個範本。如需詳細資訊，請參閱 [將 Infrastructure Composer 主控台與本機 IDE 連接](other-services-ide.md)。

## 進一步了解
<a name="using-change-inspector-learn"></a>

如需 Infrastructure Composer 所建立程式碼的詳細資訊，請參閱下列內容：
+ [Infrastructure Composer 中的卡片連線](using-composer-connecting.md).

# 參考 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)


# 將 Infrastructure Composer 與 Amazon Virtual Private Cloud (Amazon VPC) 整合
<a name="using-composer-services-vpc"></a>

AWS Infrastructure Composer 具有與 Amazon Virtual Private Cloud (Amazon VPC) 服務的整合。使用 Infrastructure Composer，您可以執行下列動作：
+ 透過視覺化 VPC 標籤，識別畫布上 **VPC **中的資源。
+ 從外部範本使用 VPCs 設定 AWS Lambda 函數。

下圖顯示使用 VPC 設定 Lambda 函數的應用程式範例。

![\[具有 VPC 標籤的應用程式，視覺化使用 VPC 設定的 Infrastructure Composer 中的 Lambda 函數。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_06.png)


若要進一步了解 Amazon VPC，請參閱《[Amazon VPC 使用者指南》中的什麼是](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) *Amazon VPC*？。

**Topics**
+ [識別 VPC 中的 Infrastructure Composer 資源和相關資訊](using-composer-services-vpc-tag.md)
+ [在 Infrastructure Composer 中使用外部 VPCs 設定 Lambda 函數](using-composer-services-vpc-configure.md)
+ [具有 Infrastructure Composer 之外部 VPC 的匯入範本中的參數](using-composer-services-vpc-import.md)
+ [使用 Infrastructure Composer 將新參數新增至匯入的範本](using-composer-services-vpc-import-add.md)
+ [使用 Infrastructure Composer 設定 Lambda 函數和另一個範本中定義的 VPC](using-composer-services-vpc-examples.md)

# 識別 VPC 中的 Infrastructure Composer 資源和相關資訊
<a name="using-composer-services-vpc-tag"></a>

若要將 Infrastructure Composer 與 Amazon VPC 整合，您必須先識別 VPC 中的資源，以及完成整合所需的資訊。這也包括與安全群組、子網路識別符、參數類型、SSM 類型、靜態值類型相關的組態資訊。

Infrastructure Composer 使用 VPC 標籤視覺化 **VPC** 中的資源。此標籤會套用至畫布上的卡片。以下是具有 VPC 標籤的 Lambda 函數範例：

![\[在使用 VPC 設定的 Infrastructure Composer 中視覺化 Lambda 函數的 VPC 標籤。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_01.png)


當您執行下列動作時，VPC 標籤會套用至畫布上的卡片：
+ 在 Infrastructure Composer 中使用 VPC 設定 Lambda 函數。
+ 匯入範本，其中包含使用 VPC 設定的資源。

## 安全群組和子網路識別符
<a name="using-composer-services-vpc-configure-ids"></a>

Lambda 函數可以設定多個安全群組和子網路。若要設定 Lambda 函數的安全群組或子網路，請提供值和類型。
+ **值** – 安全群組或子網路的識別符。接受的值會根據**類型**而有所不同。
+ **類型** – 允許下列類型的值：
  + 參數名稱
  + AWS Systems Manager (SSM) 參數存放區
  + 靜態值

## 參數類型
<a name="using-composer-services-vpc-configure-parameter"></a>

 AWS CloudFormation 範本的 `Parameters`區段可用來跨多個範本存放資源資訊。如需參數的詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[參數](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)。

對於 **參數**類型，您可以提供參數名稱。在下列範例中，我們提供`PrivateSubnet1`參數名稱值：

![\[為子網路 ID 欄位的參數類型提供的 PrivateSubnet1 值。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_07.png)


當您提供參數名稱時， Infrastructure Composer 會在範本的 `Parameters`區段中定義它。然後， Infrastructure Composer 會參考 Lambda 函數資源中的 參數。以下是範例：

```
...
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SubnetIds:
          - !Ref PrivateSubnet1
Parameters:
  PrivateSubnet1:
    Type: AWS::EC2::Subnet::Id
    Description: Parameter is generated by Infrastructure Composer
```

## SSM 類型
<a name="using-composer-services-vpc-configure-ssm"></a>

SSM 參數存放區為組態資料管理和秘密管理提供安全的階層式儲存。若要了解詳細資訊，請參閱 *AWS Systems Manager 使用者指南*中的 [AWS Systems Manager 參數存放區](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)。

對於 **SSM** 類型，您可以提供下列值：
+ 來自 SSM 參數存放區的 值動態參考。
+ 範本中定義之`AWS::SSM::Parameter`資源的邏輯 ID。

### 動態參考
<a name="using-composer-services-vpc-configure-ssm-dynamic"></a>

您可以使用動態參考，以下列格式參考 SSM 參數存放區中的值：`{{resolve:ssm:reference-key}}`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的 [SSM 參數](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-ssm)。

Infrastructure Composer 會建立基礎設施程式碼，以使用來自 SSM 參數存放區的 值來設定 Lambda 函數。以下是範例：

```
...
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds:
          - '{{resolve:ssm:demo-app/sg-0b61d5c742dc2c773}}'
  ...
```

### 邏輯 ID
<a name="using-composer-services-vpc-configure-ssm-logical"></a>

您可以透過邏輯 ID 參考相同範本中的 `AWS::SSM::Parameter` 資源。

以下是名為 `AWS::SSM::Parameter`的資源範例`PrivateSubnet1Parameter`，存放 的子網路 ID`PrivateSubnet1`：

```
...
Resources:
  PrivateSubnet1Parameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: /MyApp/VPC/SubnetIds
      Description: Subnet ID for PrivateSubnet1
      Type: String
      Value: subnet-04df123445678a036
```

以下是由 Lambda 函數的邏輯 ID 提供此資源值的範例：

![\[為子網路 ID 欄位的 SSM 類型提供的 PrivateSubnet1Parameter 值。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_08.png)


Infrastructure Composer 會建立基礎設施程式碼，以使用 SSM 參數設定 Lambda 函數：

```
...
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SubnetIds:
          - !Ref PrivateSubnet1Parameter
  ...
  PrivateSubnet1Parameter:
    Type: AWS::SSM::Parameter
    Properties:
      ...
```

## 靜態值類型
<a name="using-composer-services-vpc-configure-static"></a>

部署安全群組或子網路時 CloudFormation，會建立 ID 值。您可以將此 ID 做為靜態值提供。

對於**靜態值**類型，下列是有效值：
+ 對於安全群組，請提供 `GroupId`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[傳回值](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html#aws-properties-ec2-security-group-return-values)。以下是範例：`sg-0b61d5c742dc2c773`
+ 對於子網路，請提供 `SubnetId`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[傳回值](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html#aws-resource-ec2-subnet-return-values)。以下是範例：`subnet-01234567890abcdef`

Infrastructure Composer 會建立基礎設施程式碼，以使用靜態值設定 Lambda 函數。以下是範例：

```
...
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds:
          - subnet-01234567890abcdef
        SubnetIds:
          - sg-0b61d5c742dc2c773
  ...
```

## 使用多種類型
<a name="using-composer-services-vpc-configure-multiple"></a>

對於安全群組和子網路，您可以同時使用多種類型。以下是透過提供不同類型值，為 Lambda 函數設定三個安全群組的範例：

![\[三種不同的值類型，用於為 Lambda 函數的安全群組 ID 欄位提供識別符。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_09.png)


Infrastructure Composer 參考 `SecurityGroupIds` 屬性下的所有三個值：

```
...
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds:
          - !Ref MySecurityGroup
          - sg-0b61d5c742dc2c773
          - '{{resolve::ssm::demo/sg-0b61d5c742dc23}}'
      ...
Parameters:
  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup::Id
    Description: Parameter is generated by Infrastructure Composer
```

# 在 Infrastructure Composer 中使用外部 VPCs 設定 Lambda 函數
<a name="using-composer-services-vpc-configure"></a>

若要使用另一個範本上定義的 VPC 開始設定 Lambda 函數，請使用 **Lambda 函數**增強型元件卡。此卡代表使用 AWS Serverless Application Model (AWS SAM) `AWS::Serverless::Function` 資源類型的 Lambda 函數。

**從外部範本使用 VPC 設定 Lambda 函數**

1. 從 **Lambda 函數**資源屬性面板中，展開 **VPC 設定 （進階）** 下拉式清單區段。

1. 選取**指派給外部 VPC**。

1. 提供要為 Lambda 函數設定的安全群組和子網路值。如需詳細資訊，請參閱 [安全群組和子網路識別符](using-composer-services-vpc-tag.md#using-composer-services-vpc-configure-ids)。

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

# 具有 Infrastructure Composer 之外部 VPC 的匯入範本中的參數
<a name="using-composer-services-vpc-import"></a>

當您使用為外部 VPC 的安全群組和子網路定義的參數匯入現有範本時， Infrastructure Composer 會提供下拉式清單來選取參數。

以下是匯入範本的 `Parameters`區段範例：

```
...
Parameters:
  VPCSecurityGroups:
    Description: Security group IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::SecurityGroup::Id>
  VPCSubnets:
    Description: Subnet IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::Subnet::Id>
  VPCSubnet:
    Description: Subnet Id generated by Infrastructure Composer
    Type: AWS::EC2::Subnet::Id
...
```

在畫布上為新的 Lambda 函數設定外部 VPC 時，這些參數可從下拉式清單中取得。以下是範例：

![\[Lambda 函數卡子網路 ID 欄位參數類型所提供的值下拉式清單。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_10.png)


## 匯入清單參數類型的限制
<a name="using-composer-services-vpc-import-list"></a>

一般而言，您可以為每個 Lambda 函數指定多個安全群組和子網路識別符。如果您現有的範本包含清單參數類型，例如 `List<AWS::EC2::SecurityGroup::Id>`或 `List<AWS::EC2::Subnet::Id>`，您只能指定一個識別符。

如需參數清單類型的詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[支援 AWS的特定參數類型](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#aws-specific-parameter-types)。

以下是將 `VPCSecurityGroups`定義為清單參數類型的範本範例：

```
...
Parameters:
  VPCSecurityGroups:
    Description: Security group IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::SecurityGroup::Id>
...
```

在 Infrastructure Composer 中，如果您選取`VPCSecurityGroups`值做為 Lambda 函數的安全群組識別符，您將會看到下列訊息：

![\[針對 Lambda 函數卡之安全群組 ID 欄位的參數類型VPCSecurityGroups提供名為 的清單參數類型。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_11.png)


發生此限制是因為`AWS::Lambda::Function VpcConfig`物件的 `SecurityGroupIds`和 `SubnetIds` 屬性都只接受字串值的清單。由於單一清單參數類型包含字串清單，因此它可以是指定時提供的唯一物件。

對於清單參數類型，以下是使用 Lambda 函數設定時如何在範本中定義參數的範例：

```
...
Parameters:
  VPCSecurityGroups:
    Description: Security group IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::SecurityGroup::Id>
  VPCSubnets:
    Description: Subnet IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::Subnet::Id>
Resources:
  ...
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds: !Ref VPCSecurityGroups
        SubnetIds: !Ref VPCSubnets
```

# 使用 Infrastructure Composer 將新參數新增至匯入的範本
<a name="using-composer-services-vpc-import-add"></a>

當您匯入已定義參數的現有範本時，您也可以建立新的參數。提供新的類型和值，而不是從下拉式清單中選取現有的參數。以下是建立名為 之新參數的範例`MySecurityGroup`：

![\[Lambda 函數卡之安全群組 ID 欄位的參數類型所提供的MySecurityGroup值。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_12.png)


對於您在 Lambda 函數**的資源屬性**面板中提供的所有新值， Infrastructure Composer 會在 Lambda 函數的 `SecurityGroupIds`或 `SubnetIds` 屬性下的清單中定義這些值。以下是範例：

```
...
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds:
          - sg-94b3a1f6
        SubnetIds:
          - !Ref SubnetParameter
          - !Ref VPCSubnet
```

如果您想要參考外部範本中清單參數類型的邏輯 ID，建議您使用**範本**檢視並直接修改範本。清單參數類型的邏輯 ID 應一律以單一值提供，且是唯一的值。

```
...
Parameters:
  VPCSecurityGroups:
    Description: Security group IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::SecurityGroup::Id>
  VPCSubnets:
    Description: Subnet IDs generated by Infrastructure Composer
    Type: List<AWS::EC2::Subnet::Id>
Resources:
  ...
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      VpcConfig:
        SecurityGroupIds: !Ref VPCSecurityGroups # Valid syntax
        SubnetIds: 
          - !Ref VPCSubnets # Not valid syntax
```

# 使用 Infrastructure Composer 設定 Lambda 函數和另一個範本中定義的 VPC
<a name="using-composer-services-vpc-examples"></a>

在此範例中，我們使用另一個範本上定義的 VPC，在 Infrastructure Composer 中設定 Lambda 函數。

首先，將 **Lambda 函數**增強型元件卡拖曳到畫布上。

![\[Lambda 函數增強型元件卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_03.png)


接著，我們開啟卡片**的資源屬性**面板，並展開 **VPC 設定 （進階）** 下拉式清單區段。

![\[Lambda 函數卡的資源屬性面板影像，其中已展開 VPC 設定 （進階） 下拉式清單區段\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_04.png)


接著，選取**指派給外部 VPC**，從外部範本開始設定 VPC。

在此範例中，我們會參考安全群組 ID 和子網路 ID。這些值會在部署定義 VPC 的範本時建立。我們選擇**靜態值**類型，並輸入 IDs的值。完成後，我們會選取**儲存**。

![\[Lambda 函數卡的資源屬性面板，其靜態值會在安全群組 ID 欄位中提供。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_05.png)


現在我們的 Lambda 函數已使用 VPC 設定，VPC 標籤會顯示在我們的卡片上。

![\[在使用 VPC 設定的 Infrastructure Composer 中，VPC 標籤視覺化 Lambda 函數。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_use_vpc_01.png)


Infrastructure Composer 已建立基礎設施程式碼，以使用外部 VPC 的安全群組和子網路來設定 Lambda 函數。

```
Transform: AWS::Serverless-2016-10-31
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      Description: !Sub
        - Stack ${AWS::StackName} Function ${ResourceName}
        - ResourceName: Function
      CodeUri: src/Function
      Handler: index.handler
      Runtime: nodejs18.x
      MemorySize: 3008
      Timeout: 30
      Tracing: Active
      VpcConfig:
        SecurityGroupIds:
          - sg-10f35d07e1be09e15
        SubnetIds:
          - subnet-0d80727ca90325716
  FunctionLogGroup:
    Type: AWS::Logs::LogGroup
    DeletionPolicy: Retain
    Properties:
      LogGroupName: !Sub /aws/lambda/${Function}
```