java - Error when AWS Lambda trying to list DynamoDb tables -


i failed following logic work on aws lambda using java:

1) when there new object created in s3 bucket, trigger lambda function (written in java)

2) in lambda function, list dynamodb tables.

3) create table if there none.

4) write s3 object's details item dynamodb.

i item #1 working. when reach item #2, encounter permission related error below.

any or suggestion?

the permission use "basic dynamodb", has following permission:

start requestid: e9ab5aba-307b-11e5-9663-3188c327cf5e filesize: 1024, datetime:1970-01-01t00:00:00.000zs3key : happyface.jpgaws credential profiles file not found in given path: /home/sbx_user1052/.aws/credentials: java.lang.illegalargumentexception java.lang.illegalargumentexception: aws credential profiles file not found in given path: /home/sbx_user1052/.aws/credentials @ com.amazonaws.auth.profile.internal.profilesconfigfileloader.loadprofiles(profilesconfigfileloader.java:45) @ com.amazonaws.auth.profile.profilesconfigfile.loadprofiles(profilesconfigfile.java:176) @ com.amazonaws.auth.profile.profilesconfigfile.(profilesconfigfile.java:112) @ com.amazonaws.auth.profile.profilesconfigfile.(profilesconfigfile.java:92) @ com.amazonaws.auth.profile.profilecredentialsprovider.getcredentials(profilecredentialsprovider.java:123) @ com.amazonaws.services.dynamodbv2.amazondynamodbclient.invoke(amazondynamodbclient.java:1763) @ com.amazonaws.services.dynamodbv2.amazondynamodbclient.listtables(amazondynamodbclient.java:1208) @ com.amazonaws.services.dynamodbv2.document.internal.listtablescollection.firstpage(listtablescollection.java:46) @ com.amazonaws.services.dynamodbv2.document.internal.pageiterator.next(pageiterator.java:45) @ com.amazonaws.services.dynamodbv2.document.internal.iteratorsupport.nextresource(iteratorsupport.java:79) @ com.amazonaws.services.dynamodbv2.document.internal.iteratorsupport.hasnext(iteratorsupport.java:47) @ com.triggerdynamodb.handlerequest(triggerdynamodb.java:68) @ 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:497)

end requestid: e9ab5aba-307b-11e5-9663-3188c327cf5e report requestid: e9ab5aba-307b-11e5-9663-3188c327cf5e duration: 3294.97 ms billed duration: 3300 ms memory size: 512 mb max memory used: 51 mb

code follows:

public class triggerdynamodb implements requesthandler<s3event, string> {  public string handlerequest(s3event s3event, context context) {      lambdalogger logger = context.getlogger();      try {                     s3eventnotificationrecord record = s3event.getrecords().get(0);          // object key may have spaces or unicode non-ascii characters.          string srckey = record.gets3().getobject().getkey().replace('+', ' ');             srckey = urldecoder.decode(srckey, "utf-8");           long filesize = record.gets3().getobject().getsizeaslong();          datetime datetime = record.geteventtime();            logger.log("filesize: " + filesize + ", datetime:" + datetime);          logger.log("s3key   : " + srckey);           dynamodb dynamodb = new dynamodb(new amazondynamodbclient(new profilecredentialsprovider()));          //table table = dynamodb.gettable("dimensionfile");           tablecollection<listtablesresult> tables = dynamodb.listtables();          iterator<table> iterator = tables.iterator();           while (iterator.hasnext()) { // error thrown              table table = iterator.next();              system.out.println(table.gettablename());          }                              return "ok";      } catch (exception e) {          throw new runtimeexception(e);      } } } 

the profilecredentialsprovider you're passing amazondynamodbclient constructor attempts load credentials ~/.aws/credentials file. file doesn't exist on host lambda function being run, hence exception. provider intended things integration tests run on dev machine , use personal iam user credentials.

on lambda want environmentvariablecredentialsprovider. provider load temporary credentials iam role associated lambda function environment variables lambda sets before invoking code.

if use no-argument constructor amazondynamodbclient, default using defaultawscredentialsproviderchain pull credentials. provider checks environment variables, java system properties, ~/.aws/credentials, , ec2 instance metadata service (in order), , should pick credentials whether you're running locally or on lambda.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -