serverless-appsync-plugin icon indicating copy to clipboard operation
serverless-appsync-plugin copied to clipboard

Substitution to mock identity locally

Open moioo91120 opened this issue 2 years ago • 1 comments

Hello,

I have an appsync service with appsync simulator to make it work locally.

I'm using the substitution , same way as the readme :

service: dmkt-gql

plugins:
  - serverless-webpack
  - serverless-dynamodb-local
  - serverless-appsync-plugin
  - serverless-appsync-simulator
  - serverless-s3-local
  - serverless-offline

custom:
  defaultStage: dev
  defaultRegion: eu-central-1
  currentStage: ${opt:stage, 'dev'}
  tableName: ${self:custom.currentStage}_
  s3ImageBucketName: yycatlab.${self:provider.region}.${self:provider.stage}.images
  s3ImageBucketArn: arn:aws:s3:::${self:custom.s3ImageBucketName}
  config: ${file(./serverless/config/config.${self:custom.currentStage}.yml)}
  webpack:
    includeModules:
      forceExclude:
        - aws-sdk
    webpackConfig: ./webpack/webpack.config.${self:custom.currentStage}.js
  s3:
    host: localhost
    directory: /tmp
    cors: "./serverless/local-s3.xml"
  dynamodb:
    stages:
      - dev
    start:
      port: 8020
      inMemory: true
      heapInitial: 200m
      heapMax: 1g
      migrate: true
      seed: true
      convertEmptyValues: true
  appsync-simulator:
    apiKey: da2-fakeApiId123456
  appSync:
    substitutions:
      exampleVar1: "${self:service.name}"
      exampleVar2: { "Fn::ImportValue": "Some-external-stuff" }
    authenticationType: ${self:custom.config.APPSYNC_AUTHENTICATION_TYPE}
    additionalAuthenticationProviders: ${self:custom.config.APPSYNC_AUTHENTICATION_TYPE_ADDITIONAL}
    userPoolConfig:
      defaultAction: ALLOW
      userPoolId:
        Ref: CognitoUserPool
    schema: ./src/schema.gql
    serviceRole: ${self:custom.currentStage}_AppSync
    mappingTemplatesLocation: ./src/mapping
    mappingTemplates:
      - ${file(./serverless/mapping/layer.yml)}
      - ${file(./serverless/mapping/layer-part.yml)}
    dataSources:
      - ${file(./serverless/datasource/dynamodb.yml)}
      - ${file(./serverless/datasource/lambda.yml)}
    functionConfigurations:
      - dataSource: UploadFile
        name: "uploadLambda"
        request: "layer-part/upload.request.vtl"
        response: "one.response.vtl"
      - dataSource: LayerPartSource
        name: "layerPartSave"
        request: "layer-part/createLayerPart.request.vtl"
        response: "one.response.vtl"

provider:
  name: aws
  runtime: nodejs14.x
  memorySize: 512
  timeout: 10
  logRetentionInDays: 30
  stage: ${self:custom.currentStage}
  region: ${self:custom.defaultRegion}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - logs:PutLogEvents
        - logs:DescribeLogStreams
        - logs:DescribeLogGroups
        - logs:CreateLogStream
        - logs:CreateLogGroup
        - logs:PutRetentionPolicy
      Resource: arn:aws:logs:${self:provider.region}:*:log-group:${self:provider.stage}*
    - Effect: Allow
      Action:
        - cloudwatch:PutMetricData
        - xray:PutTraceSegments
        - xray:PutTelemetryRecords
      Resource: "*"
    - Effect: Allow
      Action:
        - s3:*
      Resource: "*"
    - Effect: Allow
      Action:
        - dynamodb:BatchReadItem
        - dynamodb:BatchWriteItem
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:Scan
        - dynamodb:Query
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:DescribeStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
        - dynamodb:ListStreams
      Resource: arn:aws:dynamodb:${self:provider.region}:*:table/*
    - Effect: Allow
      Action: appsync:GraphQL
      Resource: "*"

package:
  individually: true
  include:
    - .env
    - .schema.gql

functions:
  UploadFile:
    handler: src/handler.handler
    events:
      - http:
          path: upload
          method: post
          cors: true
    environment: ${self:custom.config.LAMBDA_VAR}

resources:
  - ${file(./serverless/dynamodb.yml)}
  - ${file(./serverless/role.yml)}
  - ${file(./serverless/file.yml)}
  - ${file(./serverless/cognito-user-pool.yml)}
  - ${file(./serverless/cognito-identity-pool.yml)}

But on my vtl template , it doesn't seem to replace "exampleVar1" with the associated value

$util.error($util.toJson(${exampleVar1}))
#set( $userId  = $ctx.identity.username )
$util.error($util.toJson($tableName))
#if( $util.isNullOrEmpty($userId) )
    $util.error("Mandatory userId")
#end

{
    "version" : "2018-05-29",
    "operation" : "PutItem",
    "key" : {
        "id" : $util.dynamodb.toDynamoDBJson($util.autoId())
    },
    "attributeValues" : {
        userId: 
        #if( !$util.isNullOrEmpty(${ctx.args.layer.name} ) )
            "name": $util.dynamodb.toDynamoDBJson($ctx.args.layer.name),
        #end
        "collectionId": $util.dynamodb.toDynamoDBJson($ctx.args.layer.collectionId),
        "z":  $util.dynamodb.toDynamoDBJson($util.defaultIfNull($ctx.args.layer.z, 1))
    }
}

Note that I'm starting my server with following script :

"start": "sls offline start --stage dev --config ./serverless/serverless.yml",

Anyone know what I did wrong ? thanks,

moioo91120 avatar Nov 14 '21 14:11 moioo91120

Are you talking about the simulator or real deployed AppSync? Looks like you are talking about the simulator, so this is the wrong repo to ask related questions.

The simulator supports substitutions too, however, there are some limitations when using Intrinsic functions (eg: { "Fn::ImportValue": "Some-external-stuff" }). SF variables might not work either.

See this section: https://github.com/bboure/serverless-appsync-simulator#override-or-mock-values

bboure avatar Dec 01 '21 21:12 bboure