Skip to content

Commit c087fbf

Browse files
committed
Get env variables
1 parent 21a7994 commit c087fbf

File tree

1 file changed

+55
-75
lines changed

1 file changed

+55
-75
lines changed

backend/src/iac/backend-stack.ts

Lines changed: 55 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,51 @@ export class BackendStack extends cdk.Stack {
4040
removalPolicy: isProd ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
4141
});
4242

43+
// Create DynamoDB table for reports
44+
const reportsTable = new Table(this, `${appName}ReportsTable-${props.environment}`, {
45+
tableName: `${appName}ReportsTable${props.environment}`,
46+
partitionKey: {
47+
name: 'userId',
48+
type: AttributeType.STRING,
49+
},
50+
sortKey: {
51+
name: 'id',
52+
type: AttributeType.STRING,
53+
},
54+
billingMode: BillingMode.PAY_PER_REQUEST,
55+
removalPolicy: isProd ? RemovalPolicy.RETAIN : RemovalPolicy.DESTROY,
56+
});
57+
58+
// Add a GSI for querying by date (most recent first)
59+
reportsTable.addGlobalSecondaryIndex({
60+
indexName: 'userIdDateIndex',
61+
partitionKey: {
62+
name: 'userId',
63+
type: AttributeType.STRING,
64+
},
65+
sortKey: {
66+
name: 'date',
67+
type: AttributeType.STRING,
68+
},
69+
});
70+
71+
// Look up existing Cognito User Pool
72+
const userPoolId = process.env.AWS_COGNITO_CLIENT_ID || cognito.UserPool.fromUserPoolId(
73+
this,
74+
`${appName}UserPool`,
75+
'us-east-1_PszlvSmWc',
76+
).userPoolId;
77+
78+
// Create a Cognito domain if it doesn't exist
79+
const userPoolDomain = cognito.UserPoolDomain.fromDomainName(
80+
this,
81+
`${appName}ExistingDomain-${props.environment}`,
82+
'us-east-1pszlvsmwc', // The domain prefix without the .auth.region.amazoncognito.com part
83+
);
84+
85+
// Replace the userPoolClient reference with a direct reference to the client ID
86+
const userPoolClientId = process.env.AWS_COGNITO_CLIENT_ID || 'default-client-id';
87+
4388
// Task Definition
4489
const taskDefinition = new ecs.FargateTaskDefinition(
4590
this,
@@ -59,7 +104,17 @@ export class BackendStack extends cdk.Stack {
59104
},
60105
}),
61106
environment: {
107+
// Basic environment variables
62108
NODE_ENV: props.environment,
109+
PORT: '3000',
110+
111+
// AWS related
112+
AWS_REGION: this.region,
113+
AWS_COGNITO_USER_POOL_ID: userPoolId,
114+
AWS_COGNITO_CLIENT_ID: userPoolClientId,
115+
DYNAMODB_REPORTS_TABLE: reportsTable.tableName,
116+
117+
// Perplexity related
63118
PERPLEXITY_API_KEY_SECRET_NAME: `medical-reports-explainer/${props.environment}/perplexity-api-key`,
64119
PERPLEXITY_MODEL: 'sonar',
65120
PERPLEXITY_MAX_TOKENS: '2048',
@@ -75,20 +130,6 @@ export class BackendStack extends cdk.Stack {
75130
protocol: ecs.Protocol.TCP,
76131
});
77132

78-
// Look up existing Cognito User Pool
79-
const userPool = cognito.UserPool.fromUserPoolId(
80-
this,
81-
`${appName}UserPool`,
82-
'us-east-1_PszlvSmWc',
83-
);
84-
85-
// Create a Cognito domain if it doesn't exist
86-
const userPoolDomain = cognito.UserPoolDomain.fromDomainName(
87-
this,
88-
`${appName}ExistingDomain-${props.environment}`,
89-
'us-east-1pszlvsmwc', // The domain prefix without the .auth.region.amazoncognito.com part
90-
);
91-
92133
// 1. Create ALB
93134
const alb = new elbv2.ApplicationLoadBalancer(this, `${appName}ALB-${props.environment}`, {
94135
vpc,
@@ -145,29 +186,6 @@ export class BackendStack extends cdk.Stack {
145186
// 7. Now register the service with the target group
146187
targetGroup.addTarget(fargateService);
147188

148-
// Create a Cognito User Pool Client for the ALB
149-
const userPoolClient = new cognito.UserPoolClient(
150-
this,
151-
`${appName}UserPoolClient-${props.environment}`,
152-
{
153-
userPool,
154-
generateSecret: true,
155-
authFlows: {
156-
userPassword: true,
157-
userSrp: true,
158-
},
159-
oAuth: {
160-
flows: {
161-
authorizationCodeGrant: true,
162-
},
163-
// Update callback URLs to use HTTPS
164-
callbackUrls: props.domainName
165-
? [`https://${props.domainName}/oauth2/idpresponse`]
166-
: [`https://${alb.loadBalancerDnsName}/oauth2/idpresponse`],
167-
},
168-
},
169-
);
170-
171189
// Add autoscaling for production
172190
if (isProd) {
173191
const scaling = fargateService.autoScaleTaskCount({
@@ -182,34 +200,6 @@ export class BackendStack extends cdk.Stack {
182200
});
183201
}
184202

185-
// Create DynamoDB table for reports
186-
const reportsTable = new Table(this, `${appName}ReportsTable-${props.environment}`, {
187-
tableName: `${appName}ReportsTable${props.environment}`,
188-
partitionKey: {
189-
name: 'userId',
190-
type: AttributeType.STRING,
191-
},
192-
sortKey: {
193-
name: 'id',
194-
type: AttributeType.STRING,
195-
},
196-
billingMode: BillingMode.PAY_PER_REQUEST,
197-
removalPolicy: isProd ? RemovalPolicy.RETAIN : RemovalPolicy.DESTROY,
198-
});
199-
200-
// Add a GSI for querying by date (most recent first)
201-
reportsTable.addGlobalSecondaryIndex({
202-
indexName: 'userIdDateIndex',
203-
partitionKey: {
204-
name: 'userId',
205-
type: AttributeType.STRING,
206-
},
207-
sortKey: {
208-
name: 'date',
209-
type: AttributeType.STRING,
210-
},
211-
});
212-
213203
// Add output for the table name
214204
new cdk.CfnOutput(this, 'ReportsTableName', {
215205
value: reportsTable.tableName,
@@ -227,15 +217,5 @@ export class BackendStack extends cdk.Stack {
227217
value: alb.loadBalancerDnsName,
228218
description: 'Load Balancer DNS Name',
229219
});
230-
231-
new cdk.CfnOutput(this, 'UserPoolId', {
232-
value: userPool.userPoolId,
233-
description: 'Cognito User Pool ID',
234-
});
235-
236-
new cdk.CfnOutput(this, 'UserPoolClientId', {
237-
value: userPoolClient.userPoolClientId,
238-
description: 'Cognito User Pool Client ID',
239-
});
240220
}
241221
}

0 commit comments

Comments
 (0)