尝试调用Go AWS Lambda函数时权限被拒绝

尝试调用Go AWS Lambda函数时权限被拒绝

问题描述:

I've created a AWS Lambda function that I'm using a Webhook to call an API Gateway Below is the code I've built with go build -o main.go since I've been reading that you have to specify the extension.

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/lambda"
)

func HandleRequest(ctx context.Context) (string, error) {
    return fmt.Sprintf("Hello!"), nil
}

func main() {
    lambda.Start(HandleRequest)
}

The issue is even though I have public permissions on my uploaded S3 function .zip as well as role permissions I'm still getting a permissions error.

{
  "errorMessage": "fork/exec /var/task/main: permission denied",
  "errorType": "PathError"
}

我创建了一个AWS Lambda函数,我正在使用Webhook调用 API网关以下是我使用 go build -o main.go code code>构建的代码 >由于我一直在阅读,因此您必须指定扩展名。 p>

 包main 
 
import(
“上下文” 
“ fmt” 
 
“ github.com/aws/aws-lambda-go/lambda”  
)
 
func HandleRequest(ctx context.Context)(字符串,错误){
返回fmt.Sprintf(“ Hello!”),nil 
} 
 
func main(){
 lambda.Start  (HandleRequest)
} 
  code>  pre> 
 
 

即使我有对我上传的” rel =“ nofollow noreferrer”> S3函数.zip 以及角色权限我仍然遇到权限错误。 p>

  {
“ errorMessage”:“ fork / exec / var / task / main:权限被拒绝”,
“ errorType”:“ PathError” 
} 
   code>  pre> 
  div>

You're attempting to run the go source code file. You need to run the binary:

# Build the binary for your module
GOOS=linux go build main.go

# Package the binary, note we're packaging "main", not "main.go" here:
zip function.zip main

# And upload "function.zip" this package to Lambda

For more details, including directions to run through this process on other platforms, see the AWS Lambda Deployment documentations

Also, you'll need to set the executable bit in the zipfile. There are a bunch of ways to do this, if you want to do it on Windows, you'll need to run a python script like this:

import zipfile
import time

def make_info(filename):
    info = zipfile.ZipInfo(filename)
    info.date_time = time.localtime()
    info.external_attr = 0x81ed0000
    info.create_system = 3
    return info

zip_source = zipfile.ZipFile("source_file.zip")
zip_file = zipfile.ZipFile("dest_file.zip", "w", zipfile.ZIP_DEFLATED)

for cur in zip_source.infolist():
    zip_file.writestr(make_info(cur.filename), zip_source.open(cur.filename).read(), zipfile.ZIP_DEFLATED)

zip_file.close()

This will take a source_file.zip and repackage it as dest_file.zip with the same contents, but with the executable bit set for all of the files.