By using AWS re:Post, you agree to the Terms of Use

Questions tagged with AWS Lambda

Sort by most recent

Browse through the questions and answers listed below or filter and sort to narrow down your results.

How to upload dynamically generated file to S3 bucket using lambda with PHP?

Using [bref][1] as PHP serverless framework. Problem: - Unable to upload the dynamically generated file to S3 bucket from Lambda using PHP. In CloudWatch, there is no any error. Code: - serverless.yml ``` service: lambdaToS3 provider: name: aws region: ap-southeast-2 runtime: provided.al2 #stage: prod profile: default iam: role: statements: - Effect: Allow Action: - 's3:GetObject' - 's3:PutObject' - 's3:GetObjectAcl' - 's3:PutObjectAcl' Resource: - 'arn:aws:s3:::*/*' plugins: - ./vendor/bref/bref functions: lambdaToS3: handler: index.php description: '' layers: - ${bref:layer.php-74} events: - httpApi: '*' # Exclude files from deployment package: patterns: - '!tests/**' - '!tmp/**' ``` index.php `comments are output when executing a file using HTTP GateWay` In this file, I am generating a simple text file in `/tmp/` folder and trying to upload it to the S3 bucket with two different attributes, `SourceFile` or `Body`. None of them working. ``` <?php declare(strict_types=1); ini_set('display_errors', "1"); ini_set('display_startup_errors', "1"); error_reporting(E_ALL); require __DIR__ . '/vendor/autoload.php'; use Aws\S3\S3Client; use Bref\Logger\StderrLogger; $logger = new StderrLogger(); $s3client = new S3Client([ 'scheme' => 'http', 'version' => '2006-03-01', 'region' => 'ap-southeast-2', 'output' => 'JSON' ]); $bucket = 'bucketname'; $file = "/tmp/newfile.txt"; $myfile = fopen($file, "w") or die("Unable to open file!"); $txt = "John Doe\n"; fwrite($myfile, $txt); $txt = "Jane Doe\n"; fwrite($myfile, $txt); fclose($myfile); echo "<pre>"; print_r("File Exist: ". file_exists($file)); echo "</pre>"; // 1 echo "<pre>"; print_r(" ================ "); echo "</pre>"; echo "<pre>"; print_r(file_get_contents($file)); echo "</pre>"; // we can get content using this method. echo "<pre>"; print_r(" ================ "); echo "</pre>"; // Put on S3 using SourceFile $result = $s3client->putObject([ 'Bucket' => $bucket, 'Key' => "AWS_LAMBDA_S3.txt", 'SourceFile' => $file, 'ACL' => 'public-read', ]); echo "<pre>"; print_r("File Upload: " . json_encode($result)); echo "</pre>"; // Put on S3 using Body $myfile = fopen($file, "rb"); $result = $s3client->putObject([ 'Bucket' => $bucket, 'Key' => "AWS_LAMBDA_S3_BODY.txt", 'Body' => $myfile, 'ACL' => 'public-read', ]); echo "<pre>"; print_r("File Upload: " . json_encode($result)); echo "</pre>"; exit; ``` Regarding policy: Once the function deploy to AWS Lambda, we got a following policy for the role ``` { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "logs:CreateLogStream", "logs:CreateLogGroup" ], "Resource": "arn:aws:logs:ap-southeast-2:xxxx:log-group:/aws/lambda/lambdaToS3-dev*:*" }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObjectAcl", "s3:PutObjectAcl" ], "Resource": "*" }, { "Sid": "VisualEditor2", "Effect": "Allow", "Action": [ "sqs:DeleteMessage", "sqs:ReceiveMessage", "sqs:GetQueueAttributes", "logs:PutLogEvents" ], "Resource": [ "arn:aws:logs:ap-southeast-2:xxxx:log-group:/aws/lambda/lambdaToS3-dev*:*:*", "arn:aws:sqs:ap-southeast-2:xxxx:xxxx" ] } ] } ``` [1]: https://bref.sh
0
answers
0
votes
17
views
asked 7 days ago

AWS lambda function not able to resolve or connect to ipv6 only domain

I implemented a AWS lambda function which shall pass an Alexa custom skill event to my domain for processing, see code below. const https = require('https'); exports.handler = (event, context, callback) => { var options = { hostname: '<my.domain.com>', path: '/<mypath>', port: 443, method: 'POST', rejectUnauthorized: false, headers: { 'Content-Type': 'application/json', 'Authorization': '<my base64 user:password>' } }; const req = https.request(options, (res) => { let body = ''; console.log('Status:', res.statusCode); console.log('Headers:', JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', (chunk) => { body += chunk; }); res.on('end', () => { console.log('Successfully processed HTTPS response'); body = JSON.parse(body); callback(null, body); }); }); req.on('error', callback); req.write(JSON.stringify(event)); req.end(); }; The function runs serverless, not connected to a VPC. The domain <my.domain.com> resolves to an IPv6 address and I am able to connect to my host for example from an internet instance using curl and receive the expected answers. curl -i -k -v -X POST -d testcase.json -u <user:password> https://<my.domain.com>:<my port>/<my path> In AWS I implemented a test case and run it. The test returned the error ENOTFOUND from function getaddrinfo trying to resolve my domain, see execution result below. Test Event Name Test0001 Response { "errorType": "Error", "errorMessage": "getaddrinfo ENOTFOUND <my.domain.com>", "trace": [ "Error: getaddrinfo ENOTFOUND <my.domain.com>", " at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)" ] } Function Logs LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform] EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [SHUTDOWN,INVOKE] START RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 Version: $LATEST 2022-09-24T04:59:06.966Z 78314f37-e991-4d3d-b4f2-03da64bf91b7 ERROR Invoke Error {"errorType":"Error","errorMessage":"getaddrinfo ENOTFOUND <my.domain.com>","code":"ENOTFOUND","errno":-3008,"syscall":"getaddrinfo","hostname":"<my.domain.com>","stack":["Error: getaddrinfo ENOTFOUND <my.domain.com>"," at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)"]} END RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 REPORT RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 Duration: 425.43 ms Billed Duration: 426 ms Memory Size: 128 MB Max Memory Used: 76 MB Init Duration: 248.14 ms During my investigation I found the hint to add option „family: 6,“. Using this option the test case resolves the domain now to the correct ipv6 address, but returns then EAFNOSUPPORT trying to connect to the address, see execution result below. Request ID 78314f37-e991-4d3d-b4f2-03da64bf91b7 Test Event Name Test0001 Response { "errorType": "Error", "errorMessage": "connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)", "trace": [ "Error: connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)", " at internalConnect (node:net:953:16)", " at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)", " at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1097:9)", " at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8)" ] } Function Logs LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform] EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN] START RequestId: f3493148-071f-466d-94c7-d29a0d715640 Version: $LATEST 2022-09-24T05:06:52.877Z f3493148-071f-466d-94c7-d29a0d715640 ERROR Invoke Error {"errorType":"Error","errorMessage":"connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)","code":"EAFNOSUPPORT","errno":-97,"syscall":"connect","address":"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx","port":443,"stack":["Error: connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)"," at internalConnect (node:net:953:16)"," at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)"," at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1097:9)"," at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8)"]} END RequestId: f3493148-071f-466d-94c7-d29a0d715640 REPORT RequestId: f3493148-071f-466d-94c7-d29a0d715640 Duration: 447.45 ms Billed Duration: 448 ms Memory Size: 128 MB Max Memory Used: 76 MB Init Duration: 231.52 ms Request ID f3493148-071f-466d-94c7-d29a0d715640 Any further investigation was not successful. I assume it is an issue using IPv6, but I am not able to solve it. Any help is appreciated. Thank you in advance. Joachim
2
answers
0
votes
25
views
asked 10 days ago