简体   繁体   中英

aws credentials not found error

I have a jar uploaded to aws lambda but it keeps throwing below error:

 {
  "errorMessage": "java.lang.NullPointerException",
  "errorType": "java.lang.NullPointerException",
  "stackTrace": [
    "com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:143)",
    "com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:132)",
    "com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:99)",
    "com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:135)",
    "com.amazonaws.http.AmazonHttpClient.getCredentialsFromContext(AmazonHttpClient.java:802)",
    "com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:828)",
    "com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:723)",
    "com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:475)",
    "com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:437)",
    "com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:386)",
    "com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2074)",
    "com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2044)",
    "com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1580)",
    "com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:85)",
    "com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:41)",
    "com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:144)",
    "augury.api.SaveAuguryApi.handleRequest(SaveAuguryApi.java:46)",
    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
    "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
    "java.lang.reflect.Method.invoke(Method.java:498)"
  ]
}

and stack trace:

 java.lang.NullPointerException: java.lang.NullPointerException
java.lang.NullPointerException
    at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:143)
    at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:132)
    at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:99)
    at com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:135)
    at com.amazonaws.http.AmazonHttpClient.getCredentialsFromContext(AmazonHttpClient.java:802)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:828)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:723)
    at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:475)
    at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:437)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:386)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2074)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2044)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1580)
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:85)
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:41)
    at com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:144)
    at augury.api.SaveAuguryApi.handleRequest(SaveAuguryApi.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

END RequestId: b2b9807e-6a09-11e6-9873-2588e6cfa497
REPORT RequestId: b2b9807e-6a09-11e6-9873-2588e6cfa497  Duration: 305.85 ms Billed Duration: 400 ms     Memory Size: 512 MB Max Memory Used: 61 MB

And also my lambda java code:

    package augury.api;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import augury.pojo.AuguryResponse;
import augury.pojo.AuguryResult;

public class SaveAuguryApi implements RequestHandler<AuguryResult, AuguryResponse> {

    // Initialize the Log4j logger.
    static final Logger log       = Logger.getLogger(SaveAuguryApi.class);

    static DynamoDB     dynamoDB  = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));

    static String       tableName = "tarot_history";

    public AuguryResponse handleRequest(AuguryResult result, Context context) {
        String userId = result.getUserId();
        List<Integer> tarotIds = result.getTarotIds();
        String createTime = result.getCreate_time();
        if (log.isDebugEnabled()) {
            log.debug("requestId = " + context.getAwsRequestId() + ", userId = " + userId + ", tarotIds = " + tarotIds
                    + ", create_time = " + createTime);
        }
        if (StringUtils.isBlank(userId) || tarotIds == null || tarotIds.isEmpty() || StringUtils.isBlank(createTime)) {
            return new AuguryResponse(400, "this request doesn't contain rightful parameters, please check log");
        }

        Table table = dynamoDB.getTable(tableName);
        Item item = new Item();
        item.withString("create_time", createTime);
        item.withString("user_id", userId);
        item.withList("tarot_ids", tarotIds);
        item.withInt("id", 1);
        table.putItem(item);
        return new AuguryResponse(201, "tarot history created");
    }

}

i tried but i still couldn't locate the problem. I'm new to aws lambda and I was trying to learn from the link

The example you are looking at assumes you have credential properties saved to a file, which isn't going to be the case in your Lambda environment. To use the IAM role assigned to the Lambda function change this:

static DynamoDB     dynamoDB  = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));

To this:

static DynamoDB     dynamoDB  = new DynamoDB(new AmazonDynamoDBClient());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM