PowerShell - How to package custom modules into a zip package for AWS Lamdba

Viewed 567

I am trying to package a script I have as a Lambda function. The script loads some functions from some other ps1 files in the same folder. Currently use "." to include. When I try to use New-AWSPowerShellLambdaPackage -ProjectDirectory projectName -OutputPackage projectName.zip to package the script files, it keeps failing with error below.

Line |
 253 |              throw $msg
     |              ~~~~~~~~~~
     | Error publishing PowerShell Lambda Function: -1 CALLSTACK: Command                        Arguments                                                                                                   Location -------
     | ---------                                                                                                   -------- _packageProject
     | {OutputPackage=C:\Users\tom\projectName\projectname.zip, BuildDirectory=projectName} _DeploymentFunctions.ps1: line 251 New-AWSPowerShellLambdaPackage {ProjectDirectory=projectName,
     | OutputPackage=projectName.zip}                                                       New-AWSPowerShellLambdaPackage.ps1: line 182 <ScriptBlock>                  {}
     | <No file>

Is this because I use "." to source the other ps1 files? Should I convert them into modules? Anyone can suggest any documentation for this? I can't seem to find any good source detailing how to package PowerShell lambda. All the examples I can find are for single ps1 file.

1 Answers

You need to have a proper .csproj file to be able to use -ProjectDirectory switch.

I typically use the following procedure:

  1. Generate project structure using New-AWSPowerShellLambdaPackage with -StagingDirectory switch:

    New-AWSPowerShellLambdaPackage -StagingDirectory projectName -ScriptPath .\projectName.ps1 -OutputPackage projectName.zip
    
  2. Modify projectName.csproj to include additional files. Following example adds additionalScript.ps1 and all files within SQL folder into the package:

    <ItemGroup>
        <Content Include="projectName.ps1">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="additionalScript.ps1">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="./SQL/**">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="./Modules/**">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
    
  3. Use New-AWSPowerShellLambdaPackage with -ProjectDirectory switch to build the package:
    New-AWSPowerShellLambdaPackage -ProjectName projectName -OutputPackage projectName.zip
    
Related