dtt27783 2019-03-08 06:46
浏览 85
已采纳

为DynamoDB创建会话的常用功能

I am developing an AWS Lambda based application which uses go SDK.

I have multiple functions which make calls to DynamoDB APIs.

Currently in each every function I am creating the session.

Instead of this I am thinking, if I can create a common method which can create a session. This session can be used by other functions.

The code snippet is as below -

func GetDynamoDbConnection(customerId string) *dynamodb.DynamoDB {

    roleArn := constants.IAM_ROLE_ARN_PREFIX + constants.AWS_ACCOUNT_ID + ":" + constants.IAM_ROLE_STR + constants.IAM_CUST_ROLE_PREFIX + "-" + customerId
    sess := session.Must(session.NewSession())
    creds := stscreds.NewCredentials(sess, roleArn)
    dynamoDbSession := dynamodb.New(sess, &aws.Config{Credentials: creds})

    return dynamoDbSession

}

I call this function as below -

dbInfo := utils.GetDynamoDbConnection(customerId)

But when I use this session to make DynamoDb calls, it doesn't work. I get an error - Access Denied.

But if I create the session in the same function where I am using it, it works.

Any pointers?

  • 写回答

1条回答 默认 最新

  • dphe5602 2019-03-08 09:17
    关注

    The code seems to be fine. Do make sure that the function calls inside the GetDynamoDbConnection are getting the apt parameters and returning the expected data.

    Here, the error Access Denied probably hints that the Role ARN might not be correctly configured in the stscreds.NewCredentials function. So, check if the RoleARN string has the appropriate value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂