aws凭证未找到错误

时间:2016-08-24 14:58:19

标签: amazon-web-services aws-lambda aws-java-sdk

我有一个jar上传到aws lambda但是它一直在抛出错误:

 {
  "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)"
  ]
}

和堆栈跟踪:

 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

还有我的lambda java代码:

    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");
    }

}

我试过但我仍然无法找到问题所在。我是aws lambda的新手,我试图从the link

学习

1 个答案:

答案 0 :(得分:2)

您正在查看的示例假设您已将凭据属性保存到文件中,而Lambda环境中的情况并非如此。要使用分配给Lambda函数的IAM角色,请更改:

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

对此:

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