Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda custom triggers

Can someone tell me how to get access to AWS credentials in an AWS Lambda function?

I've searched the internet thoroughly but still haven't found anything that has helped me.

Im writing the function in Java. I think I should have access to the credentials with the context object in the HandleRequest method.

If it helps, I want to invoke a DynamoDB client and upload a record to the database.

like image 946
Jose Maria Landa Avatar asked Dec 20 '22 00:12

Jose Maria Landa


1 Answers

I came into the same problem myself recently. This certainly is a blind spot in AWS's Lambda documentation for Java, in my opinion.

This snippet in Java should work for you, assuming you're using the AWS SDK for Java Document API :

DynamoDB dynamodb = new DynamoDB(
    new AmazonDynamoDBClient(new EnvironmentVariableCredentialsProvider()));

The main takeaway is to use the EnvironmentVariableCredentialsProvider to access the required credentials to access your other AWS resources within the AWS Lambda container. The Lambda containers are shipped with credentials as environment variables, and this is sufficient in retrieving them.

Note: This creates a DynamoDB instance that only sees resources in the default region. To create one for a specific region, use this (assuming you want to access DynamoDB's in the ap-northeast-1 region):

DynamoDB dynamodb = new DynamoDB(
    Regions.getRegion(Regions.AP_NORTHEAST_1).createClient(
        AmazonDynamoDBClient.class,
        new EnvironmentVariableCredentialsProvider(),
        new ClientConfiguration()));
like image 124
Gordon Tai Avatar answered Jan 04 '23 06:01

Gordon Tai