終止運HTTPS行的 Amazon EC2 實例。 NET - AWS Elastic Beanstalk

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

終止運HTTPS行的 Amazon EC2 實例。 NET

下列組態檔案會建立並執行執行下列工作的 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),請在範例的 [移除HTTP繫結] 區段下加入Remove-WebBinding命令。

範例 .ebextension/. 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 上的流量。下列組態檔案會使用 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