AWS CodeBuild for PowerShell


As I’ve been building more AWS Lambda for PowerShell functions I’m wanting to automate testing and deployment with Continuous Integration and Continuous Deployment, a CI/CD Pipeline. To do this one of the steps we’ll need to do is use the AWS Lambda PowerShell cmdlet New-AWSPowerShellLambdaPackage which will package up our script with all it’s dependencies into a zip file. I’ll want to run this on a build environment within AWS CodeBuild. Unfortunately there’s not a PowerShell specific runtime in CodeBuild yet. However, we can utilize the existing .NET 2.2 runtime provided and install PowerShell on it. With that we can run our PowerShell scripts on CodeBuild to build our functions artifacts.

Build Commands

To do this we can add to our AWS CodeBuild buildspec.yml file the commands within the install phase to add PowerShell and run our commands or scripts during the build phase. Alternatively I might be able to create a custom Docker image with PowerShell installed but for now this is the easy option. Also the next step will involve creating a script that builds the package for each PowerShell script stored in a CodeCommit Repository.

version: 0.2

      dotnet: 2.2
      - echo Install started on `date`
      - wget -q
      - dpkg -i packages-microsoft-prod.deb
      - apt-get update
      - add-apt-repository universe
      - apt-get install -y powershell
      - pwsh
      - pwsh -Command 'Install-Module -Name AWSPowerShell.NetCore -Confirm:$False -Force'
      - pwsh -Command 'Install-Module AWSLambdaPSCore -Confirm:$False -Force'
      - echo Build started on `date`
      - pwsh -Command 'New-AWSPowerShellLambdaPackage -ScriptPath $env:ScriptPath -OutputPackage $env:OutputPackage'
      - echo Build completed on `date`
    - $OutputPackage
  discard-paths: yes

Environment Variables

Note the $env:ScriptPath, $env:OutputPackage, and $OutputPackage variables are actually just two environment variables set on the CodeBuild environment itself. For now I have the CodeBuild project set to get the source from CodeCommit and is triggered through a CloudWatch Event when a commit is pushed to the master branch in my repository. In the future I’ll be setting up AWS CodePipeline to orchestrate the integration and deployment phases. The goal being that I simply commit my script files to a repository and it automatically builds and deploys to AWS Lambda.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.