Can AWS CodeDeploy execute powershell scripts

amazon-web-servicesdeployment

Is it acceptable to include Powershell scripts directly into the appspec.yml files?

version: 0.0
os: windows
files:
  - source: ./MyWebsiteFiles
    destination: /MyWebsite
hooks:
  AfterInstall:
    - location: /Scripts/MyScript.ps1
      timeout: 300

I'm running a ps1 file that executes instantly through the Powershell console on the EC2 instance, but my deployment is getting stuck or failing at the point of executing the Powershell script.

There doesn't appear to be a list of acceptable file types that can be included in an appspec.yml file, from what I can see on the CodeDeploy documentation.

Thanks for your help.

Best Answer

YES!

Although I haven't been able to find a definitive list of the acceptable script types, it looks like the answer is Yes - Powershell .ps1 scripts are acceptable and will be executed if included in the appspec.yml file.

My Powershell script wasn't working consistently until I added the code as recommended on the troubleshooting page by @kafka, so my script now contains the following above it:

# 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...
# ...
#
# I PUT MY SCRIPT HERE
#

I am still unsure as to whether my script is only compatible with the 64 bit version of Powershell or how to find it out, but it works with this modification.

I hope that this helps someone.

Update: A note on file location

I'd like to highlight an issue that I faced with running .ps1 scripts. From my experience, ps1 scripts must be placed at the root of your deployment package (the same folder location as your appspec.yml file), if not, your script may fail to execute and the deployment will display as 'Successful' in CodeDeploy. More info on this here.