From 6a355cf25edbdc0777cfe0977810ed89db5db39e Mon Sep 17 00:00:00 2001 From: Jonathan Nunn Date: Mon, 12 Oct 2026 23:25:54 +0000 Subject: [PATCH 001/294] LambdaPSRuntime (1/8): Module manifest, config, build script, test helpers, and shared AWS-call helpers Part 1 of 8 of the stacked pull requests for AWS.ToolExtras.LambdaPSRuntime. --- LambdaPSCustomRuntime/.gitattributes | 5 + LambdaPSCustomRuntime/.gitignore | 13 + .../PSScriptAnalyzerSettings.Relaxed.psd1 | 32 ++ .../PSScriptAnalyzerSettings.psd1 | 37 +++ LambdaPSCustomRuntime/README.md | 286 ++++++++++++++++++ LambdaPSCustomRuntime/build.ps1 | 260 ++++++++++++++++ .../AWS.ToolExtras.LambdaPSRuntime.psd1 | 60 ++++ .../AWS.ToolExtras.LambdaPSRuntime.psm1 | 69 +++++ .../Config/function.psd1 | 13 + .../Config/pwsh.psd1 | 43 +++ .../Config/wait.psd1 | 16 + .../Private/Get-LambdaPSAWSToolsRegion.ps1 | 41 +++ .../Private/Get-LambdaPSClientConfig.ps1 | 46 +++ .../Get-LambdaPSCredentialArgumentText.ps1 | 38 +++ .../Get-LambdaPSCredentialParameter.ps1 | 68 +++++ .../Private/Get-LambdaPSPartition.ps1 | 38 +++ .../Private/Get-LambdaPSSdkFallbackRegion.ps1 | 31 ++ .../Private/Get-LambdaPSTarCommand.ps1 | 38 +++ .../Private/Invoke-LambdaPSExternalTool.ps1 | 65 ++++ .../Private/New-LambdaPSFinding.ps1 | 54 ++++ .../Private/Resolve-LambdaPSPath.ps1 | 55 ++++ .../Private/Resolve-LambdaPSRegion.ps1 | 74 +++++ .../Private/Test-LambdaPSServiceError.ps1 | 55 ++++ .../Tests/Component.BuildScript.Tests.ps1 | 176 +++++++++++ .../Tests/Component.ModuleImport.Tests.ps1 | 136 +++++++++ .../Tests/Component.TestHelpers.Tests.ps1 | 96 ++++++ .../Tests/Helpers/DockerStub.ps1 | 18 ++ .../Tests/Helpers/FakeModule.ps1 | 15 + .../Tests/Helpers/FakeRuntimeApi.ps1 | 165 ++++++++++ .../Tests/Helpers/FakeTypes.ps1 | 73 +++++ .../Tests/Helpers/FreshRunspace.ps1 | 104 +++++++ .../Tests/Helpers/Module.ps1 | 13 + .../Tests/Helpers/PayloadStream.ps1 | 6 + .../Tests/Helpers/PwshConfig.ps1 | 4 + .../Tests/Helpers/Zip.ps1 | 45 +++ ...ivate.Get-LambdaPSAWSToolsRegion.Tests.ps1 | 44 +++ ...Private.Get-LambdaPSClientConfig.Tests.ps1 | 55 ++++ ...t-LambdaPSCredentialArgumentText.Tests.ps1 | 23 ++ ....Get-LambdaPSCredentialParameter.Tests.ps1 | 153 ++++++++++ ...it.Private.Get-LambdaPSPartition.Tests.ps1 | 22 ++ ...te.Get-LambdaPSSdkFallbackRegion.Tests.ps1 | 28 ++ ...t.Private.Get-LambdaPSTarCommand.Tests.ps1 | 47 +++ ...vate.Invoke-LambdaPSExternalTool.Tests.ps1 | 112 +++++++ ...Unit.Private.New-LambdaPSFinding.Tests.ps1 | 35 +++ ...nit.Private.Resolve-LambdaPSPath.Tests.ps1 | 90 ++++++ ...t.Private.Resolve-LambdaPSRegion.Tests.ps1 | 205 +++++++++++++ ...rivate.Test-LambdaPSServiceError.Tests.ps1 | 41 +++ 47 files changed, 3143 insertions(+) create mode 100644 LambdaPSCustomRuntime/.gitattributes create mode 100644 LambdaPSCustomRuntime/.gitignore create mode 100644 LambdaPSCustomRuntime/PSScriptAnalyzerSettings.Relaxed.psd1 create mode 100644 LambdaPSCustomRuntime/PSScriptAnalyzerSettings.psd1 create mode 100644 LambdaPSCustomRuntime/README.md create mode 100644 LambdaPSCustomRuntime/build.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/AWS.ToolExtras.LambdaPSRuntime.psd1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/AWS.ToolExtras.LambdaPSRuntime.psm1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Config/function.psd1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Config/pwsh.psd1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Config/wait.psd1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSAWSToolsRegion.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSClientConfig.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSCredentialArgumentText.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSCredentialParameter.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSPartition.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSSdkFallbackRegion.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Get-LambdaPSTarCommand.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Invoke-LambdaPSExternalTool.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/New-LambdaPSFinding.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Resolve-LambdaPSPath.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Resolve-LambdaPSRegion.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Private/Test-LambdaPSServiceError.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Component.BuildScript.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Component.ModuleImport.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Component.TestHelpers.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/DockerStub.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/FakeModule.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/FakeRuntimeApi.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/FakeTypes.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/FreshRunspace.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/Module.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/PayloadStream.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/PwshConfig.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Helpers/Zip.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSAWSToolsRegion.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSClientConfig.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSCredentialArgumentText.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSCredentialParameter.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSPartition.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSSdkFallbackRegion.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Get-LambdaPSTarCommand.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Invoke-LambdaPSExternalTool.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.New-LambdaPSFinding.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Resolve-LambdaPSPath.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Resolve-LambdaPSRegion.Tests.ps1 create mode 100644 LambdaPSCustomRuntime/src/AWS.ToolExtras.LambdaPSRuntime/Tests/Unit.Private.Test-LambdaPSServiceError.Tests.ps1 diff --git a/LambdaPSCustomRuntime/.gitattributes b/LambdaPSCustomRuntime/.gitattributes new file mode 100644 index 000000000..4f932d5d5 --- /dev/null +++ b/LambdaPSCustomRuntime/.gitattributes @@ -0,0 +1,5 @@ +# Everything under Templates/ is copied into a Linux Lambda runtime or image. The bootstraps are +# executed directly by the kernel: with CRLF line endings the shebang reads '/bin/sh\r' and the runtime +# fails to start with "bad interpreter". Force LF on checkout regardless of core.autocrlf, so a Windows +# clone scaffolds working projects. +src/AWS.ToolExtras.LambdaPSRuntime/Templates/** text eol=lf diff --git a/LambdaPSCustomRuntime/.gitignore b/LambdaPSCustomRuntime/.gitignore new file mode 100644 index 000000000..34bd005ba --- /dev/null +++ b/LambdaPSCustomRuntime/.gitignore @@ -0,0 +1,13 @@ +# PowerShell / .NET build and test artifacts +bin/ +obj/ +*.zip +coverage.xml +testResults*.xml +pester*.xml + +# Editor / OS +.vs/ +.vscode/ +*.user +.DS_Store diff --git a/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.Relaxed.psd1 b/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.Relaxed.psd1 new file mode 100644 index 000000000..43b97c132 --- /dev/null +++ b/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.Relaxed.psd1 @@ -0,0 +1,32 @@ +# PSScriptAnalyzer contract for Tests/ and Templates/ -- code that is NOT shipping cmdlet source and +# is legitimately written to different rules. `build.ps1 -Task Lint` reports findings from this scope +# but does not fail on them (it still fails on Error severity). +# +# Each exclusion below is excluded because the pattern it flags is CORRECT in these directories. None +# of these rules is excluded for the module source, which is linted with the strict +# PSScriptAnalyzerSettings.psd1 instead. +@{ + IncludeDefaultRules = $true + + Severity = @( + 'Error' + 'Warning' + 'Information' + ) + + ExcludeRules = @( + # Templates/: the runtime (runtime.ps1 and the runtime interface client modules) logs to the + # console on purpose. Lambda captures stdout/stderr into CloudWatch Logs, and Write-Host is how + # the runtime reports progress before any structured logging exists. (All in Templates/. The + # cmdlet source has none, and the strict settings keep it that way.) + 'PSAvoidUsingWriteHost' + + # Tests/: many tests declare a stub such as `function New-LMCRuntime { param($Name, $Region, ...) }` + # purely to give Pester a command with the right parameter names to mock. The parameters are + # unused BY DESIGN -- the stub body is empty and the mock replaces it. (All in Tests/.) + 'PSReviewUnusedParameter' + + # Tests/ and Templates/: plural nouns in helper names that are not exported cmdlets. + 'PSUseSingularNouns' + ) +} diff --git a/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.psd1 b/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.psd1 new file mode 100644 index 000000000..0ce47cf54 --- /dev/null +++ b/LambdaPSCustomRuntime/PSScriptAnalyzerSettings.psd1 @@ -0,0 +1,37 @@ +# PSScriptAnalyzer contract for the MODULE SOURCE (Public/, Private/, Config/, the .psm1 and .psd1). +# +# This scope is held to the full default rule set with NO rule exclusions, and `build.ps1 -Task Lint` +# fails on ANY finding here, at any severity. The source is kept at zero findings, and the gate is +# worth keeping: a rule excluded here would be excluded everywhere in the +# shipping cmdlet code. +# +# Findings that are genuinely wrong are suppressed AT THE SITE with a +# [Diagnostics.CodeAnalysis.SuppressMessageAttribute] carrying a Justification, so the reason lives +# next to the code a reviewer is reading. The suppressed rules, and why (each site's Justification +# says which case it is): +# +# PSUseOutputTypeCorrectly A comma-wrapped return (`return , ([string[]]$x)`) emits one +# object whose runtime type is the declared one, or a collected +# @(...) is unrolled on emit; the rule infers Object[] from the +# static shape. +# PSShouldProcess The scaffolders declare SupportsShouldProcess and leave every +# write to helpers that gate themselves. +# PSUseShouldProcessForState- New-* helpers that only build an in-memory object or +# ChangingFunctions ErrorRecord change no state. +# PSUsePSCredentialType, -Credential takes an AWS.Tools AWSCredentials object, as on every +# PSAvoidUsingPlainTextForPassword AWS.Tools cmdlet, not a PSCredential or a password string. +# +# `grep -rn SuppressMessageAttribute src/` lists every site. +# +# Tests/ and Templates/ are NOT linted with this file -- they are different contexts with their own +# legitimate patterns (mockable stub signatures, bootstrap console logging). See +# PSScriptAnalyzerSettings.Relaxed.psd1. +@{ + IncludeDefaultRules = $true + + Severity = @( + 'Error' + 'Warning' + 'Information' + ) +} diff --git a/LambdaPSCustomRuntime/README.md b/LambdaPSCustomRuntime/README.md new file mode 100644 index 000000000..96d4da206 --- /dev/null +++ b/LambdaPSCustomRuntime/README.md @@ -0,0 +1,286 @@ +# AWS.ToolExtras.LambdaPSRuntime + +Value-add PowerShell tooling for running PowerShell on AWS Lambda via a **customer-managed runtime** +or a **custom runtime**. It scaffolds, validates, builds, and packages runtime and function projects, +and offers one-command deployments for the common case. + +AWS Lambda has no managed PowerShell runtime, so PowerShell brings its own: a native `pwsh` +interpreter plus a bootstrap implementing the Lambda Runtime API loop. That runtime ships as a +customer-managed runtime (a container image that functions bind to by alias), as a custom runtime on +`provided.al2023` (a layer, or bundled into the function's .zip), or inside a function's own container +image. This module automates building and deploying it. It lives deliberately outside the +`AWS.Tools.*` family so it is not tied to that family's unified-versioning lockstep. + +## Requirements + +- PowerShell 7.4+ +- For the container-based approaches (CMR, container-per-function): **Docker with `buildx`** on + `PATH`. Docker is the only supported container CLI — the image build depends on + `--provenance=false` and `oci-mediatypes=false` to emit the Docker v2 Schema 2 manifest a + customer-managed runtime requires, and those have no equivalent in Podman, Finch, or nerdctl. + `Test-LambdaPSContainerRuntime` preflights both the CLI and the plugin. +- The container-free approaches (Layer, self-contained `.zip`) need no container runtime at all — + that is the path for Docker-averse environments. +- AWS.Tools 5.0.1001 or later (`AWS.Tools.Lambda`, `AWS.Tools.LambdaCore`, `AWS.Tools.ECR`, + `AWS.Tools.SecurityToken`), with an `AWS.Tools.LambdaCore` that exports the Lambda + customer-managed runtime cmdlets (`New-LMCRuntime`, `New-LMCRuntimeVersion`, + `New-LMCRuntimeAlias`, …) and an `AWS.Tools.Lambda` whose function cmdlets accept the runtime + binding (`Publish-LMFunction -RuntimeConfig_RuntimeArn -RuntimeConfig_RuntimeAlias`). + +## Concepts + +The runtime can be packaged four ways; they differ only in packaging, each a scaffolding template +with its own lint rules: + +| Approach | Scaffold with | `pwsh` | Container | One-command deploy | +| --- | --- | --- | --- | --- | +| **CMR** (default) | `New-LambdaPSRuntimeProject -Target CMR` | installed in image | Yes | Yes | +| Layer | `New-LambdaPSRuntimeProject -Target Layer` | bundled on `provided.al2023` | No | No | +| Self-contained `.zip` | `New-LambdaPSFunctionProject -Target SelfContainedZip` | bundled on `provided.al2023` | No | No | +| Container-per-function | `New-LambdaPSFunctionProject -Target Container` | installed in image | Yes | No | + +CMR (customer-managed runtime) is the default: publish the runtime once as a container image, +reference it by alias, and roll the whole fleet forward by moving the alias — no per-function +redeploy. Each PowerShell line (7.4, 7.5, 7.6) installs a pinned pwsh release by default; the +releases and their tarball SHA256s are listed in `Config/pwsh.psd1`, and `-PwshVersion` picks +another. The container-free targets run in invariant-globalization mode +(`DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1`) so no ICU libraries are needed. + +The Layer and self-contained `.zip` packages bundle pwsh, so they are large: about 72 MB zipped and +174 MB unzipped for PowerShell 7.4 on x86_64, before any modules. That is over Lambda's direct-upload +limit, so upload the `.zip` to S3 and pass `-Code_S3Bucket`/`-Code_S3Key` (`-Content_S3Bucket`/ +`-Content_S3Key` for `Publish-LMLayerVersion`). The 250 MB unzipped limit leaves roughly 75 MB for modules and code. + +## Quick start (high-level) + +Deploy the runtime once, then deploy functions bound to its alias. + +```powershell +Import-Module ./src/AWS.ToolExtras.LambdaPSRuntime/AWS.ToolExtras.LambdaPSRuntime.psd1 + +# 1. Deploy the runtime (preflight container CLI -> scaffold -> build/push image -> +# publish version -> point the 'production' alias at it). +New-LambdaPSRuntimeDeployment -RuntimeName my-powershell-runtime -PowerShellVersion 7.4 ` + -IncludeModule @( + @{ ModuleName = 'AWS.Tools.S3'; RequiredVersion = '5.0.274' }, + @{ ModuleName = 'AWS.Tools.DynamoDBv2'; RequiredVersion = '5.0.274' } + ) -Region us-east-1 + +# 2. Deploy a function bound to the runtime alias (resolves the runtime ARN for you). +New-LambdaPSFunctionDeployment -FunctionName hello-powershell -ProjectPath ./handler.ps1 ` + -RuntimeName my-powershell-runtime -RuntimeAlias production ` + -Role arn:aws:iam::111122223333:role/hello-powershell-role -Region us-east-1 + +# 3. Invoke it. +Invoke-LambdaPSFunction -FunctionName hello-powershell -Payload @{ name = 'world' } -Region us-east-1 ` + -IncludeLog -InformationAction Continue +``` + +The three cmdlets that call AWS take `-ProfileName`, `-ProfileLocation` and `-Credential` as any +AWS.Tools cmdlet does, except that a relative `-ProfileLocation` is resolved against the current +PowerShell location. Without them, AWS.Tools uses its default credentials (`Set-AWSCredential`, +environment variables, the default profile, or an instance or container role). `-Region` is optional +the same way: without it, the region comes from `Set-DefaultAWSRegion`, the `-ProfileName` profile, the +default profile, `AWS_REGION`, the `AWS_PROFILE` profile, or the instance or container, in that order, +and a deploy that finds none fails with `RegionNotResolved`. For `New-LambdaPSFunctionDeployment` and +`Invoke-LambdaPSFunction`, a full function ARN in `-FunctionName` supplies the region when `-Region` is +omitted, and a `-Region` that differs from the ARN's fails with `FunctionRegionMismatch`. + +A handler declares `param($LambdaInput, $LambdaContext)` — the runtime passes the parsed event and the +runtime details **positionally**, so a script without that param block silently sees `$null` for both. +Whatever the handler leaves on the pipeline is returned as the response. `-Handler` takes one of three +forms: `