云计算
现在阅读
Deploying multiple web apps to Elastic Beanstalk
0

Deploying multiple web apps to Elastic Beanstalk

由 ultracpy2018年1月20日

Introduction

In my organization, we typically install a single web app to a single AWS Elastic Beanstalk server, and that web app is at the root directory [Default Web Site], and the URL ends up being http://instancename.   We wanted to deploy several apps to the same box, and thus have http://instancename/app1, http://instancename/app2, etc.  

Amazon’s blog : https://aws.amazon.com/blogs/developer/multiple-application-support-for-net-and-elastic-beanstalk/ shows a screen shot of example “amazing front end” and “amazing admin” sites, but no sample code.  I will present a asp.net core and classic asp.net mvc website, deployed to the same amazon instancename, with working example code. 

Using the code

Refer to prerequisite in “points of interest” below.  

Run visual studio 2015

New web project, .net core, create application called “core”, and save to desktop folder.

Run visual studio developer command prompt

C:\Users\username\Desktop\Core\CoreApp\src\CoreApp>dotnet run coreapp

Visit http://localhost:5000 in my browser, to verify it works. 

Right click, and deploy to my EB instance, as http://instancename.us-east-1.elasticbeanstalk.com/core

Verify that it works.

Make a new ASP app, using online template “Dynamic MVC”, added to desktop folder “Asp”.

Right click, and deploy to my EB instance, as http://instancename.us-east-1.elasticbeanstalk.com/asp

Verify that it works.

Amazon’s example says to deploy the .net core app, and “bring along” the associated asp.net app.  I therefore choose the core app to be “the parent project” to deploy, and the asp app to be “the child project”.

Default visual studio deploy behavior will try to deploy a global web config with a url rewrite section.  This following step will override this behavior. 

Unload each project by right clicking the project name in visual studio, clicking “unload”, and right clicking again, and clicking “Edit project”. In the first <PropertyGroup> you see, insert the following line.  You may refer to the enclosed application csproj raw xml to see how it was done in the example app. 

<DeployIISAppPath>Default Web Site/sitename</DeployIISAppPath>

Per the article, add the manifest. 

Add a json formatted file, named “aws-windows-deployment-manifest.json”, to the root directory of the Core csproj. The .net core portion, Core, will be designated in the first section “aspNetCoreWeb”.  The classic asp portion, Asp, will be designated in the msDeploy section, in zip file format.  My manifest, much like Amazon’s example, in the enclosed example application, is shown below.  For more manifest information, consult their site: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/dotnet-manifest.html 

{
  "manifestVersion": 1,
  "deployments": {
    "aspNetCoreWeb": [
      {
        "name": "Core",
        "parameters": {
          "appBundle": "./Core",
          "iisPath": "/Core"
        }
      }
    ],

    "msDeploy": [
      {
        "name": "Asp",
        "parameters": {
          "appBundle": "AspApp.zip",
          "iisPath": "/Asp"
        }
      }
    ]
  }
}

Make the powershell as described in the article.  Create a .ps1 file as follows.   In my example, I add the .ps1 file to the “core” project for housekeeping purposes, but it is not necessary.  After all is done, I will be adding all the files to source control, so why not put the deploy script in with the solution you will be deploying?

Go to the directory where the .net core site resides in the source tree, to do dotnet publish.

set-location C:\Users\username\Desktop\Core\CoreApp\src\CoreApp

$publishFolder = "c:\temp\publish"

$publishWorkspace = [System.IO.Path]::Combine($publishFolder, "workspace")

If (Test-Path $publishWorkspace){
       Remove-Item $publishWorkspace -Confirm:$false -Force
}

$appBundle = [System.IO.Path]::Combine($publishFolder, "app-bundle.zip")

If (Test-Path $appBundle){
       Remove-Item $appBundle -Confirm:$false -Force
}

Do the dotnet publish.

Write-Host 'Publish the ASP.NET Core frontend' 

$publishFrontendFolder = [System.IO.Path]::Combine($publishWorkspace, "Core")

write-host $publishFrontendFolder

dotnet publish .\project.json -o $publishFrontendFolder -c Release

MSBuild creates the zipped package for Asp.  Note the path specified is relative to the Core project directory.  Let’s be real.  In an organization, you very well have the various web site solutions in different parts of your source tree; rarely will you have all websites going to a common server all wrapped within the same solution.   So just make your relative path have the right number of ..\..\ in there to reference the other websites to publish.

Write-Host 'Create msdeploy archive for Asp'

msbuild   ..\..\..\Desktop\Core\CoreApp\src\CoreApp.csproj /t:package /p:Configuration=Release

Copy-Item ..\..\..\Desktop\Core\CoreApp\obj\Release\Package\Asp.zip $publishWorkspace

The project manifest we created gets copied to the publish folder, along with the two zip files we made from dotnet publish and msbuild.

Write-Host 'Copy deployment manifest'

Copy-Item .\aws-windows-deployment-manifest.json $publishWorkspace

Write-Host 'Zipping up publish workspace to create app bundle'

Add-Type -assembly "system.io.compression.filesystem"

[io.compression.zipfile]::CreateFromDirectory( $publishWorkspace, $appBundle)

Amazon shows more powershell steps to automate the deploy of the zip we are about to deploy.   However, you can manually deploy the zip file you just made. 

Log in to your amazon console, https://yourname.signin.aws.amazon.com/console

In the “all services” section, click on “elastic beanstalk”, and navigate to your target server.   In the “Overview” section, click the “Upload and deploy” button, choose your app-bundle.zip, and deploy. 

Points of Interest

Important prerequisite, which I learned the hard way:   Start with a perfectly clean install of IIS on your AWS instance.  If your server has some apps installed, no problem: just be sure that no web app resides at the root:  “default web site” or “c:\inetpub\wwwroot”.    

If you just right click a web app in visual studio, and publish to elastic beanstalk, as the default website on your AWS instance, it will put your code in this default website.   If you then follow that app by installing to instancename\site1,  you’ll get a misleading error message saying you have a duplicate script handler section in web config.  

 

While working this out, and communicating my progress with Amazon support, they admitted the “amazing front end” was indeed only a screen shot.  I gave them the enclosed source to use as an example in their online help / documentation, and they liked it, and might publish it!   You can say you saw it HERE first! 🙂

History

20170419  Initial revision

出处:https://www.codeproject.com/Articles/1182976/Deploying-multiple-web-apps-to-Elastic-Beanstalk

关于作者
ultracpy
评论

你必须 登录 提交评论