

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

# 將 AWS Transfer Family 伺服器端點類型從 VPC\$1ENDPOINT 更新為 VPC
<a name="update-endpoint-type-vpc"></a>

您可以使用 AWS 管理主控台 CloudFormation或 Transfer Family API，將伺服器的 `EndpointType` 從 更新`VPC_ENDPOINT`為 `VPC`。下列各節提供使用每種方法來更新伺服器端點類型的詳細程序和範例。如果您在多個 AWS 區域和多個帳戶中有伺服器 AWS ，您可以使用下一節提供的範例指令碼進行修改，以使用您需要更新的`VPC_ENDPOINT`類型來識別伺服器。

**Topics**
+ [使用`VPC_ENDPOINT`端點類型識別伺服器](#id-servers)
+ [使用 更新伺服器端點類型 AWS 管理主控台](#update-endpoint-console)
+ [使用 更新伺服器端點類型 CloudFormation](#update-endpoint-cloudformation)
+ [使用 API 更新伺服器 EndpointType](#update-endpoint-cli)

## 使用`VPC_ENDPOINT`端點類型識別伺服器
<a name="id-servers"></a>

您可以使用 來識別哪些伺服器正在使用 `VPC_ENDPOINT` AWS 管理主控台。

**使用主控台使用`VPC_ENDPOINT`端點類型識別伺服器**

1. 在 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/) 開啟 AWS Transfer Family 主控台。

1. 在導覽窗格中選擇**伺服器**，以顯示您帳戶中該區域的伺服器清單。

1. 依**端點類型**排序伺服器清單，以查看使用 的所有伺服器`VPC_ENDPOINT`。

**`VPC_ENDPOINT` 跨多個 AWS 區域和帳戶使用 識別伺服器**

如果您在多個 AWS 區域和多個帳戶中有伺服器 AWS ，您可以使用下列範例指令碼進行修改，以使用`VPC_ENDPOINT`端點類型識別伺服器。範例指令碼使用 Amazon EC2 [DescribeRegions](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRegions.html) 和 Transfer Family [https://docs.aws.amazon.com/transfer/latest/APIReference/API_ListServers.html](https://docs.aws.amazon.com/transfer/latest/APIReference/API_ListServers.html) API 操作。如果您有多個 AWS 帳戶，如果您使用工作階段設定檔向身分提供者進行身分驗證，則可以使用具有唯讀稽核程式存取權的 IAM 角色來循環通過您的帳戶。

1. 以下是簡單的範例。

   ```
   import boto3
   
   profile = input("Enter the name of the AWS account you'll be working in: ")
   session = boto3.Session(profile_name=profile)
   
   ec2 = session.client("ec2")
   
   regions = ec2.describe_regions()
   
   for region in regions['Regions']:
       region_name = region['RegionName']
       if region_name=='ap-northeast-3': #https://github.com/boto/boto3/issues/1943
           continue
       transfer = session.client("transfer", region_name=region_name)
       servers = transfer.list_servers()
       for server in servers['Servers']:
          if server['EndpointType']=='VPC_ENDPOINT':
              print(server['ServerId'], region_name)
   ```

1. 在您擁有要更新的伺服器清單之後，您可以使用下列各節所述的其中一種方法，將 更新`EndpointType`為 `VPC`。

## 使用 更新伺服器端點類型 AWS 管理主控台
<a name="update-endpoint-console"></a>

1. 在 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/) 開啟 AWS Transfer Family 主控台。

1. 在導覽窗格中，選擇 **Servers (伺服器)**。

1. 選取您要變更端點類型的伺服器的核取方塊。
**重要**  
您必須先停止伺服器，才能變更其端點。

1. 針對 **Actions** (動作)，選擇 **Stop** (停止)。

1. 在出現的確認對話方塊中，選擇**停止**以確認您想要停止伺服器。
**注意**  
在繼續下一個步驟之前，請等待伺服器**狀態**變更為**離線**；這可能需要幾分鐘的時間。您可能必須在**伺服器**頁面上選擇**重新整理**，以查看狀態變更。

1. 狀態變更為**離線**後，選擇要顯示伺服器詳細資訊頁面的伺服器。

1. 在**端點詳細資訊**區段中，選擇**編輯**。

1. 選擇端點**類型的****託管 VPC**。

1. 選擇**儲存**

1. 對於**動作**，選擇**開始**並等待伺服器狀態變更為**線上**；這可能需要幾分鐘的時間。

## 使用 更新伺服器端點類型 CloudFormation
<a name="update-endpoint-cloudformation"></a>

本節說明如何使用 CloudFormation 將伺服器的 更新`EndpointType`為 `VPC`。針對您已部署使用的 Transfer Family 伺服器，請使用此程序 CloudFormation。在此範例中，用於部署 Transfer Family 伺服器的原始 CloudFormation 範本如下所示：

```
AWS TemplateFormatVersion: '2010-09-09'
Description: 'Create AWS Transfer Server with VPC_ENDPOINT endpoint type'
Parameters:
  SecurityGroupId:
    Type: AWS::EC2::SecurityGroup::Id
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
  VpcId:
    Type: AWS::EC2::VPC::Id
Resources:
  TransferServer:
    Type: AWS::Transfer::Server
    Properties:
      Domain: S3
      EndpointDetails:
        VpcEndpointId: !Ref VPCEndpoint
      EndpointType: VPC_ENDPOINT
      IdentityProviderType: SERVICE_MANAGED
      Protocols:
        - SFTP
  VPCEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: com.amazonaws.us-east-1.transfer.server
      SecurityGroupIds:
        - !Ref SecurityGroupId
      SubnetIds:
        - !Select [0, !Ref SubnetIds]
        - !Select [1, !Ref SubnetIds]
        - !Select [2, !Ref SubnetIds]
      VpcEndpointType: Interface
      VpcId: !Ref VpcId
```

範本會隨著下列變更而更新：
+ `EndpointType` 已變更為 `VPC`。
+ `AWS::EC2::VPCEndpoint` 資源已移除。
+ `SecurityGroupId`、 `SubnetIds`和 `VpcId`已移至`AWS::Transfer::Server`資源的 `EndpointDetails`區段，
+ 的 `VpcEndpointId` 屬性`EndpointDetails`已移除。

更新的範本如下所示：

```
AWS TemplateFormatVersion: '2010-09-09'
Description: 'Create AWS Transfer Server with VPC endpoint type'
Parameters:
  SecurityGroupId:
    Type: AWS::EC2::SecurityGroup::Id
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
  VpcId:
    Type: AWS::EC2::VPC::Id
Resources:
  TransferServer:
    Type: AWS::Transfer::Server
    Properties:
      Domain: S3
      EndpointDetails:
        SecurityGroupIds:
          - !Ref SecurityGroupId
        SubnetIds:
          - !Select [0, !Ref SubnetIds]
          - !Select [1, !Ref SubnetIds]
          - !Select [2, !Ref SubnetIds]
        VpcId: !Ref VpcId
      EndpointType: VPC
      IdentityProviderType: SERVICE_MANAGED
      Protocols:
        - SFTP
```

**使用 更新部署的 Transfer Family 伺服器的端點類型 CloudFormation**

1. 使用下列步驟停止您要更新的伺服器。

   1. 在 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/) 開啟 AWS Transfer Family 主控台。

   1. 在導覽窗格中，選擇 **Servers (伺服器)**。

   1. 選取您要變更端點類型的伺服器的核取方塊。
**重要**  
您必須先停止伺服器，才能變更其端點。

   1. 針對 **Actions** (動作)，選擇 **Stop** (停止)。

   1. 在出現的確認對話方塊中，選擇**停止**以確認您想要停止伺服器。
**注意**  
在繼續下一個步驟之前，請等待伺服器**狀態**變更為**離線**；這可能需要幾分鐘的時間。您可能必須在**伺服器**頁面上選擇**重新整理**，以查看狀態變更。

1. 更新 CloudFormation 堆疊

   1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

   1. 選擇用來建立 Transfer Family 伺服器的堆疊。

   1. 選擇**更新**。

   1. 選擇**取代目前範本**

   1. 上傳新範本。CloudFormation 變更集可協助您了解範本變更會如何影響執行中的資源，然後再實作它們。在此範例中，將修改 Transfer 伺服器資源，並將移除 VPCEndpoint 資源。VPC 端點類型伺服器會代表您建立 VPC 端點，取代原始`VPCEndpoint`資源。

      上傳新範本後，變更集看起來會類似以下內容：  
![\[顯示用於取代目前 CloudFormation 範本的變更集預覽頁面。\]](http://docs.aws.amazon.com/zh_tw/transfer/latest/userguide/images/vpc-endpoint-update-cfn.png)

   1. 更新堆疊。

1. 堆疊更新完成後，請前往 Transfer Family 管理主控台，網址為 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/)。

1. 重新啟動伺服器。選擇您在其中更新的伺服器 CloudFormation，然後從**動作**功能表中選擇**開始**。

## 使用 API 更新伺服器 EndpointType
<a name="update-endpoint-cli"></a>

您可以使用 [describe-server](https://docs.aws.amazon.com/cli/latest/reference/transfer/update-server.html) AWS CLI 命令或 [UpdateServer](https://docs.aws.amazon.com/transfer/latest/APIReference/API_UpdateServer.html) API 命令。下列範例指令碼會停止 Transfer Family 伺服器、更新 EndpointType、移除 VPC\$1ENDPOINT，以及啟動伺服器。

```
import boto3
import time

profile = input("Enter the name of the AWS account you'll be working in: ")
region_name = input("Enter the AWS Region you're working in: ")
server_id = input("Enter the AWS Transfer Server Id: ")

session = boto3.Session(profile_name=profile)

ec2 = session.client("ec2", region_name=region_name)
transfer = session.client("transfer", region_name=region_name)

group_ids=[]

transfer_description = transfer.describe_server(ServerId=server_id)
if transfer_description['Server']['EndpointType']=='VPC_ENDPOINT':
    transfer_vpc_endpoint = transfer_description['Server']['EndpointDetails']['VpcEndpointId']
    transfer_vpc_endpoint_descriptions = ec2.describe_vpc_endpoints(VpcEndpointIds=[transfer_vpc_endpoint])
    for transfer_vpc_endpoint_description in transfer_vpc_endpoint_descriptions['VpcEndpoints']:
        subnet_ids=transfer_vpc_endpoint_description['SubnetIds']
        group_id_list=transfer_vpc_endpoint_description['Groups']
        vpc_id=transfer_vpc_endpoint_description['VpcId']
        for group_id in group_id_list:
             group_ids.append(group_id['GroupId'])
    if transfer_description['Server']['State']=='ONLINE':
        transfer_stop = transfer.stop_server(ServerId=server_id)
        print(transfer_stop)
        time.sleep(300) #safe
        transfer_update = transfer.update_server(ServerId=server_id,EndpointType='VPC',EndpointDetails={'SecurityGroupIds':group_ids,'SubnetIds':subnet_ids,'VpcId':vpc_id})
        print(transfer_update)
        time.sleep(10) 
        transfer_start = transfer.start_server(ServerId=server_id)
        print(transfer_start)
        delete_vpc_endpoint = ec2.delete_vpc_endpoints(VpcEndpointIds=[transfer_vpc_endpoint])
```