Я использую микросервис со структурой папок.
Microservice =>
resolvers ->
Media/Image/get-images/request.vtl
Media/Image/get-images/response.vtl
templates -> services.yaml
Запрос сопоставления:
#set($imageIds=$ctx.source.imageIds)
#set($keys=[])
#foreach($imageId in $imageIds)
#set($key={})
$util.qr($key.put("id", $util.dynamodb.toString($imageId)))
$util.qr($keys.add($key))
#end
{
"version": "2018-05-29",
"operation": "BatchGetItem",
"tables" : {
"MediaImages": {
"keys": $util.toJson($keys)
}
}
}
Отображение ответа:
#set($result=$ctx.result.data.MediaImages)
$util.toJson($result)
Service.yaml
Resources:
MediaImagesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: MediaImages
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: userId
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
GlobalSecondaryIndexes:
- IndexName: UserImages
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: id
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 2
ImageDetailsDataSource:
Type: AWS::AppSync::DataSource
Properties:
Name: ImageDetailsDataSource
Type: AMAZON_DYNAMODB
ServiceRoleArn:
Fn::ImportValue: !Sub "DynamoDB-Role"
ApiId:
Fn::ImportValue: !Sub "API-Id"
DynamoDBConfig:
TableName: !Ref MediaImagesTable
AwsRegion: !Ref AWS::Region
UseCallerCredentials: false
GetImagesPipelineFunction:
Type: AWS::AppSync::FunctionConfiguration
Properties:
ApiId:
Fn::ImportValue: !Sub "API-Id"
Name: GetImagesPipelineFunction
FunctionVersion: "2018-05-29"
Description: Function to get the images from dynamo db
DataSourceName: !GetAtt ImageDetailsDataSource.Name
RequestMappingTemplateS3Location: ../resolvers/get-images/request.vtl
ResponseMappingTemplateS3Location: ../resolvers/get-images/response.vtl
Я пытался
#set($tableName=$util.dynamodb.getDataSourceTableName())
#set($imageIds=$ctx.source.imageIds)
#set($keys=[])
#foreach($imageId in $imageIds)
#set($key={})
$util.qr($key.put("id", $util.dynamodb.toString($imageId)))
$util.qr($keys.add($key))
#end
{
"version": "2018-05-29",
"operation": "BatchGetItem",
"tables" : {
"$tableName": {
"keys": $util.toJson($keys)
}
}
}
"error": {
"message": "1 validation error detected: Value '{$tableName= .
[com.amazonaws.dynamodb.v20120810.WriteRequest@1528275d]}' at
'requestItems' failed to satisfy constraint: Map keys must satisfy
constraint: [Member must have length less than or equal to 255, Member
must have length greater than or equal to 3, Member must satisfy regular
expression pattern: [a-zA-Z0-9_.-]+] (Service: AmazonDynamoDBv2; Status
Code: 400; Error Code: ValidationException; Request ID:
464H3LIEPOSA2S8OI34RJ31QLNVV4KQNSO5AEMVJF66Q9ASUAAJG)",
"type": "DynamoDB:AmazonDynamoDBException"
},
В моем шаблоне сопоставления запросов AppSync. Я делаю BatchGetItem и жестко кодирую имя таблицы. Я хочу динамически получать имя таблицы в шаблон сопоставления моего запроса и ответа. Я попробовал справочник по шаблонам картографии $util.dynamodb.getDataSourceTableName($dataSourceName)
, но не сработал.