0

我遇到了数据源是关系数据库的应用同步问题。

因此,我的目标是将 Aurora serverless postgresql 集群与应用同步作为数据源连接并使其正常工作。

我执行的步骤(全部通过无服务器框架):

创建了极光 postgreSQL 集群。

将极光 postgreSQL 集群连接到应用同步。

在应用同步中创建了 graphql Schema。

身份验证类型是 - API 密钥。

应用程序同步配置在此处(https://github.com/serverless-components/aws-app-sync#data-sources--templates)。:

appSync:
name: Posts
authenticationType: API_KEY
apiKeys:
  - myApiKey
dataSources:
  - type: RELATIONAL_DATABASE
    name: Posts
    config:
      awsSecretStoreArn: 
        Ref: SecretsManager
      databaseName: "mydatabase"
      dbClusterIdentifier: 
        Ref: AuroraRDSCluster
schema: schema.graphql
mappingTemplates:
  - dataSource: Posts
    type: Query
    field: getPet
    request: "request.vtl"
    response: "response.vtl"
  - dataSource: Posts
    type: Query
    field: listPets
    request: "listPets_request.vtl"
    response: "listPets_response.vtl"

我的 graphql 架构取自这里(https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-rds-resolvers.html

    type Mutation {
createPet(input: CreatePetInput!): Pet
updatePet(input: UpdatePetInput!): Pet
deletePet(input: DeletePetInput!): Pet

}

    input CreatePetInput {
type: PetType
price: Float!

}

    input UpdatePetInput {id: ID!
type: PetType
price: Float!

}

    input DeletePetInput {
id: ID!

}

    type Pet {
id: ID!
type: PetType
price: Float

}

    enum PetType {
dog
cat
fish
bird
gecko

}

    type Query {
getPet(id: ID!): Pet
listPets: [Pet]
listPetsByPriceRange(min: Float, max: Float): [Pet]

}

    schema {
query: Query
mutation: Mutation

}

我的 getPet request.vtl :

{
"version": "2018-05-29",
    "statements": [
        $util.toJson("select * from Pets WHERE id='$ctx.args.id'")
]

}

我的 getPet response.vtl:

$utils.toJson($utils.rds.toJsonObject($ctx.result)[0][0])

一切都部署得非常完美,例如集群 Arn(dbClusterIdentifier) 、带有应用同步数据源的 awsSecretStoreArn 数据库名称、带有架构的解析器。

并在部署输出中获得了带有 api 密钥的 graphql api。

在此之后,我通过 rds 集群的查询编辑器创建了一个具有列 id、类型、价格和一条记录的表 Pets。

所以现在,当我试图在应用程序同步的控制台中查询时,我得到的值为 null。查询是:

    query {getPet(id: "1"){id type price }}

也尝试使用节点 js 代码,但结果相同。



// url of your GraphQL API. If you configured a custom domain, you could use that instead


    const url =

  "https://z4#############.appsync-api.us-east-1.amazonaws.com/graphql";


// api key of your GraphQL API

const apiKey = "da2-7by6############";


// ID of the post you wanna query

const id = "1";


fetch(url, {

  method: "POST",

  headers: {

    "Content-Type": "application/json",

    "x-api-key": apiKey

  },

  body: JSON.stringify({

    query: `query {getPet(id: "${id}"){id}}`

  })

})

  .then(res => res.text())

  .then(post => console.log(post));```
any help how to make it working.
4

1 回答 1

2

因此,问题在于 appsync 采用的默认 iam 角色策略。默认情况下,appsync 将使用以下策略创建 iam 角色

- Effect: Allow
  Action:
    - secretsmanager:GetSecretValue
  Resource: "your resource mentioned"
- Effect: Allow
  Action:
    - rds-data:DeleteItems
    - rds-data:ExecuteSql
    - rds-data:GetItems
    - rds-data:InsertItems
    - rds-data:UpdateItems
  Resource: "your resource mentioned"

现在这里缺少一件事,那就是

- rds-data:ExecuteStatement

因此,我创建了缺少警察的新 iam 角色,并将其附加到 appsync,它对我有用。

于 2020-04-04T12:55:29.760 回答