Aurora Serverless Postgresql 作为应用程序同步的数据源

Aurora Serverless Postgresql as data source for app sync

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

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

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

创建了 aurora postgreSQL 集群。

已将 aurora postgreSQL 集群连接到应用程序同步。

已在应用同步中创建 graphql 架构。

身份验证类型是 - 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

}

我的宠物 request.vtl :

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

}

我的宠物 response.vtl:

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

一切都完美部署,如集群 Arn(dbClusterIdentifier)、awsSecretStoreArn 数据库名称与应用程序同步数据源、解析器与架构。

并在部署输出中获得带有 api 键的 graphql api。

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

所以现在,当我尝试在应用程序同步控制台中查询时,我得到的值为 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.

因此,问题出在 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,它对我有用。