Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { Build } from 'punchcard/lib/core/build';
export const app = new Core.App();
const stack = app.root.map(app => new cdk.Stack(app, 'invoke-function-example'));
const table = new DynamoDB.Table(stack, 'my-table', {
partitionKey: 'id',
attributes: {
id: string(),
count: integer({
minimum: 0
}),
anyProperty: dynamic
},
tableProps: Build.of({
billingMode: BillingMode.PAY_PER_REQUEST
})
});
// create a function that increments counts in a dynamodb table
// Function
const incrementer = new Lambda.Function(stack, 'Callable', {
// request is a structure with a single property, 'id'
request: struct({
id: string()
}),
// response is just an integer
response: integer(),
depends: table.readWriteAccess(),
}, async (request, table) => {
console.log(request);
const item = await table.get({
name: string(),
array: array(string()),
struct: struct({
key: string(),
number: integer()
}),
any: dynamic
}
// the type can be inferred, but we explicitly define them to illustrate how it works
// 'id' is the partitionKey, undefined is the sortKey (no sort key), and Item is the attributes of data in the table
const table: DynamoDB.Table<'id', undefined, Item> = new DynamoDB.Table(stack, 'hash-table', {
partitionKey: 'id',
attributes: Item,
tableProps: Build.of({
billingMode: BillingMode.PAY_PER_REQUEST
})
});
// 'count' is the sortKey in this case
const sortedTable: DynamoDB.Table<'id', 'count', Item> = new DynamoDB.Table(stack, 'sorted-table', {
partitionKey: 'id',
sortKey: 'count',
attributes: Item,
tableProps: Build.of({
billingMode: BillingMode.PAY_PER_REQUEST
})
});
// call the incrementer function from another Lambda Function
Lambda.schedule(stack, 'Caller', {
depends: Core.Dependency.concat(table.readWriteAccess(), sortedTable.readAccess()),
// 'id' is the partitionKey, undefined is the sortKey (no sort key), and Item is the attributes of data in the table
const table: DynamoDB.Table<'id', undefined, Item> = new DynamoDB.Table(stack, 'hash-table', {
partitionKey: 'id',
attributes: Item,
tableProps: Build.of({
billingMode: BillingMode.PAY_PER_REQUEST
})
});
// 'count' is the sortKey in this case
const sortedTable: DynamoDB.Table<'id', 'count', Item> = new DynamoDB.Table(stack, 'sorted-table', {
partitionKey: 'id',
sortKey: 'count',
attributes: Item,
tableProps: Build.of({
billingMode: BillingMode.PAY_PER_REQUEST
})
});
// call the incrementer function from another Lambda Function
Lambda.schedule(stack, 'Caller', {
depends: Core.Dependency.concat(table.readWriteAccess(), sortedTable.readAccess()),
schedule: Schedule.rate(Duration.minutes(1)),
}, async (_, [table, sortedTable]) => {
await table.get({
id: 'id',
});
await table.put({
// the item is type-safe and well structured
item: {
id: 'id',
// Purpose: serverless, pay as you go, persistent storage for the demo app
// See also:
// - https://aws.amazon.com/dynamodb/
// - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-dynamodb-readme.html
const itemsTable = new dynamodb.Table(this, "ItemsTable", {
billingMode: BillingMode.PAY_PER_REQUEST,
serverSideEncryption: true,
stream: StreamViewType.NEW_AND_OLD_IMAGES,
partitionKey: {name: "id", type: dynamodb.AttributeType.STRING}
});
const usersTable = new dynamodb.Table(this, "UsersTable", {
billingMode: BillingMode.PAY_PER_REQUEST,
serverSideEncryption: true,
stream: StreamViewType.NEW_AND_OLD_IMAGES,
partitionKey: {name: "username", type: dynamodb.AttributeType.STRING},
timeToLiveAttribute: "ttl",
});
// ========================================================================
// Resource: AWS Lambda Function - CRUD API Backend
// ========================================================================
// Purpose: serverless backend for the demo app, uses express.js
// See also:
// - https://aws.amazon.com/lambda/
// - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-readme.html
groupName: usersGroupName,
userPoolId: userPool.userPoolId,
});
// ========================================================================
// Resource: Amazon DynamoDB Table
// ========================================================================
// Purpose: serverless, pay as you go, persistent storage for the demo app
// See also:
// - https://aws.amazon.com/dynamodb/
// - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-dynamodb-readme.html
const itemsTable = new dynamodb.Table(this, "ItemsTable", {
billingMode: BillingMode.PAY_PER_REQUEST,
serverSideEncryption: true,
stream: StreamViewType.NEW_AND_OLD_IMAGES,
partitionKey: {name: "id", type: dynamodb.AttributeType.STRING}
});
const usersTable = new dynamodb.Table(this, "UsersTable", {
billingMode: BillingMode.PAY_PER_REQUEST,
serverSideEncryption: true,
stream: StreamViewType.NEW_AND_OLD_IMAGES,
partitionKey: {name: "username", type: dynamodb.AttributeType.STRING},
timeToLiveAttribute: "ttl",
});
// ========================================================================
// Resource: AWS Lambda Function - CRUD API Backend
// ========================================================================
save(name: String!): ${tableName}
delete(${tableName}Id: ID!): ${tableName}
}
type Schema {
query: Query
mutation: Mutation
}`
});
const itemsTable = new Table(this, 'ItemsTable', {
tableName: tableName,
partitionKey: {
name: `${tableName}Id`,
type: AttributeType.STRING
},
billingMode: BillingMode.PAY_PER_REQUEST,
stream: StreamViewType.NEW_IMAGE,
// The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete
// the new table, and it will remain in your account until manually deleted. By setting the policy to
// DESTROY, cdk destroy will delete the table (even if it has data in it)
removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
});
const itemsTableRole = new Role(this, 'ItemsDynamoDBRole', {
assumedBy: new ServicePrincipal('appsync.amazonaws.com')
});
itemsTableRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonDynamoDBFullAccess'));
const dataSource = new CfnDataSource(this, 'ItemsDataSource', {
apiId: itemsGraphQLApi.attrApiId,
tableProps: Build.lazy(() => ({
billingMode: BillingMode.PAY_PER_REQUEST
}))
});
tableProps: Build.lazy(() => ({
billingMode: BillingMode.PAY_PER_REQUEST
}))
});