AWS Java SDK - Region Cannot Find Through Region Provider Network

I looked at the question titled "Configuring AWS Region Programmatically 1", but it doesn't provide all the answers I need.

Q1: I receive SDKClientException-Unable to find a region via the region provider chain

. What am I doing wrong? or is there a typo I missed.

public class CreateS3Bucket {

public static void main(String[] args) throws IOException {

    BasicAWSCredentials creds = new BasicAWSCredentials("aws-access-key", "aws-secret-key");
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();

    Region region = Region.getRegion(Regions.US_EAST_1);
    s3Client.setRegion(region);

    try {
        String bucketName = "testBucket" + UUID.randomUUID();
        s3Client.createBucket(bucketName);
        System.out.println("Bucket Created Successfully.");

    } catch(AmazonServiceException awse) {

        System.out.println("This means that your request made it AWS S3 but got rejected");
        System.out.println("Error Message:" +awse.getMessage());
        System.out.println("Error Message:" +awse.getErrorCode());
        System.out.println("Error Message:" +awse.getErrorType());
        System.out.println("Error Message:" +awse.getRequestId());

    } catch (AmazonClientException ace) {

        System.out.println("The Amazon Client encountered an Error with network Connectivity");
        System.out.println("Error Message:" + ace.getMessage());
    }


}

      

}

Q2: What code changes need to be made if I want to build a Lambda Function from it? I know how to create a lambda function and the roles it needs. Just need to know if the code I wrote needs to be changed. How should I implement the LambdaFuctionHandler class like below:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

 public class LambdaFunctionHandler implements RequestHandler<String, String> {

@Override
public String handleRequest(String input, Context context) {
    context.getLogger().log("Input: " + input);


    return null;
}

}

      

+3


source to share


2 answers


For Q1, try building your client using the following syntax:



AmazonS3 amazonS3 = AmazonS3Client.builder()
    .withRegion("us-east-1")
    .withCredentials(new AWSStaticCredentialsProvider(creds))
    .build();

      

+4


source


The steps you can take to investigate:

Please make sure your Lambda and S3 are in the same region. (When you use ProviderChain it will render the scope from the lambda function

Also, you don't need to specify BasicCredentials (aws-key..etc) if you are using Lambda.

Please read about the Lambda Permission Model ( http://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html ):

Basically, the Lambda role that you assign must have permission to access S3.

Everything you need to set up S3 is pretty much:



private static final AmazonS3 s3Client = 
AmazonS3ClientBuilder.defaultClient();

      

To test it locally, make sure you set up your AWS credentials locally.

You can check if you have credentials by going into .aws / credentials (This will contain "aws-access-key", "aws-secret-key")

http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

To set up credentials locally, all you need to do is run the AWS Cli command: aws configure ( http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.htm )

+3


source







All Articles