Skip to content

Instantly share code, notes, and snippets.

View cal0610's full-sized avatar

Calvin Pang cal0610

View GitHub Profile
@cal0610
cal0610 / myStack.ts
Last active November 1, 2022 09:44
add rest api
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class MyStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const api = new apigateway.RestApi(this, 'test-api', {
description: 'Test project',
@cal0610
cal0610 / myStack.ts
Last active November 2, 2022 03:38
add dynamodb
const storesTable = new Table(this, "stores-table", {
partitionKey: { name: "id", type: AttributeType.STRING },
billingMode: BillingMode.PAY_PER_REQUEST,
tableClass: TableClass.STANDARD_INFREQUENT_ACCESS,
tableName: "stores",
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
@cal0610
cal0610 / create.ts
Created November 1, 2022 09:51
create store
import * as AWS from 'aws-sdk';
import { v4 as uuidv4 } from 'uuid';
const TABLE_NAME = process.env.STORE_TABLE_NAME || '';
const PRIMARY_KEY = process.env.STORE_PRIMARY_KEY || '';
const db = new AWS.DynamoDB.DocumentClient();
const RESERVED_RESPONSE = `Error: You're using AWS reserved keywords as attributes`,
DYNAMODB_EXECUTION_ERROR = `Error: Execution update, caused a Dynamodb error, please take a look at your CloudWatch Logs.`;
export const handler = async (event: any = {}): Promise<any> => {
import * as AWS from "aws-sdk";
const TABLE_NAME = process.env.STORE_TABLE_NAME || "";
const PRIMARY_KEY = process.env.STORE_PRIMARY_KEY || "";
const db = new AWS.DynamoDB.DocumentClient();
export const handler = async (event: any = {}): Promise<any> => {
const requestedItemId = event.pathParameters.id;
if (!requestedItemId) {
import * as AWS from 'aws-sdk';
const TABLE_NAME = process.env.STORE_TABLE_NAME || '';
const db = new AWS.DynamoDB.DocumentClient();
export const handler = async (event: any): Promise<any> => {
const params = {
TableName: TABLE_NAME,
};
import * as AWS from 'aws-sdk';
const TABLE_NAME = process.env.STORE_TABLE_NAME || '';
const PRIMARY_KEY = process.env.STORE_PRIMARY_KEY || '';
const db = new AWS.DynamoDB.DocumentClient();
export const handler = async (event: any = {}): Promise<any> => {
const requestedItemId = event.pathParameters.id;
if (!requestedItemId) {
import * as AWS from 'aws-sdk';
const TABLE_NAME = process.env.STORE_TABLE_NAME || '';
const PRIMARY_KEY = process.env.STORE_PRIMARY_KEY || '';
const RESERVED_RESPONSE = `Error: You're using AWS reserved keywords as attributes`,
DYNAMODB_EXECUTION_ERROR = `Error: Execution update, caused a Dynamodb error, please take a look at your CloudWatch Logs.`;
const db = new AWS.DynamoDB.DocumentClient();
@cal0610
cal0610 / myStack.ts
Last active November 1, 2022 10:03
create nodejs lambda functions
const nodejsProps: NodejsFunctionProps = {
depsLockFilePath: join(__dirname, '..', 'package-lock.json'),
environment: {
STORE_PRIMARY_KEY: 'id', // we're able to reference it in our lambda function with process.env
STORE_TABLE_NAME: storesTable.tableName,
}
};
const getOneLambda = new NodejsFunction(this, 'getOneStoreFunction', {
entry: join(__dirname, '..', 'get-one.ts'),
@cal0610
cal0610 / myStack.ts
Created November 1, 2022 10:05
grant lambda permission to dynamodb
[createOneLambda, getAllLambda, getOneLambda, updateOneLambda, deleteOneLambda]
.forEach(i => storesTable.grantReadWriteData(i));
@cal0610
cal0610 / myStack.ts
Created November 1, 2022 10:08
lambda integration
const getAllIntegration = new LambdaIntegration(getAllLambda, {proxy: true});
const createOneIntegration = new LambdaIntegration(createOneLambda, {proxy: true});
const getOneIntegration = new LambdaIntegration(getOneLambda, {proxy: true});
const updateOneIntegration = new LambdaIntegration(updateOneLambda, {proxy: true});
const deleteOneIntegration = new LambdaIntegration(deleteOneLambda, {proxy: true});
const store = api.root.addResource('store');
store.addMethod('POST', createOneIntegration);
store.addMethod('GET', getAllIntegration);