在執行 .NET 的 Amazon EC2 執行個體上終止 HTTPS - AWS Elastic Beanstalk

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

在執行 .NET 的 Amazon EC2 執行個體上終止 HTTPS

下列組態檔案會建立並執行執行下列工作的 Windows PowerShell 指令碼:

  • 檢查連接埠 443 的現有 HTTPS 憑證繫結。

  • 從 Amazon S3 存儲桶獲取 PFX 證書

    注意

    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 綁定) 部分之下。

範例 .ebextension/. 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,並對其新增規則。

範例 .ebextension/. 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