Search code examples
phpamazon-web-servicesaws-lambdaphp-curlawsdeploy

Importing cURL module into AWS Lambda


I'm a beginner at using AWS. I'm currently facing a problem importing the PHP cURL module into the Lambda layer.

I try to follow the instruction on GitHub https://github.com/stackery/php-lambda-layer to set up the layer, but I've only revised the arn: arn:aws:lambda:<region>:887080169480:layer:php73:3 to my region. I don't know where to type those commands to add the extension.

I've also followed the online tutorial https://www.youtube.com/watch?v=MipjLaTp5nA&t=800s&ab_channel=BeABetterDev step by step deploying my stack, but it didn't work as well.

Are there any possible instructions for me to followed or any material I could refer to it?

I'm trying to run the following code in Lambda which is triggered by API gateway.

<?php
//------------------------Main--------------------------------------------------
$post = file_get_contents('php://input');
$url = 'https://cinv.ezpay.com.tw/Api/invoice_issue';

$result = curl_work($url, $post);

//------------------------Function----------------------------------------------
function curl_work($url = '', $parameter = '')
{
    $curl_options = array(
    'CURLOPT_URL' => $url,
    'CURLOPT_HEADER' => false,
    'CURLOPT_RETURNTRANSFER' => true,
    'CURLOPT_USERAGENT' => 'ezPay',
    'CURLOPT_FOLLOWLOCATION' => true,
    'CURLOPT_SSL_VERIFYPEER' => false,
    'CURLOPT_SSL_VERIFYHOST' => false,
    'CURLOPT_POST' => '1',
    'CURLOPT_POSTFIELDS' => $parameter
);
    $ch = curl_init();
    curl_setopt_array($ch, $curl_options);
    $result = curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_error = curl_errno($ch);
    curl_close($ch);
    $return_info = array(
        'url' => $url,
        'sent_parameter' => $parameter,
        'http_status' => $retcode,
        'curl_error_no' => $curl_error,
        'web_info' => $result
    );
    return $return_info;
}
?>

I've tested with Postman and got the following error response.

<br />
<b>Fatal error</b>: Uncaught Error: Call to undefined function curl_init() in /var/task/index.php:22
Stack trace:
#0 /var/task/index.php(6): curl_work('https://cinv.ez...', '')
#1 {main}
thrown in <b>/var/task/index.php</b> on line <b>22</b><br />

Solution

  • I've solved this problem. I should add the extension to the php.ini file in the zip.file upload to Lambda.

    extension=json.so
    extension=curl.so
    

    The code should be like the following.

    <?php
    //------------------------Main--------------------------------------------------
    
    $post = file_get_contents('php://input');
    $url = 'https://cinv.ezpay.com.tw/Api/invoice_issue';
    
    $array = json_decode($post, true);
    $PostData = $array['PostData_'];
        
    $result = curl_work($url, $PostData);
    
    echo "Success";
    
    //------------------------Function----------------------------------------------
    function curl_work($url = '', $parameter = '')
    {
        $curl_options = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_USERAGENT => 'ezPay',
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_POST => '1',
            CURLOPT_POSTFIELDS => $parameter
        );
        $ch = curl_init();
        curl_setopt_array($ch, $curl_options);
        $result = curl_exec($ch);
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_error = curl_errno($ch);
        curl_close($ch);
        $return_info = array(
            'url' => $url,
            'sent_parameter' => $parameter,
            'http_status' => $retcode,
            'curl_error_no' => $curl_error,
            'web_info' => $result
        );
        return $return_info;
    }
    ?>