Using CodeDeploy to deploy to EC2 instance

0

I have a Windows Server 2016 base AMI on AWS that I am raising for CodeDeploy to deploy into. I am using the CLI to deploy a core asp.net website from github into this EC2 instance.

aws deploy create-deployment --application-name my-App --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name App-DepGrp --description "My GitHub deployment demo" --github-location repository=xxx-yyy/aws,commitId=d2c7cfcf90sfsfd2c7cfcfgs9e1e0f50eshshsgsgd2c7cfcfd2c7cgbsfgsbg

The deploy starts and the appspec calls the Install.ps1 file which goes and creates the folder as called out in the file command in the powershell script file. The website doesn't get created. The app pool also doesn't get created. This is what my Appspec.yml file looks like:

version: 0.0
os: windows
files:
  - source: \
    destination: c:\website-dropfolder
hooks:
 ApplicationStart:
    - location: .\Install.ps1
      timeout: 300
      runas: Administrator

This is the Install.ps1

# Create folder to publish binaries from drop folder
mkdir c:\website-published

# Switch to drop folder
Set-Location C:\website-dropfolder   

# Restore the nuget references
& "C:\Program Files\dotnet\dotnet.exe" restore  

# Publish application with all of its dependencies and runtime for IIS to use
& "C:\Program Files\dotnet\dotnet.exe" publish --configuration release -o c:\website-published --runtime active  

# Create an IIS website and point it to the published folder
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Import-Module WebAdministration; New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWebsite -PhysicalPath "$env:systemdrive\website-published"}


# Create AppPool for website and set CLR version to core-dotnet
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Import-Module WebAdministration; New-WebAppPool CoreWebsiteAppPool}

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Import-Module WebAdministration; Set-ItemProperty IIS:\AppPools\CoreWebsiteAppPool managedRuntimeVersion "" -verbose}

I have put the -ErrorAction SilentlyContinue in case there are any issues with the dotnet restore or publish. What is causing the website to not get created? I ran the same scripts on my windows 10 dev machine and the website was created an same with the app pool and even setting its CLR version. The same doesn't work on a Windows Server 2016?

EDIT

I looked at the User Data Logs and it seems to be erroring out from this line onwards: New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWebsite -PhysicalPath "$env:systemdrive\website-published" -ErrorAction SilentlyContinue

Error is:

System.Management.Automation.PSCustomObjectSystem.Object1Preparing modules for first use.0-1-1Completed-1 New-WebSite : Cannot retrieve the dynamic parameters for the cmdlet. _x000D__x000A_Retrieving the COM class factory for component with CLSID _x000D__x000A_{688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: _x000D__x000A_80040154 Class not registered (Exception from HRESULT: 0x80040154 _x000D__x000A_(REGDB_E_CLASSNOTREG))._x000D__x000A_At line:1 char:34_x000D__x000A_+ ... nistration; New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWe ..._x000D__x000A_+

The powershell commands don't seem to work when they are called by the codedeploy agent even thought its being run as administrator, but when I manually run the script file on the powershell prompt it works as expected. I even specified the path to the powershell executable to be called just so that it doesn't use the one in the C:\Windows\SysWOW64 folder. Even that is not working. Any ideas?

powershell
asp.net-core
web-deployment
windows-server-2016
aws-code-deploy
asked on Stack Overflow Oct 20, 2019 by user20358 • edited Oct 21, 2019 by user20358

1 Answer

0

From the error message shared:

"Exception from HRESULT: 0x80040154 _x000D__x000A_(REGDB_E_CLASSNOTREG)"

... it looks to me that CodeDeploy is running the PS script in 32 bit mode, instead of 64 bit. This is a frequent issue called out in AWS docs [1].

Can you modify your PowerShell script as suggested in [1] as follows:

# Are you running in 32-bit mode?
#   (\SysWOW64\ = 32-bit mode)

if ($PSHOME -like "*SysWOW64*")
{
  Write-Warning "Restarting this script under 64-bit Windows PowerShell."

  # Restart this script under 64-bit Windows PowerShell.
  #   (\SysNative\ redirects to \System32\ for 64-bit mode)

  & (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File `
    (Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args

  # Exit 32-bit script.

  Exit $LastExitCode
}

# Was restart successful?
Write-Warning "Hello from $PSHOME"
Write-Warning "  (\SysWOW64\ = 32-bit mode, \System32\ = 64-bit mode)"
Write-Warning "Original arguments (if any): $args"

# Your 64-bit script code follows here...
# ...

Ref:

[0] http://kino505.blogspot.com/2016/12/aws-codedeploy-windows-3264-bit.html

[1] Troubleshoot EC2/On-Premises Deployment Issues - Windows PowerShell scripts fail to use the 64-bit version of Windows PowerShell by default - https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-deployments-powershell

answered on Stack Overflow Oct 21, 2019 by shariqmaws • edited Oct 23, 2019 by shariqmaws

User contributions licensed under CC BY-SA 3.0