awslabs / aws-lambda-powershell-runtime

This new PowerShell custom runtime for AWS Lambda makes it even easier to run Lambda functions written in PowerShell to process events.
Apache License 2.0
57 stars 17 forks source link

Error running deployed lambda #8

Closed asherber closed 1 year ago

asherber commented 1 year ago

Following these instructions, I built the demo with build-layers.ps1 and deployed it with sam deploy -g. Everything appeared fine, but when I tested the function from the AWS console, I got:

Response
{
  "errorType": "Runtime.InvalidEntrypoint",
  "errorMessage": "RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]"
}

Function Logs
START RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Version: $LATEST
RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]
Runtime.InvalidEntrypoint
END RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8
REPORT RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8  Duration: 1.01 ms   Billed Duration: 2 ms   Memory Size: 1024 MB    Max Memory Used: 2 MB   
XRAY TraceId: 1-63b71eaa-3c6d1c9659686d725e011aa4   SegmentId: 20d87356108e2d62 Sampled: true

Is something incorrect with the demo code?

svitty commented 1 year ago

Following these instructions, I built the demo with build-layers.ps1 and deployed it with sam deploy -g. Everything appeared fine, but when I tested the function from the AWS console, I got:

Response
{
  "errorType": "Runtime.InvalidEntrypoint",
  "errorMessage": "RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]"
}

Function Logs
START RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Version: $LATEST
RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]
Runtime.InvalidEntrypoint
END RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8
REPORT RequestId: 0193270a-1347-4d96-80f3-b4f659b60db8    Duration: 1.01 ms   Billed Duration: 2 ms   Memory Size: 1024 MB    Max Memory Used: 2 MB   
XRAY TraceId: 1-63b71eaa-3c6d1c9659686d725e011aa4 SegmentId: 20d87356108e2d62 Sampled: true

Is something incorrect with the demo code?

I think its one of the January commits, I switched to commit "e7b193492bc8bca5d7a8ef91a9f397a4aa2aa868" and it works fine.

austoonz commented 1 year ago

This fails for me as well. In fact, I personally have not used the PowerShell build script before, having only ever built this using Docker on my Mac.

Looking at the documentation for AWS::Serverless::LayerVersion - ContentUri, it states:

If a path to a local folder is provided, for the content to be transformed properly the template must go through the workflow that includes sam build followed by either sam deploy or sam package. By default, relative paths are resolved with respect to the AWS SAM template's location.

Given the documentation, I'm not sure how this ever worked (I've only ever used SAM to build the layers in my usage of this).

@julianwood - did you ever use the PowerShell build script entirely without any usage of sam build in your local environment that may have messed up the testing with PowerShell?

fv-ian commented 1 year ago

I've got it working now with a couple changes to the demo-runtime-layer example. The only part I don't have working yet is the script by itself.

To fix (I'll eventually get an PR going for this)

1: For handler of "Module::examplemodule::examplehandler" -> Move the examplemodule into a subdirectory called "modules" 2: the sam deploy -g doesn't make the layers right a: pwsh-runtime should be created out of the powershell-runtime/pwsh-runtime folder (make a zip where that's the root) b: aws tools layer needs to be made where the modules directory is a directory in the zip file. that way the modules can be found properly.

julianwood commented 1 year ago

Thanks for the update, I'm travelling this week so will have a look next week. I'm pretty sure the script was working at some stage, would have been bad of me if it wasn't when published!

fv-ian commented 1 year ago

@asherber @svitty if you can pull this branch down and give it a test for the two handler calling methods that'd be appreciated. the handler for just a script is still erroring for me with

{
  "errorMessage": "Cannot bind argument to parameter 'Message' because it is null.",
  "errorType": "ParameterBindingValidationException"
}
julianwood commented 1 year ago

I've amended the build process which hopefully fixes this problem. I'd appreciate you trying it with your handler.

fv-ian commented 1 year ago

Latest build didn't work. I deleted all the stacks, all the layers, all the things. Deployed using sam deploy -g with a clean config.

handler: examplehandler.ps1::handler

{
  "errorType": "CommandNotFoundException",
  "errorMessage": "The term 'Get-AWSRegion' is not recognized as a name of a cmdlet, function, script file, or executable program.\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again."
}

   3 |  Import-Module "AWS.Tools.Common"
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The specified module 'AWS.Tools.Common' was not loaded because
     | no valid module file was found in any module directory.

handler: examplescript.ps1

{
  "errorType": "ParameterBindingValidationException",
  "errorMessage": "Cannot bind argument to parameter 'Message' because it is null."
}
julianwood commented 1 year ago

Looks like the layer isn't built correctly. Are you building the layer with https://github.com/awslabs/aws-lambda-powershell-runtime/blob/main/powershell-modules/AWSToolsforPowerShell/Demo-AWS.Tools/build-AWSToolsLayer.ps1 for example, or using SAM? If you're building with the PowerShell script, after the build process, what do you have in the https://github.com/awslabs/aws-lambda-powershell-runtime/tree/main/powershell-modules/AWSToolsforPowerShell/Demo-AWS.Tools/buildlayer folder? You should have a modules folder? This is the folder that is zipped up either manually, or via SAM and makes the layer.

If you're just trying to get it working, try the instructions in: https://github.com/awslabs/aws-lambda-powershell-runtime/tree/main/examples/demo-runtime-layer-function

This builds everything in one step

julianwood commented 1 year ago

Another way to check is to download the layer from the Lambda console, and unzip it. You should have a modules folder and then the tools underneath that.

Also, worth confirming the layer is actually added to the function.

fv-ian commented 1 year ago

Deleted everything. Nuked my fork off my local system. Nuked the CF stacks and made sure all layers, functions were gone.

Made sure my fork main is synced with yours. Re cloned it back out.

cd aws-lambda-powershell-runtime
./build-pwshRuntimeLayer.ps1

sam deploy -g

Then went to the demo directory and built and deployed it.

cd ../examples/demo-runtime-layer-function
./build-layers.ps1

sam deploy -g

Same issue. Downloaded the DemoAWSToolsLayer.

Directory ree is

template.yml
build-AWSToolsLayer.ps1
buildlayer\

Inside of buildlayer is the modules tree.

fv-ian commented 1 year ago

Also looks like the new updates remove files from the wrong places and messes up git.

        deleted:    source/modules/Private/Get-Handler.ps1
        deleted:    source/modules/Private/Get-LambdaNextInvocation.ps1
        deleted:    source/modules/Private/Invoke-FunctionHandler.ps1
        deleted:    source/modules/Private/Send-FunctionHandlerError.ps1
        deleted:    source/modules/Private/Send-FunctionHandlerResponse.ps1
        deleted:    source/modules/Private/Set-HandlerEnvironmentVariables.ps1
        deleted:    source/modules/Private/Set-LambdaContext.ps1
        deleted:    source/modules/Private/Set-PSModulePath.ps1
        modified:   source/modules/pwsh-runtime.psm1
julianwood commented 1 year ago

Thanks, I've found the issue, https://github.com/awslabs/aws-lambda-powershell-runtime/commit/13f928014a6b469f0fce4cdf62ee4930051ea123 should resolve. There was a missing folder in the demo template file for SAM.

Also, if you're using SAM, it may be easier to just use sam build or sam build --use-container but the PowerShell build process should now work.

Additionality, you also don't have to build the Powershell runtime twice. This isn't required.

cd aws-lambda-powershell-runtime
./build-pwshRuntimeLayer.ps1
sam deploy -g

The demo builds the runtime and the layer, you can see in: https://github.com/awslabs/aws-lambda-powershell-runtime/blob/main/examples/demo-runtime-layer-function/build-layers.ps1

cd ../examples/demo-runtime-layer-function
./build-layers.ps1

sam deploy -g

Let me know how you get on, apologies for the missing folders, I had amended the individual layer build files, and missed the overall examples.

notxt commented 1 year ago

@julianwood I'm also getting this error with a fresh clone

image

when I download the Runtime layer it looks like

image

build using Docker on Mac ... not sure where exactly it's going wrong 🤔

julianwood commented 1 year ago

How are you building the layer, are you using sam or the PowerShell scripts? The bootstrap file looks like its in the right place. Can you please confirm that the layer is actually added to the function. Also worth checking whether bootstrap is executable.

notxt commented 1 year ago

@julianwood thanks for the quick response! ✨

I was building using

sam build --parallel --use-container

and deploying like:

sam deploy \
    --capabilities CAPABILITY_IAM \
    --s3-bucket "" \
    --s3-prefix "" \
    --stack-name "" \
    --tags Env="$env" \
    --template-file template.yml

executed from examples/demo-runtime-layer-function

seems like it can't find the layer which is odd because it's for sure there ... ah crud was gonna grab a screenshot but tore down the stack

I ended up using an Win EC2 with an API instead but would love to move the workload to lambda if at all possible so will spin it back up and verify this weekend

but I'm pretty much 100% sure both layers were there

notxt commented 1 year ago

@julianwood ah, snap, bootstrap looking real suspicious

image

need to wrap up a couple things and then will try a quick chmod and let you know if that was the issue

julianwood commented 1 year ago

Strange, doesn't look executable. I've updated the source file to explicitly set it, however the makefile does the same process. Can you please try again.