Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ローカルで検証する時に自動で環境変数が入るように #74

Merged
merged 3 commits into from
Sep 22, 2023
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ npm run cdk:deploy -- -c openAiApiKeySecretArn=<Secret ARN>

開発者用にローカル環境を構築する手順を説明します。なお、ローカル環境を構築する場合も、前述した AWS へのデプロイは完了している必要があります。

### Unix 系コマンドが使えるユーザー (Linux, MacOS 等)

```bash
npm run web:devw
```

### その他のユーザー (Windows 等)

デプロイ完了時に表示される Outputs から API の Endpoint (Output key = APIApiEndpoint...)、Cognito User Pool ID (Output key = AuthUserPoolId...)、Cognito User Pool Client ID (Output Key = AuthUserPoolClientId...) 、Cognito Identity Pool ID (Output Key = AuthIdPoolId...)、レスポンスストリーミングの Lambda 関数の ARN (Output Key = APIPredictStreamFunctionArn...) を取得します。
デプロイ時の出力が消えている場合、[CloudFormation](https://console.aws.amazon.com/cloudformation/home) の GenerativeAiUseCasesStack をクリックして Outputs タブから確認できます。
それらの値を以下のように環境変数に設定してください。
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"lint": "run-s root:lint web:lint cdk:lint",
"root:lint": "npx prettier --write .",
"web:devw": "source setup-env.sh && npm -w packages/web run dev",
"web:dev": "npm -w packages/web run dev",
"web:build": "npm -w packages/web run build",
"web:lint": "npm -w packages/web run lint",
Expand Down
6 changes: 1 addition & 5 deletions packages/cdk/lib/construct/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duration, CfnOutput } from 'aws-cdk-lib';
import { Duration } from 'aws-cdk-lib';
import {
AuthorizationType,
CognitoUserPoolsAuthorizer,
Expand Down Expand Up @@ -277,9 +277,5 @@ export class Api extends Construct {

this.api = api;
this.predictStreamFunction = predictStreamFunction;

new CfnOutput(this, 'PredictStreamFunctionArn', {
value: predictStreamFunction.functionArn,
});
}
}
6 changes: 1 addition & 5 deletions packages/cdk/lib/construct/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CfnOutput, Duration } from 'aws-cdk-lib';
import { Duration } from 'aws-cdk-lib';
import { UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
import {
IdentityPool,
Expand Down Expand Up @@ -46,9 +46,5 @@ export class Auth extends Construct {
this.client = client;
this.userPool = userPool;
this.idPool = idPool;

new CfnOutput(this, 'UserPoolId', { value: userPool.userPoolId });
new CfnOutput(this, 'UserPoolClientId', { value: client.userPoolClientId });
new CfnOutput(this, 'IdPoolId', { value: idPool.identityPoolId });
}
}
20 changes: 20 additions & 0 deletions packages/cdk/lib/generative-ai-use-cases-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@ export class GenerativeAiUseCasesStack extends Stack {
predictStreamFunctionArn: api.predictStreamFunction.functionArn,
});

new CfnOutput(this, 'Region', {
value: this.region,
});

new CfnOutput(this, 'WebUrl', {
value: `https://${web.distribution.domainName}`,
});

new CfnOutput(this, 'ApiEndpoint', {
value: api.api.url,
});

new CfnOutput(this, 'UserPoolId', { value: auth.userPool.userPoolId });

new CfnOutput(this, 'UserPoolClientId', {
value: auth.client.userPoolClientId,
});

new CfnOutput(this, 'IdPoolId', { value: auth.idPool.identityPoolId });

new CfnOutput(this, 'PredictStreamFunctionArn', {
value: api.predictStreamFunction.functionArn,
});
}
}
21 changes: 21 additions & 0 deletions setup-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -eu

STACK_NAME='GenerativeAiUseCasesStack'

function stack_output {
aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='$1'].OutputValue" \
--output text
}

echo 'Setup environment variables...'

export VITE_APP_API_ENDPOINT=`stack_output 'ApiEndpoint'`
export VITE_APP_REGION=`stack_output 'Region'`
export VITE_APP_USER_POOL_ID=`stack_output 'UserPoolId'`
export VITE_APP_USER_POOL_CLIENT_ID=`stack_output 'UserPoolClientId'`
export VITE_APP_IDENTITY_POOL_ID=`stack_output 'IdPoolId'`
export VITE_APP_PREDICT_STREAM_FUNCTION_ARN=`stack_output PredictStreamFunctionArn`