

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在运行.NET 的亚马逊 EC2 实例上终止 HTTPS
<a name="SSLNET.SingleInstance"></a>

以下[配置文件](ebextensions.md)创建并运行用于执行以下任务的 Windows PowerShell 脚本：
+ 检查现有 HTTPS 证书是否已绑定到端口 443。
+ 从 Amazon S3 存储桶获取 [PFX 证书](configuring-https-ssl.md)。
**注意**  
向 `aws-elasticbeanstalk-ec2-role` 添加 `AmazonS3ReadOnlyAccess` 策略，以访问 Amazon S3 存储桶中的 SSL 证书。
+ 从中获取密码 AWS Secrets Manager。
**注意**  
在 `aws-elasticbeanstalk-ec2-role` 中添加一条语句，以允许对包含证书密码的密钥执行 `secretsmanager:GetSecretValue` 操作
+ 安装证书。
+ 将证书绑定到端口 443。
**注意**  
要删除 HTTP 终端节点（端口 80），请在示例的**删除 HTTP 绑定** 部分中包括 `Remove-WebBinding` 命令。

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

```
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/ .config https-instance-single**  

```
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)。 end-to-end