如何将应用程序池分配给 AWS Elastic Beanstalk 中不同的 ASP.Net 核心网站?

How to assign Application Pools to different ASP.Net Core web sites in AWS Elastic Beanstalk?

我们在 Elastic Beanstalk 运行 Windows 2019 核心服务器和 IIS 中部署了两个 ASP.Net 核心 3.1 网站(具有不同的域 names/host headers) .我们能够通过遵循 https://aws.amazon.com/blogs/developer/multi-app-support-with-custom-domains-for-net-and-aws-elastic-beanstalk/

来做到这一点

但是,这两个网站都在 'DefaultAppPool' 下 运行。 ASP.Net Core 不允许每个应用程序池有多个应用程序(HTTP 错误 500.35 - ASP.NET Core 不支持同一应用程序池中的多个应用程序)。

因此,我们将以下部分添加到 aws-windows-deployment-manifest。json

"iisConfig": {
    "appPools": [
      {
        "name": "AppPool1",
        "managedPipelineMode": "Integrated",
        "managedRuntimeVersion": "v4.0"
      },
      {
        "name": "AppPool2",
        "managedPipelineMode": "Integrated",
        "managedRuntimeVersion": "v4.0"
      }
    ]
  }

并且,EB 部署在 IIS 中创建了相应的应用程序池。

现在,我们无法将应用程序池分配给每个网站。理想情况下,我们希望在 'install*.ps1' PowerShell 脚本或“.ebextensions”文件夹中的“.config”文件中使用来自 WebAdministration 模块 (https://docs.microsoft.com/en-us/powershell/module/webadministration/?view=windowsserver2019-ps) 的命令。但是,Web 管理模块在任何一个地方都不可用,并且 IMPORT-MODULE WebAdministration 也不行。在执行 'install*.ps1' 个脚本时,只有 IISAdministration 模块 (https://docs.microsoft.com/en-us/powershell/module/iisadministration/?view=windowsserver2019-ps) 可用。

那么,如何在 AWS Elastic Beanstalk 中将应用程序池分配给不同的 ASP.Net 核心网站?

在清单文件中定义 appPools 后,您可以使用 appPool 属性:

将它们分配给您的应用
{
    "manifestVersion": 1,
    "iisConfig": {
        "appPools": [
            {
              "name": "AppPool1",
              "managedPipelineMode": "Integrated",
              "managedRuntimeVersion": "v4.0"
            },
            {
              "name": "AppPool2",
              "managedPipelineMode": "Integrated",
              "managedRuntimeVersion": "v4.0"
            }
          ]
    },
    "deployments": {
        "aspNetCoreWeb": [
            {
                "name": "frontend",
                "parameters": {
                    "appBundle": "./frontend",
                    "iisPath": "/frontend",
                    "appPool": "AppPool1"
                }
            },
            {
                "name": "ext-api",
                "parameters": {
                    "appBundle": "./ext-api",
                    "iisPath": "/ext-api",
                    "appPool": "AppPool2"
                }
            }
        ]
    }
}

非常简单的记录(对于 Windows 服务器)秘诀隐藏在 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

因此,对我们来说,以下策略奏效了。我们在.ebextensions

文件中添加了以下内容在apppool.config(名称无所谓只要有.config扩展名)
files:
  "c:/Program Files/Amazon/ElasticBeanstalk/hooks/appdeploy/post/config.ps1":
    content: |
      Start-IISCommitDelay
      $site = Get-IISSite "site1"
      $site.Applications["/"].ApplicationPoolName = "AppPool1"
      $site = Get-IISSite "site2"
      $site.Applications["/"].ApplicationPoolName = "AppPool2"
      Stop-IISCommitDelay

这在 c:/Program Files/Amazon/ElasticBeanstalk/hooks/appdeploy/post/ 文件夹中创建了一个 powershell 脚本 config.ps1。 powershell 脚本在创建新实例或部署新版本后执行。

唯一的缺点是每次在现有实例上部署新版本时都会创建 config.ps1。它不会因为旧文件重命名为 config.ps1.bak 并被 EB 部署过程忽略而受到伤害。这里肯定有改进的余地(将推迟一天)。

为了完整起见,这里是完整的aws-windows-deployment-manifest.json

{
  "manifestVersion": 1,
  "iisConfig": {
    "appPools": [
      {
        "name": "AppPool1",
        "managedPipelineMode": "Integrated",
        "managedRuntimeVersion": "v4.0"
      },
      {
        "name": "AppPool2",
        "managedPipelineMode": "Integrated",
        "managedRuntimeVersion": "v4.0"
      }
    ]
  },
  "deployments": {
    "custom": [
      {
        "name": "site1",        
        "scripts": {
          "install": {
            "file": "install1.ps1"
          },
          "restart": {
            "file": "restart.ps1"
          },
          "uninstall": {
            "file": "uninstall1.ps1"
          }
        }
      },
      {
        "name": "site2",        
        "scripts": {
          "install": {
            "file": "install2.ps1"
          },
          "restart": {
            "file": "restart.ps1"
          },
          "uninstall": {
            "file": "uninstall2.ps1"
          }
        }
      }  
    ]
  }
}

这是install1.ps1

Copy-Item -Path "C:\staging\site1" -Destination "C:\inetpub" -Recurse -Force
New-IISSite -Name "site1" -BindingInformation "*:80:*.site1.com" -PhysicalPath "C:\inetpub\site1" -Force

uninstall1.ps1

Remove-IISSite -Name "site1" -Confirm:$False
rm -r "c:\inetpub\site1" -Force

restart.ps1

iisreset /timeout:1

install2.ps1uninstall2.ps1 与上面的 site1 相似。

我们在网上参考了很多文章,并感谢所有这些文章Thank You