Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions product-service/bin/aws-shop-nodejs-back.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { App } from 'aws-cdk-lib';
import { AwsShopNodejsBackStack } from '../lib/aws-shop-nodejs-back-stack';

const app = new cdk.App();
const app = new App();

new AwsShopNodejsBackStack(app, {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
description: "This stack includes resources needed to deploy aws-shop-backend application"
});
2 changes: 1 addition & 1 deletion product-service/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testMatch: ['<rootDir>/src/**/*.test.(ts|tsx)'],
testMatch: ['<rootDir>/test/*.test.(ts|tsx)'],
roots: ['<rootDir>'],
};
96 changes: 72 additions & 24 deletions product-service/lib/aws-shop-nodejs-back-stack.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,86 @@
import * as cdk from 'aws-cdk-lib';
import * as lambda from "aws-cdk-lib/aws-lambda";
import { HttpApi, CorsHttpMethod, HttpMethod, ParameterMapping, MappingValue } from "@aws-cdk/aws-apigatewayv2-alpha";
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { Construct } from 'constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
import { Runtime } from "aws-cdk-lib/aws-lambda";
import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as core from 'aws-cdk-lib/core';
import {
HttpApi,
CorsHttpMethod,
HttpMethod,
ParameterMapping,
MappingValue
} from "@aws-cdk/aws-apigatewayv2-alpha";
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { NodejsFunction, NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';
import * as dotenv from 'dotenv';
import { config as envConfig } from 'dotenv';

dotenv.config();
console.log(process.env)
envConfig();
export class AwsShopNodejsBackStack extends Stack {
private getEnvironment() {
return {
TABLE_PRODUCTS: process.env.DB_TABLE_PRODUCTS!,
TABLE_STOCKS: process.env.DB_TABLE_STOCKS!,
USE_NOSQL_DB: process.env.USE_NOSQL_DB!,
...process.env.USE_NOSQL_DB === 'true' ? {} : {
PGHOST: process.env.PGHOST!,
PGPORT: process.env.PGPORT!,
PGDATABASE: process.env.PGDATABASE!,
PGUSER: process.env.PGUSER!,
PGPASSWORD: process.env.PGPASSWORD!,
}
}
}

export class AwsShopNodejsBackStack extends cdk.Stack {
constructor(scope: Construct, props?: cdk.StackProps) {
const APP_PREFIX = "bw-aws-shop-backend";
constructor(scope: Construct, props?: StackProps) {
const APP_PREFIX = "bw-aws-shop-backnd";
super(scope, `${APP_PREFIX}-stack`, props);

const lambdaPolicy = new Policy(this, `${APP_PREFIX}-dynamodb-read-policy`, {
statements: [
new PolicyStatement({
actions: [
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:PutItem",
],
resources: [
`arn:aws:dynamodb:*:*:table/${process.env.DB_TABLE_PRODUCTS}`,
`arn:aws:dynamodb:*:*:table/${process.env.DB_TABLE_STOCKS}`
],
}),
],
});

const sharedProps: Partial<NodejsFunctionProps> = {
entry: './src/index.ts',
runtime: lambda.Runtime.NODEJS_18_X,
entry: './src/handlers/index.ts',
runtime: Runtime.NODEJS_18_X,
environment: this.getEnvironment(),
};

const getProductList = new NodejsFunction(this, `${APP_PREFIX}-get-product-list-lambda`, {
const getProductListLambda = new NodejsFunction(this, `${APP_PREFIX}-get-product-list-lambda`, {
...sharedProps,
functionName: "getProductList",
handler: "getAllProducts",
environment: {
TABLE_PRODUCTS: process.env.DB_TABLE_PRODUCTS!,
TABLE_STOCKS: process.env.DB_TABLE_STOCKS!,
},
});

const getProductById = new NodejsFunction(this, `${APP_PREFIX}-get-product-by-id-lambda`, {
const getProductByIdLambda = new NodejsFunction(this, `${APP_PREFIX}-get-product-by-id-lambda`, {
...sharedProps,
functionName: "getProductById",
handler: "getProductById",
environment: {
TABLE_PRODUCTS: process.env.DB_TABLE_PRODUCTS!,
TABLE_STOCKS: process.env.DB_TABLE_STOCKS!,
},
});

const createProductLambda = new NodejsFunction(this, `${APP_PREFIX}-create-product-lambda`, {
...sharedProps,
functionName: "createProduct",
handler: "createProduct",
});

getProductListLambda.role?.attachInlinePolicy(lambdaPolicy);
getProductByIdLambda.role?.attachInlinePolicy(lambdaPolicy);
createProductLambda.role?.attachInlinePolicy(lambdaPolicy);

const api = new HttpApi(this, `${APP_PREFIX}-products-api`, {
corsPreflight: {
allowHeaders: ["*"],
Expand All @@ -48,17 +90,23 @@ export class AwsShopNodejsBackStack extends cdk.Stack {
});

api.addRoutes({
integration: new HttpLambdaIntegration(`${APP_PREFIX}-getProductLst-integration`, getProductList),
integration: new HttpLambdaIntegration(`${APP_PREFIX}-getProductLst-integration`, getProductListLambda),
path: "/products",
methods: [HttpMethod.GET]
});


api.addRoutes({
integration: new HttpLambdaIntegration(`${APP_PREFIX}-getProductById-integration`, getProductById, {
integration: new HttpLambdaIntegration(`${APP_PREFIX}-getProductById-integration`, getProductByIdLambda, {
parameterMapping: new ParameterMapping().appendQueryString('productId', MappingValue.requestPathParam('productId'))}),
path: "/products/{productId}",
methods: [HttpMethod.GET]
});

api.addRoutes({
integration: new HttpLambdaIntegration(`${APP_PREFIX}-createProduct-integration`, createProductLambda),
path: "/products",
methods: [HttpMethod.POST]
});
}
}
Loading