@@ -7,28 +7,35 @@ import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations
77import { RetentionDays } from 'aws-cdk-lib/aws-logs' ;
88import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs' ;
99import { ServicePrincipal } from 'aws-cdk-lib/aws-iam' ;
10+ import { getResourceName } from '../utils/getResourceName' ;
1011
12+ interface AuthStackProps extends cdk . StackProps {
13+ envName : string ;
14+ }
1115
1216export class AuthStack extends cdk . Stack {
13- constructor ( scope : Construct , id : string , props ?: cdk . StackProps ) {
17+ constructor ( scope : Construct , id : string , props : AuthStackProps ) {
1418 super ( scope , id , props ) ;
1519
16- const httpApiId = cdk . Fn . importValue ( 'BeejhoHttpApiId' ) ;
17- const httpApi = HttpApi . fromHttpApiAttributes ( this , 'BeejhoHttpApi' , {
20+ const { envName } = props ;
21+
22+ const httpApiId = cdk . Fn . importValue ( getResourceName ( envName , 'BeejhoHttpApiId' ) ) ;
23+ const httpApi = HttpApi . fromHttpApiAttributes ( this , getResourceName ( envName , 'BeejhoHttpApiId' ) , {
1824 httpApiId
19- } )
25+ } ) ;
2026
21- const userTable = dynamodb . Table . fromTableName ( this , 'OrderTable' , 'users' )
22- const restaurantTable = dynamodb . Table . fromTableName ( this , 'RestaurantTable' , 'restaurants' )
27+ const userTable = dynamodb . Table . fromTableName ( this , getResourceName ( envName , 'UsersTable' ) , getResourceName ( envName , 'users' ) ) ;
28+ const restaurantTable = dynamodb . Table . fromTableName ( this , getResourceName ( envName , 'RestaurantTable' ) , getResourceName ( envName , 'restaurants' ) ) ;
2329
24- const userLoginLambda = new NodejsFunction ( this , 'LoginUser' , {
30+ const userLoginLambda = new NodejsFunction ( this , getResourceName ( envName , 'LoginUser' ) , {
2531 runtime : lambda . Runtime . NODEJS_LATEST ,
2632 entry : './src/lambdas/auth.ts' ,
2733 handler : 'userLoginHandler' ,
28- functionName : 'loginUser' ,
34+ functionName : getResourceName ( envName , 'LoginUser' ) ,
2935 architecture : lambda . Architecture . ARM_64 ,
3036 environment : {
3137 JWT_SECRET : 'Abhi2199@321#' ,
38+ envName,
3239 } ,
3340 logRetention : RetentionDays . ONE_DAY ,
3441 bundling : {
@@ -37,59 +44,64 @@ export class AuthStack extends cdk.Stack {
3744 } ) ;
3845 userTable . grantReadWriteData ( userLoginLambda ) ;
3946
40- new HttpRoute ( this , 'LoginRoute' , {
47+ new HttpRoute ( this , getResourceName ( envName , 'LoginRoute' ) , {
4148 httpApi,
4249 routeKey : HttpRouteKey . with ( '/v0/login' , HttpMethod . POST ) ,
43- integration : new HttpLambdaIntegration ( 'LoginrUserLambdaIntegration' , userLoginLambda ) ,
50+ integration : new HttpLambdaIntegration ( getResourceName ( envName , 'LoginUserLambdaIntegration' ) , userLoginLambda ) ,
4451 } ) ;
4552
46- const generateOTPLambda = new NodejsFunction ( this , 'GenerateOTP' , {
53+ const generateOTPLambda = new NodejsFunction ( this , getResourceName ( envName , 'GenerateOTP' ) , {
4754 runtime : lambda . Runtime . NODEJS_LATEST ,
4855 entry : './src/lambdas/auth.ts' ,
4956 handler : 'generateOTPHandler' ,
50- functionName : 'generateOTP' ,
57+ functionName : getResourceName ( envName , 'GenerateOTP' ) ,
5158 architecture : lambda . Architecture . ARM_64 ,
59+ environment : {
60+ JWT_SECRET : 'Abhi2199@321#' ,
61+ envName,
62+ } ,
5263 logRetention : RetentionDays . ONE_DAY ,
5364 bundling : {
5465 externalModules : [ 'aws-sdk' ]
5566 } ,
5667 } ) ;
57- userTable . grantWriteData ( generateOTPLambda ) ;
5868
59- new HttpRoute ( this , 'GenerateOTPRoute' , {
69+ new HttpRoute ( this , getResourceName ( envName , 'GenerateOTPRoute' ) , {
6070 httpApi,
6171 routeKey : HttpRouteKey . with ( '/v0/generateOTP' , HttpMethod . POST ) ,
62- integration : new HttpLambdaIntegration ( 'GenerateOTPLambdaIntegration' , generateOTPLambda ) ,
63- } )
72+ integration : new HttpLambdaIntegration ( getResourceName ( envName , 'GenerateOTPLambdaIntegration' ) , generateOTPLambda ) ,
73+ } ) ;
74+ userTable . grantWriteData ( generateOTPLambda ) ;
6475
65- const userAuthorizerLambda = new NodejsFunction ( this , 'UserAuthorizerLambda' , {
76+ const userAuthorizerLambda = new NodejsFunction ( this , getResourceName ( envName , 'UserAuthorizerLambda' ) , {
6677 runtime : lambda . Runtime . NODEJS_LATEST ,
6778 entry : './src/lambdas/auth.ts' ,
6879 handler : 'userAuthorizerHandler' ,
69- functionName : 'userAuthorizer' ,
80+ functionName : getResourceName ( envName , 'UserAuthorizer' ) ,
7081 architecture : lambda . Architecture . ARM_64 ,
7182 environment : {
7283 JWT_SECRET : 'Abhi2199@321#' ,
84+ envName,
7385 } ,
7486 logRetention : RetentionDays . ONE_DAY ,
7587 bundling : {
7688 externalModules : [ 'aws-sdk' ]
7789 } ,
7890 } ) ;
7991
80- const userAuthorizer = new HttpAuthorizer ( this , 'UserAuthorizer' , {
92+ const userAuthorizer = new HttpAuthorizer ( this , getResourceName ( envName , 'UserAuthorizer' ) , {
8193 httpApi,
8294 type : HttpAuthorizerType . LAMBDA ,
8395 identitySource : [ '$request.header.Authorization' ] ,
8496 enableSimpleResponses : true ,
8597 payloadFormatVersion : AuthorizerPayloadVersion . VERSION_2_0 ,
8698 authorizerUri : `arn:aws:apigateway:${ this . region } :lambda:path/2015-03-31/functions/${ userAuthorizerLambda . functionArn } /invocations` ,
87- } )
99+ } ) ;
88100
89101 // Export the authorizer ID
90- new cdk . CfnOutput ( this , 'UserAuthorizerId' , {
102+ new cdk . CfnOutput ( this , getResourceName ( envName , 'UserAuthorizerId' ) , {
91103 value : userAuthorizer . authorizerId ,
92- exportName : 'UserAuthorizerId' ,
104+ exportName : getResourceName ( envName , 'UserAuthorizerId' ) ,
93105 } ) ;
94106
95107 // Grant permission to API Gateway to invoke the Lambda authorizer
@@ -99,37 +111,39 @@ export class AuthStack extends cdk.Stack {
99111 sourceArn : `arn:aws:execute-api:${ this . region } :${ this . account } :${ httpApiId } /*` ,
100112 } ) ;
101113
102- const restaurantLoginLambda = new NodejsFunction ( this , 'RestaurantLogin' , {
114+ const restaurantLoginLambda = new NodejsFunction ( this , getResourceName ( envName , 'RestaurantLogin' ) , {
103115 runtime : lambda . Runtime . NODEJS_LATEST ,
104116 entry : './src/lambdas/auth.ts' ,
105117 handler : 'restaurantLoginHandler' ,
106- functionName : 'restaurantLogin' ,
118+ functionName : getResourceName ( envName , 'RestaurantLogin' ) ,
107119 architecture : lambda . Architecture . ARM_64 ,
108120 logRetention : RetentionDays . ONE_DAY ,
109121 bundling : {
110122 externalModules : [ 'aws-sdk' ]
111123 } ,
112124 environment : {
113125 JWT_SECRET : 'Abhi2199@321#' ,
126+ envName,
114127 } ,
115128 memorySize : 512 //bcrypt comparison is expensive
116129 } ) ;
117130 restaurantTable . grantReadData ( restaurantLoginLambda ) ;
118131
119- new HttpRoute ( this , 'RestaurantLoginRoute' , {
132+ new HttpRoute ( this , getResourceName ( envName , 'RestaurantLoginRoute' ) , {
120133 httpApi,
121134 routeKey : HttpRouteKey . with ( '/v0/restaurantLogin' , HttpMethod . POST ) ,
122- integration : new HttpLambdaIntegration ( 'RestaurantLoginLambdaIntegration' , restaurantLoginLambda ) ,
123- } )
135+ integration : new HttpLambdaIntegration ( getResourceName ( envName , 'RestaurantLoginLambdaIntegration' ) , restaurantLoginLambda ) ,
136+ } ) ;
124137
125- const restaurantAuthorizerLambda = new NodejsFunction ( this , 'RestaurantAuthorizerLambda' , {
138+ const restaurantAuthorizerLambda = new NodejsFunction ( this , getResourceName ( envName , 'RestaurantAuthorizerLambda' ) , {
126139 runtime : lambda . Runtime . NODEJS_LATEST ,
127140 entry : './src/lambdas/auth.ts' ,
128141 handler : 'restaurantAuthorizerHandler' ,
129- functionName : 'restaurantAuthorizer' ,
142+ functionName : getResourceName ( envName , 'RestaurantAuthorizer' ) ,
130143 architecture : lambda . Architecture . ARM_64 ,
131144 environment : {
132145 JWT_SECRET : 'Abhi2199@321#' ,
146+ envName,
133147 } ,
134148 logRetention : RetentionDays . ONE_DAY ,
135149 bundling : {
@@ -138,25 +152,25 @@ export class AuthStack extends cdk.Stack {
138152 } ) ;
139153
140154
141- const restaurantAuthorizer = new HttpAuthorizer ( this , 'RestaurantAuthorizer' , {
155+ const restaurantAuthorizer = new HttpAuthorizer ( this , getResourceName ( envName , 'RestaurantAuthorizer' ) , {
142156 httpApi,
143157 type : HttpAuthorizerType . LAMBDA ,
144158 identitySource : [ '$request.header.Authorization' ] ,
145159 enableSimpleResponses : true ,
146160 payloadFormatVersion : AuthorizerPayloadVersion . VERSION_2_0 ,
147161 authorizerUri : `arn:aws:apigateway:${ this . region } :lambda:path/2015-03-31/functions/${ restaurantAuthorizerLambda . functionArn } /invocations` ,
148- } )
162+ } ) ;
149163
150- restaurantAuthorizerLambda . addPermission ( 'InvokeRestaurantAuthorizerLambda' , {
164+ restaurantAuthorizerLambda . addPermission ( getResourceName ( envName , 'InvokeRestaurantAuthorizerLambda' ) , {
151165 principal : new ServicePrincipal ( 'apigateway.amazonaws.com' ) ,
152166 action : 'lambda:InvokeFunction' ,
153167 sourceArn : `arn:aws:execute-api:${ this . region } :${ this . account } :${ httpApiId } /*` ,
154168 } ) ;
155169
156170 // Export the authorizer ID
157- new cdk . CfnOutput ( this , 'RestaurantAuthorizerId' , {
171+ new cdk . CfnOutput ( this , getResourceName ( envName , 'RestaurantAuthorizerId' ) , {
158172 value : restaurantAuthorizer . authorizerId ,
159- exportName : 'RestaurantAuthorizerId' ,
173+ exportName : getResourceName ( envName , 'RestaurantAuthorizerId' ) ,
160174 } ) ;
161175 }
162176}
0 commit comments