Prerequisites
- powershell core
- aws cli
Install-Module AWSLambdaPSCore -Scope CurrentUser
Install-Module -Name AWS.Tools.Installer -Scope CurrentUser
(only if using AWS modules)Install-AWSToolsModule -Name Common
(only if using AWS modules)- lambda execution role
Start a lamda powershell project.
New-AWSPowerShellLambda -ScriptName MyFirstPSScript -Template Basic
cd MyFirstPSScript
Create a custom powershell module to hold all your stuff.
mkdir -p StuffModule/Stuff
New-Item StuffModule/Stuff.psm1
Add a function to StuffModule/Stuff.psm1
to return the stuff path.
function Get-StuffPath {
Join-Path -Path $PSScriptRoot -ChildPath 'Stuff'
}
Move whatever files you want included in the lambda to the StuffModule/Stuff
folder.
Create a module manifest
New-ModuleManifest ./StuffModule/Stuff.psd1 -RootModule Stuff.psm1 -Description 'StuffModule'
Import the module, verify you can see your stuff.
Import-Module ./StuffModule/Stuff.psd1
Get-Module Stuff
Get-StuffPath
dir (Get-StuffPath)
Add a required statement for the new stuff module to MyFirstPSScript\MyFirstPSScript.ps1
.
# PowerShell script file to be executed as a AWS Lambda function.
#
# When executing in Lambda the following variables will be predefined.
# $LambdaInput - A PSObject that contains the Lambda function input data.
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
#
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
#
# To include PowerShell modules with your Lambda function, like the AWS.Tools.S3 module, add a "#Requires" statement
# indicating the module and version. If using an AWS.Tools.* module the AWS.Tools.Common module is also required.
#Requires -Modules @{ModuleName='AWS.Tools.Common';ModuleVersion='4.0.5.0'}
#Requires -Modules @{ModuleName='Stuff';ModuleVersion='0.0.1'}
# Uncomment to send the input event to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
Add a line to the end of MyFirstPSScript\MyFirstPSScript.ps1
to test that you can see all your stuff from the lambda's perspective. I don't know why, but running aws lambda invoke only seems to return the last line of the script's output so I will wrap everything in one line of json using ConvertTo-Json -Compress
.
(dir (Get-StuffPath)).FullName | ConvertTo-Json -Compress
Publish and test the lambda function.
Publish-AWSPowerShellLambda -ScriptPath .\MyFirstPSScript.ps1 -Name MyFirstPSScript -Region us-east-1
aws lambda invoke --function-name MyFirstPSScript out.txt
You should see a list of the fullpath to all the stuff you included in the StuffModule/Stuff
directory.
It's important to import the module from your relative path before you publish the lambda. Otherwise you will get an error.
Saving module Stuff
Save-Package: C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:11794
Line |
11794 | $null = PackageManagement\Save-Package @PSBoundParameters
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| No match was found for the specified search criteria and module name 'Stuff'. Try Get-PSRepository to see all
| available registered module repositories.