Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import Az modules in AWS PowerShell Lambda #1085

Closed
techthoughts2 opened this issue Feb 23, 2022 · 8 comments
Closed

Can't import Az modules in AWS PowerShell Lambda #1085

techthoughts2 opened this issue Feb 23, 2022 · 8 comments
Labels
bug This issue is a bug. module/custom-runtime p2 This is a standard priority issue queued

Comments

@techthoughts2
Copy link

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

  1. Add any Azure Az module to required modules for PowerShell Lambda:
    • #Requires -Modules @{ModuleName='Az.Accounts';ModuleVersion='2.7.2'}
  2. Deploy Lambda
  3. 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-Module and 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.1 Lambda

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


This is a 🐛 bug-report

@techthoughts2 techthoughts2 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 23, 2022
@ashishdhingra
Copy link
Contributor

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 ashishdhingra added response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 23, 2022
@techthoughts2
Copy link
Author

@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.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. label Feb 24, 2022
@techthoughts2
Copy link
Author

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: dotnet6 Lambda

@ashishdhingra
Copy link
Contributor

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.

@hunanniu hunanniu added the B label Feb 25, 2022
@ashishdhingra ashishdhingra added queued p2 This is a standard priority issue and removed B labels Nov 2, 2022
@jeremybeavon
Copy link

A workaround is to set the HOME environment variable to /tmp in the lambda configuration

@per-oestergaard
Copy link

@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.

@normj
Copy link
Member

normj commented Sep 9, 2024

We can't do specific work for all available PS modules. Users that run into this issue should rely on the workaround @jeremybeavon and @per-oestergaard mentioned.

@normj normj closed this as completed Sep 9, 2024
Copy link
Contributor

github-actions bot commented Sep 9, 2024

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/custom-runtime p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

6 participants