aws-lambda-dotnet
aws-lambda-dotnet copied to clipboard
Can't import Az modules in AWS PowerShell Lambda
Description
AWS PowerShell Lambda does not currently permit the use of any cmdlets/functions from an Azure Az module.
Expectation
#Requires -Modules @{ModuleName='Az.Accounts';ModuleVersion='2.7.2'}
With an Az module added to a PowerShell Lambda - the module should be able to be successfully imported and functions engaged.
Actual Behavior
Importing module ./Modules/Az.Accounts/2.7.2/Az.Accounts.psd1
[Error] - Value cannot be null. (Parameter 'path1')
Reproduction Steps
- Add any Azure Az module to required modules for PowerShell Lambda:
#Requires -Modules @{ModuleName='Az.Accounts';ModuleVersion='2.7.2'}
- Deploy Lambda
- Attempt to run Lambda and observe import of Az module
Logs
When adding the Az module to the #Requires:
An exception was thrown when the constructor for type 'AzModuleProcessor.Bootstrap' was invoked. Check inner exception for more details.: LambdaException
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
Value cannot be null. (Parameter 'path1'): ArgumentNullException
at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor()
at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor(String powerShellScriptFileName)
at AzModuleProcessor.Bootstrap..ctor() in /tmp/LambdaStaging/AzModuleProcessor/Bootstrap.cs:line 9
Importing module ./Modules/Az.Accounts/2.7.2/Az.Accounts.psd1
[Error] - Value cannot be null. (Parameter 'path1')
As a troubleshooting step I also attempted the following from within the Lambda logic code:
- Use
Save-Moduleand download an Az module directly from the PSGallery. - Perform a manual import within the Lambda using
Import-Module. This also does not work and results in the following log entries:
2022-02-23T07:45:36.799-06:00 [Warning] - An error was encountered importing: Az.Accounts
2022-02-23T07:45:36.802-06:00 [Error] - Cannot bind argument to parameter 'Path' because it is an empty string.
Environment
- Build Version:
7.0.8 - OS Info: AWS PowerShell Lambda
- Targeted .NET Platform:
dotnetcore3.1Lambda
Resolution
I think the issue is something with the System.IO.IsolatedStorage as that's the only place I see the "Path1" parameter.
Additional mentions of this issue
- Can't import AZ.Resources in AWS Lambda - Azure/azure-powershell
- Cannot import Azure AZ Modules into PS Script under AWS Lambda .NetCore - StackOverFlow
This is a :bug: bug-report
Hi @techthoughts2,
Good afternoon.
Could you please share the output of Publish-AWSPowerShellLambda command and sample script for deployment that uses Az.Accounts module to reproduce the issue? Also advise on how you come into conclusion that there is an issue with System.IO.IsolatedStorage.
Thanks, Ashish
@ashishdhingra
Sure thing. Attached you can reference the full output log of the Publish command.
publish_awspowershelllambda_logs.txt
The Lambda publishes to the AWS account without issue. It just errors on execution when trying to import any Az module.
The System.IO.IsolatedStorage mention is a quote pulled from the linked Azure/azure-powershell issue.
I wanted to add that I updated the Lambda deployment today to the new release of: .NET 6 runtime for AWS Lambda
This issue persists with the same behavior with the new configuration:
Environment
- Build Version:
7.2.1 - OS Info: AWS PowerShell Lambda
- Targeted .NET Platform:
dotnet6Lambda
Reproducible using the below minimal code (followed instructions at Deploy PowerShell Lambda functions with .zip file archives):
# 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.1.15.0'}
#Requires -Modules @{ModuleName='Az.Accounts';ModuleVersion='2.7.2'}
# Uncomment to send the input event to CloudWatch Logs
Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
$PSVersionTable
This produces the below error in CloudWatch:
2022-02-25T11:49:56.761-08:00 | 2022-02-25T19:49:56.759Z info Importing module ./Modules/Az.Accounts/2.7.2/Az.Accounts.psd1
-- | --
| 2022-02-25T11:49:56.761-08:00 | [Error] - Cannot bind argument to parameter 'Path' because it is an empty string.
| 2022-02-25T11:49:56.761-08:00 | [Error] - The variable '$pathToInstallationChecks' cannot be retrieved because it has not been set.
| 2022-02-25T11:49:56.761-08:00 | [Error] - Value cannot be null. (Parameter 'path1')
| 2022-02-25T11:49:56.770-08:00 | 2022-02-25T19:49:56.770Z fail System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
| 2022-02-25T11:49:56.770-08:00 | ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor()
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor(String powerShellScriptFileName)
| 2022-02-25T11:49:56.770-08:00 | at MyFirstPSScript.Bootstrap..ctor() in /private/var/folders/j5/ny8g7hsj7c7dyj71n14nwmqm0000gr/T/MyFirstPSScript/Bootstrap.cs:line 7
| 2022-02-25T11:49:56.770-08:00 | at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
| 2022-02-25T11:49:56.770-08:00 | --- End of inner exception stack trace ---
| 2022-02-25T11:49:56.770-08:00 | at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
| 2022-02-25T11:49:56.770-08:00 | at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
| 2022-02-25T11:49:56.770-08:00 | at System.Activator.CreateInstance(Type type)
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.GetCustomerObject(Type customerType) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 407
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 122
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
| 2022-02-25T11:49:56.770-08:00 | at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 155
| 2022-02-25T11:49:56.861-08:00 | Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
| 2022-02-25T11:49:56.861-08:00 | ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor()
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.PowerShellHost.PowerShellFunctionHost..ctor(String powerShellScriptFileName)
| 2022-02-25T11:49:56.861-08:00 | at MyFirstPSScript.Bootstrap..ctor() in /private/var/folders/j5/ny8g7hsj7c7dyj71n14nwmqm0000gr/T/MyFirstPSScript/Bootstrap.cs:line 7
| 2022-02-25T11:49:56.861-08:00 | at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
| 2022-02-25T11:49:56.861-08:00 | --- End of inner exception stack trace ---
| 2022-02-25T11:49:56.861-08:00 | at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
| 2022-02-25T11:49:56.861-08:00 | at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
| 2022-02-25T11:49:56.861-08:00 | at System.Activator.CreateInstance(Type type)
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.GetCustomerObject(Type customerType) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 407
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 122
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 155
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 161
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.RunAsync(CancellationToken cancellationToken) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 131
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.RuntimeSupportInitializer.RunLambdaBootstrap() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/RuntimeSupportInitializer.cs:line 49
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.Program.Main(String[] args) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Program.cs:line 19
| 2022-02-25T11:49:56.861-08:00 | at Amazon.Lambda.RuntimeSupport.Program.<Main>(String[] args)
Appears to be a dependency issue Az.Accounts module.
A workaround is to set the HOME environment variable to /tmp in the lambda configuration
@jeremybeavon that saved me! However, I also had to set $env:SkipAzInstallationChecks = 'true' to make it work. Perhaps because I am using Import-Module, not completely sure.