

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

# 在執行 .NET 的 Amazon EC2 執行個體上終止 HTTPS
<a name="SSLNET.SingleInstance"></a>

下列的[組態檔案](ebextensions.md)會建立和執行 Windows PowerShell 指令碼，此指令碼會執行下列任務：
+ 檢查現有的 HTTPS 憑證是否繫結至連接埠 443。
+ 從 Amazon S3 儲存貯體取得 [PFX 憑證](configuring-https-ssl.md)。
**注意**  
將 `AmazonS3ReadOnlyAccess`政策新增至 `aws-elasticbeanstalk-ec2-role`，以存取 Amazon S3 儲存貯體中的 SSL 憑證。
+ 從 取得密碼 AWS Secrets Manager。
**注意**  
在 中新增陳述式`aws-elasticbeanstalk-ec2-role`，允許包含憑證密碼之秘密`secretsmanager:GetSecretValue`的動作
+ 安裝憑證。
+ 將憑證繫結至連接埠 443。
**注意**  
若要移除 HTTP 端點 (連接埠 80)，請將 `Remove-WebBinding` 命令納入此範例 **Remove the HTTP binding (移除 HTTP 綁定)** 部分之下。

**Example .ebextensions/https-instance-dotnet.config**  

```
files:
  "C:\\certs\\install-cert.ps1":
    content: |
      import-module webadministration
      ## Settings - replace the following values with your own
      $bucket = "amzn-s3-demo-bucket"  ## S3 bucket name
      $certkey = "example.com.pfx"    ## S3 object key for your PFX certificate
      $secretname = "example_secret"  ## AWS Secrets Manager name for a secret that contains the certificate's password
      ##

      # Set variables
      $certfile = "C:\cert.pfx"
      $pwd = Get-SECSecretValue -SecretId $secretname | select -expand SecretString

      # Clean up existing binding
      if ( Get-WebBinding "Default Web Site" -Port 443 ) {
        Echo "Removing WebBinding"
        Remove-WebBinding -Name "Default Web Site" -BindingInformation *:443:
      }
      if ( Get-Item -path IIS:\SslBindings\0.0.0.0!443 ) {
        Echo "Deregistering WebBinding from IIS"
        Remove-Item -path IIS:\SslBindings\0.0.0.0!443
      }

      # Download certificate from S3
      Read-S3Object -BucketName $bucket -Key $certkey -File $certfile
      
      # Install certificate
      Echo "Installing cert..."
      $securepwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
      $cert = Import-PfxCertificate -FilePath $certfile cert:\localMachine\my -Password $securepwd
      
      # Create site binding
      Echo "Creating and registering WebBinding"
      New-WebBinding -Name "Default Web Site" -IP "*" -Port 443 -Protocol https
      New-Item -path IIS:\SslBindings\0.0.0.0!443 -value $cert -Force
      
      ## Remove the HTTP binding
      ## (optional) Uncomment the following line to unbind port 80
      # Remove-WebBinding -Name "Default Web Site" -BindingInformation *:80:
      ##
      
      # Update firewall
      netsh advfirewall firewall add rule name="Open port 443" protocol=TCP localport=443 action=allow dir=OUT

commands:
  00_install_ssl:
    command: powershell -NoProfile -ExecutionPolicy Bypass -file C:\\certs\\install-cert.ps1
```

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 443 上的流量。下列組態檔案會使用 CloudFormation [函數](ebextensions-functions.md)擷取安全群組的 ID，並將規則新增至其中。

**Example .ebextensions/https-instance-single.config**  

```
Resources:
  sslSecurityGroupIngress: 
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443
      CidrIp: 0.0.0.0/0
```

針對使用負載平衡的環境，您應設定負載平衡器，使其[以未處理之方式通過安全流量](https-tcp-passthrough.md)，或針對端點對端點加密進行[解密與重新加密](configuring-https-endtoend.md)。