Skip to content

Commit

Permalink
Implemented get-list with a lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
willa75 committed May 19, 2022
1 parent a989c00 commit cdd1e11
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.js
!jest.config.js
*.d.ts
node_modules
Expand Down
17 changes: 17 additions & 0 deletions lambdas/get-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const DynamoDB = require('aws-sdk/clients/dynamodb');

const DocumentClient = new DynamoDB.DocumentClient();
const { POSTS_TABLE} = process.env;

module.exports.handler = async (event, context) => {
console.log(event);
const resp = await DocumentClient.scan({
TableName: POSTS_TABLE,
}).promise();

console.log(resp.Items);

return resp.Items;

return [];
};
35 changes: 29 additions & 6 deletions lib/cdk-typescript-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import { Construct } from 'constructs';
import {
aws_cognito as cognito,
aws_dynamodb as dynamodb,
aws_lambda as lambda
aws_ec2 as ec2,
aws_ecs as ecs,
aws_iam as iam,
aws_lambda as lambda
} from 'aws-cdk-lib';

import * as path from 'path';


export class CdkTypescriptStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
Expand All @@ -20,6 +25,17 @@ export class CdkTypescriptStack extends Stack {
replicationRegions: ['us-east-1']
});

const getListLambda = new lambda.Function(this, 'CdkListFunction', {
runtime: lambda.Runtime.NODEJS_16_X,
code: lambda.Code.fromAsset('lambdas'),
handler: 'get-list.handler',
environment: {
POSTS_TABLE: postsTable.tableName
}
});

postsTable.grantReadData(getListLambda);

const userPool = new cognito.UserPool(this, 'CdkAuth', {
userPoolName: 'CdkAuth',
selfSignUpEnabled: true,
Expand Down Expand Up @@ -55,15 +71,22 @@ export class CdkTypescriptStack extends Stack {
});

const postsDS = api.addDynamoDbDataSource('postsTable', postsTable);
const getListFunctionDS = api.addLambdaDataSource('getListFunction', getListLambda);

// get list of posts
postsDS.createResolver({
// get list of posts with a lambda
getListFunctionDS.createResolver({
typeName: 'Query',
fieldName: 'getPosts',
requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList()
fieldName: 'getPosts'
});

// get list of posts
// postsDS.createResolver({
// typeName: 'Query',
// fieldName: 'getPosts',
// requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(),
// responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList()
// });

// create post
postsDS.createResolver({
typeName: 'Mutation',
Expand Down

0 comments on commit cdd1e11

Please sign in to comment.