AWS Lambda PHP Create Function with Zip

2.6k views Asked by At

I'm trying to create a PHP script that creates a function from some code that i zip up on our server. I uploaded the file manually to lambda, and it works fine. But when i try to use the aws sdk to create the function, it fails with an error message. Anyone got any clue?

Code:

private function createLambdaFunction() {

    $result = $this->lambdaConn->createFunction(array(
        'FunctionName' => $this->lambdaFunctionName,
        'Runtime' => $this->runtime,
        'Role' => $this->role,
        'Handler' => $this->lambdaFunctionName.".".$this->handler,
        'Description' => $this->description,
        'Timeout' => $this->timeout,
        'MemorySize' => $this->memorySize,
        'Code' => array(
            'ZipFile' => 'fileb://test.zip'
        )
    ));

Error:

PHP Fatal error:  Uncaught Aws\Lambda\Exception\LambdaException: AWS 
Error Code: InvalidParameterValueException, 
Status Code: 400, AWS Request ID: asdf, AWS Error Type: user, 
AWS Error Message: Could not unzip uploaded file. Please check 
your file, then try to upload again., User-Agent: 
aws-sdk-php2/2.8.10 Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.9

I can't seem to find a good example on google, and the documentation is...less than ideal. I created the zip file with php, so I've tried passing that var, the full path to the file, relative path to file, etc. Finally learned you have to use fileb:// preface, but that didn't end up fixing anything.

1

There are 1 answers

2
Fooldj On BEST ANSWER

Okay, I'm not sure why this is the case, but you need to base64 encode your zip file like:

            $result = $this->lambdaConn->createFunction(array(
            'FunctionName' => $this->lambdaFunctionName,
            'Runtime' => $this->runtime,
            'Role' => $this->role,
            'Handler' => $this->lambdaFunctionName . "." . $this->handler,
            'Description' => $this->description,
            'Timeout' => $this->timeout,
            'MemorySize' => $this->memorySize,
            'Code' => array(
                'ZipFile' => 'fileb://'.base64_encode(file_get_contents('test.zip'))
            )
        ));

I'm not sure why this is required, as accourding to the doumentation and a post by an AWS employee, you dont have to have base64 encoding for create function. They must have mixed up something or another.