Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

I have written a Lambda function. This function is uploaded in s3Bucket = "my-lambda", which is mapped to the role hello-lambda-role and the regionName = "us-west-2". 

Now I wanted to access s3Bucket="some-other" where we have mapped Policy with 'hello-lambda-role' and it is in the region "eu-west-1". 

Here is the API class I am using AmazonS3Client. My intention is to get some file from the bucket "some-other". But before that, I need to make the connection. 




public class LambdaFunctionHandler implements RequestHandler<Request, Response> {


    public Response handleRequest(Request request, Context context) {

        String greetingString = String.format("Hello %s %s.",

                request.firstName, request.lastName);

        return new Response(greetingString);




Here is the class which list the bucket. 

public class Test{

    private static final Log logger = LogFactory.getLog(InvokeLambda.class);

    private static final String awsAccessKeyId = "XXXXX";

    private static final String awsSecretAccessKey = "XXXXX";

    private static final String regionName = "eu-west-1";

    private static Region region;

    private static AWSCredentials credentials;

    private static AWSLambdaClient lambdaClient;

    private static AmazonS3Client s3Client;


    private static void init() {

        credentials = new BasicAWSCredentials(awsAccessKeyId,


        s3Client = (credentials == null) ? new AmazonS3Client()

                : new AmazonS3Client(credentials);

        region = Region.getRegion(Regions.fromName(regionName));


        lambdaClient = (credentials == null) ? new AWSLambdaClient()

                : new AWSLambdaClient(credentials);



        // lambdaClient.configureRegion(Regions.US_WEST_2);




     * The entry point into the AWS lambda function.


    public static void main(String... args) {





    private static Bucket getExistingBucket() {

        List<Bucket> buckets = s3Client.listBuckets();

        for (Bucket bucket : buckets) {



        return null;



1 Answer

0 votes
by (44.4k points)

Use same code as you'd in your test, except you do not need to give credentials when you create the AmazonS3Client. Note that the role your lambda uses wants the authority to access your S3 bucket. The region of the S3 bucket shouldn't matter; the bucket name uniquely identifies the bucket regardless of region.

Lambdas are generally triggered by an event, however, you'll call AWSLambdaClient.invoke() to run it manually.


For example:

public Response handleRequest(Request request, Context context) {

    AmazonS3Client s3Client = new AmazonS3Client();

    S3Object = s3Client.getObject("some-other", request.getFilename());


    return new Response(result);


Deploy that to AWS as "mylambda", and then invoke remotely with:

lambdaClient.invoke(new InvokeRequest().withFunctionName("mylambda").withPayload("input"));

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories