I have a Aws lambda that try to access to S3 bucket to get head object and then their metadata.
I am using Serverless to test locally.
But, when I execute locally the test, i get:
software.amazon.awssdk.core.exception.SdkClientException: Unable to execute HTTP request: Read timed
If I implemented the lambda with node js, then it work fine. But I need to use Java.
Technologies:
- Java8
- software.amazon.awssdk:bom:2.17.103
- software.amazon.awssdk:lambda
- software.amazon.awssdk:s3
- “serverless”: “^2.71.0”
- “serverless-offline”: “^8.4.0”
- “serverless-s3-local”: “^0.6.21”
Environment:
- Local
- I don’t have VPC
Code:
public class FileS3Handler implements RequestHandler<Map<String,String>, String> {
@Override
public String handleRequest(Map<String,String> event, Context context)
{
String response;
URI uri = new URI("http://localhost:3333");
S3Configuration config = S3Configuration.builder()
.pathStyleAccessEnabled(true)
.build();
S3Client s3Client = S3Client.builder()
.endpointOverride(uri)
.serviceConfiguration(config)
.build();
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
.bucket("myBucket")
.key("myFile")
.build();
s3Client.
HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest);
response = "Metadata: "+ headObjectResponse.metadata().toString();
return response;
}
}
Environment Variables:
AWS_ACCESS_KEY_ID=S3RVER
AWS_SECRET_ACCESS_KEY=S3RVER
AWS_REGION=us-west-2
serverless.yml
service: example
provider:
name: aws
region: us-west-2
runtime: java8
lambdaHashingVersion: 20201221
package:
individually: true
functions:
file-handler:
handler: com.example.FileS3Handler
events:
- http:
path: /api/filetest
method: get
package:
artifact: build/example.zip
plugins:
- serverless-offline
- serverless-dotenv-plugin
- serverless-s3-local
custom:
serverless-offline:
httpPort: 4000
s3:
port: 3333
host: localhost
resources:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: myBucket
Test:
- Run: serverless start offline
- Postman → GET http://localhost:4000/dev/api/filetest
Does anyone know what is happening?
Thanks!