6

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.

4
  • 1
  • 1
    How does that answer the question?
    – Luke
    Jun 14, 2016 at 10:43
  • Judging by your answer it looks like it's helped!
    – kafka
    Jun 14, 2016 at 14:54
  • Thankfully that bit of the article helped. Thanks. Sorry that someone felt the need to down vote, it's a genuine enquiry.
    – Luke
    Jun 14, 2016 at 15:03

2 Answers 2

10

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.

1
  • I wish I could upvote this answer twice!! Spent two days trying to figure out why powershell works in the window but not when codedeploy calls it!!
    – user20358
    Oct 21, 2019 at 5:46
2

Yes it can but unless you set a variable errors encountered during running of the script will not stop your deployment.

$ErrorActionPreference = ‘Stop’

See this link for more details:

https://aws.amazon.com/premiumsupport/knowledge-center/powershell-cmdlet-errors-codedeploy/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .