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

Bedrock API追加 #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion bin/jpk2024-broadcast-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { Jpk2024BroadcastBackendTranslateFunctionStack } from '../lib/jpk2024-br
import { Jpk2024BroadcastBackendDbStack } from '../lib/jpk2024-broadcast-backend-db-stack';
import { Jpk2024BroadcastBackendIvsViewersCountStack } from '../lib/jpk2024-broadcast-backend-ivs-viewers-count-stack';
import { Jpk2024BroadcastBackendConnectIvsChatStack } from '../lib/jpk2024-broadcast-backend-connect-ivs-chat-stack';
import { Jpk2024BroadcastBackendSummaryFunctionStack } from '../lib/jpk2024-broadcast-backend-summary-function-stack';

const app = new cdk.App();
new Jpk2024BroadcastBackendDbStack(app, 'Jpk2024BroadcastBackendDbStack');
new Jpk2024BroadcastBackendTranslateFunctionStack(app, 'Jpk2024BroadcastBackendTranslateFunctionStack');
new Jpk2024BroadcastBackendIvsViewersCountStack(app, 'Jpk2024BroadcastBackendIvsViewersCountStack');
new Jpk2024BroadcastBackendConnectIvsChatStack(app, 'Jpk2024BroadcastBackendConnectIvsChatStack');
new Jpk2024BroadcastBackendConnectIvsChatStack(app, 'Jpk2024BroadcastBackendConnectIvsChatStack');
new Jpk2024BroadcastBackendSummaryFunctionStack(app, 'Jpk2024BroadcastBackendSummaryFunctionStack');
8 changes: 4 additions & 4 deletions lib/jpk2024-broadcast-backend-db-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export class Jpk2024BroadcastBackendDbStack extends Stack {
/**
* 2024/8/17 Change date type to String.
*/
sortKey: {
name: 'date',
type: AttributeType.NUMBER
},
partitionKey: {
name: 'sessionId',
type: AttributeType.STRING
},
sortKey: {
name: 'date',
type: AttributeType.STRING
},
tableName: 'translateResults',
removalPolicy: RemovalPolicy.RETAIN
})
Expand Down
78 changes: 78 additions & 0 deletions lib/jpk2024-broadcast-backend-summary-function-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Stack, StackProps, Duration, Fn } from 'aws-cdk-lib'
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'
import { ITableV2, TableV2 } from 'aws-cdk-lib/aws-dynamodb';
import { RestApi, Model, JsonSchemaType, Cors } from 'aws-cdk-lib/aws-apigateway'
import { LambdaIntegration } from 'aws-cdk-lib/aws-apigateway'
import { Runtime } from 'aws-cdk-lib/aws-lambda'
import { Construct } from 'constructs'
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam'

export class Jpk2024BroadcastBackendSummaryFunctionStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const translateResultTableArn = Fn.importValue('translateResultTableArn');
const translateResultTable: ITableV2 = TableV2.fromTableArn(this, 'translateResult', translateResultTableArn);
const summaryResultTableArn = Fn.importValue('summaryResultTableArn');
const summaryResultTable: ITableV2 = TableV2.fromTableArn(this, 'summaryResultTable', summaryResultTableArn);

const summaryFunction = new NodejsFunction(this, 'summary-function', {
runtime: Runtime.NODEJS_18_X,
functionName: 'summaryFunction',
entry: 'src/summary-function.handler.ts',
timeout: Duration.seconds(60),
logRetention: 30,
environment: {
RESULT_TABLE_NAME: 'translateResults',
RESULT_TABLE_PRIMARY_KEY: 'sessionId',
RESULT_TABLE_SORT_KEY: 'date',
SUMMARY_TABLE_NAME: summaryResultTable.tableName,
SUMMARY_TABLE_PRIMARY_KEY: 'sessionId',
SEARCHWORD: 'F300'
},
})
// add readWrite permission to the translateResultTable
translateResultTable.grantReadData(summaryFunction);
summaryResultTable.grantReadWriteData(summaryFunction);

summaryFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"],
resources: ["*"],
})
)

const restApi = new RestApi(this, 'summary-function-rest-api', {
restApiName: 'RestApiForSummaryFunction',
deployOptions: {
stageName: 'v1',
},
})

const restApiTranslateResource = restApi.root.addResource('summary', {
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
allowMethods: Cors.ALL_METHODS,
allowHeaders: Cors.DEFAULT_HEADERS,
statusCode: 200,
},
})

const summaryModel: Model = restApi.addModel('summaryModel', {
schema: {
type: JsonSchemaType.OBJECT,
properties: {
sessionId: {
type: JsonSchemaType.STRING,
}
},
required: ['sessionId'],
},
})

restApiTranslateResource.addMethod('POST', new LambdaIntegration(summaryFunction), {
requestModels: { 'application/json': summaryModel },
})
}
}
Loading