在运行 .NET 的 Amazon EC2 实例上终止 HTTPS - AWS Elastic Beanstalk

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

在运行 .NET 的 Amazon EC2 实例上终止 HTTPS

以下配置文件创建并运行用于执行以下任务的 Windows PowerShell 脚本:

  • 检查是否存在绑定到端口 443 的现有 HTTPS 证书。

  • 从 Amazon S3 存储桶获取 PFX 证书

    注意

    向中添加访问亚马逊 S3 存储桶中的 SSL 证书的AmazonS3ReadOnlyAccess策略。aws-elasticbeanstalk-ec2-role

  • 从中获取密码AWS Secrets Manager。

    注意

    在中aws-elasticbeanstalk-ec2-role添加一条允许对包含证书密码的密钥secretsmanager:GetSecretValue执行操作的语句

  • 安装证书。

  • 将证书绑定到端口 443。

    注意

    要删除 HTTP 终端节点(端口 80),请在示例的删除 HTTP 绑定 部分中包括 Remove-WebBinding 命令。

例 .ebextensions/ .config https-instance-dotnet
files: "C:\\certs\\install-cert.ps1": content: | import-module webadministration ## Settings - replace the following values with your own $bucket = "DOC-EXAMPLE-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 上的流量。以下配置文件使用 AWS CloudFormation 函数检索安全组的 ID,并向其中添加规则。

例 .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

对于负载平衡的环境,您可以将负载均衡器配置为要么原封不动地传递安全流量,要么解密并重新加密以进行加密。 end-to-end