import * as cdk from 'aws-cdk-lib'*
export class beTestStack extends cdk.Stack {
constructor(scope: Consturct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// DYnamo db
const table = new Table(this, 'PaymentsTable', {
tableName: 'paymentsTable',
partitionKey: { name: 'paymentId, type: AttributeType.STRING }
})
// API
const paymentsApi = rnew RestApi(this, 'name', {
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
allowMethods: Cors.ALL_METHODS
}
})
const apymentResource = paymentsApi.root.addResource('payments')
const specificpaymentResoruce = paymentsResource.addResearouce('{id}')
// functions
const createPaymentFunction = this.createLambda('create', 'src/createPayment.ts');
paymentsTable.grantWriteDate(createPaymentFunction);
paymentsResource.addMethod('POST', new LambdaIngtegration(createPaymentFunction))
const getPaymentFunction = this.createLambda('getPayment', 'src/getPayment.ts');
paymentsTable.grantReadDate(getPaymentFunction);
specificPaymentResource.addMethod('GET', new LamdbdaIntegration(getPaymentFunction))
}
createLambda = (name: string, path: string) => {
return new NodejsFunction(this, name, {
functionName: name,
runtime: Runtime.NODEJS_16_X,
entry: path
})
}
}Construct
**api gateway
import {APIGatewayProxyResult} from 'aws-lambda'
export const buildResponse = (statusCode: number, body: Object): APIGatewayProxyResult => {
return {
statusCode,
body: JSON.stringify(body),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
}
}
}