1

我正在使用包括 dynamodb 和 s3 在内的服务和 ios 用于自定义登录用户池,该用户池由联合身份使用,为经过身份验证和未经身份验证的用户提供角色。这完美无缺。

然后,当我使用 AWS AppSync 时,我只是在执行一个 Mutation 并且我收到一条错误消息 Unable to take role arn:aws:iam::xxxxxxxxxx:role/xxxxxxxxx 。尝试由 AppSync 使用的这个 arn:aws:iam:xxx/xxx 角色不是我想要使用的角色,并且 iam 角色与 DynamoDb 和 s3 出于某种原因使用的角色不同,即使我是不改变代码中使用的任何 cognitoid。请参阅下面的一些示例代码。

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// CognitoIdentityUserPoolId is the id of the federated identity from console. works fine with all services except for AppSync by wrong Iam role being chosen
  let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)

  let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: CognitoIdentityUserPoolAppClientSecret, poolId: CognitoIdentityUserPoolId)

  AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)

  pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)

  let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: CognitoIdentityUserPoolId, identityProviderManager:pool)

  let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)

  AWSServiceManager.default().defaultServiceConfiguration = configuration

  pool?.delegate = self

  return true 
}
// In SampleViewController
  // after user signs in
  let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL, serviceRegion: CognitoIdentityUserPoolRegion, userPoolsAuthProvider: self)

  // Initialize the AWS AppSync client
  self.appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)

  // Set id as the cache key for objects
  self.appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }

// later on in the file....
extension SampleViewController: AWSCognitoUserPoolsAuthProvider{
  func getLatestAuthToken() -> String {
    return (myUserSess?.idToken?.tokenString)!
  }
}

AWS AppSync 尝试使用错误的 Iam 名称的任何帮助/原因?

4

2 回答 2

4

你从哪里得到这个消息?使用查询页面时在控制台中还是在客户端上?如果它在控制台中,则问题可能与您的数据源的 IAM 角色有关,重新创建它或确保您具有以下信任策略可能是有意义的:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "appsync.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

如果它在客户端上,则错误可能出在您正在初始化 Cognito Identity 提供程序或 AppSync 构造函数时。

于 2018-05-03T12:34:05.720 回答
0

确保与 Appsync 一起使用的角色能够调用 Lambda 函数,并且与解析程序 Lambda 关联的角色应该具有足够的权限来访问您尝试查询的数据库。

此外,如果有任何策略不适用于 AppSync,它会抛出这样的错误。

于 2020-01-06T21:51:42.707 回答