5
5
aws_iam as iam ,
6
6
aws_lambda as lambda ,
7
7
} from 'aws-cdk-lib'
8
+ import { HttpApi , HttpMethod } from 'aws-cdk-lib/aws-apigatewayv2'
9
+ import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations'
8
10
import type { Construct } from 'constructs'
9
11
import { config } from '@stacksjs/config'
10
12
import type { NestedCloudProps } from '../types'
@@ -23,8 +25,8 @@ export class AiStack {
23
25
24
26
const bedrockAccessPolicy = new iam . PolicyStatement ( {
25
27
effect : iam . Effect . ALLOW ,
26
- actions : [ 'bedrock:InvokeModel' ] , // See: https://docs.aws.amazon.com/ja_jp/service-authorization/latest/reference/list_amazonbedrock.html
27
- resources : [ '*' ] ,
28
+ actions : [ 'bedrock:InvokeModel' ] ,
29
+ resources : config . ai . models ?. map ( model => `arn:aws:bedrock:us-east-1::foundation-model/ ${ model } ` ) ,
28
30
} )
29
31
30
32
const bedrockAccessRole = new iam . Role ( scope , 'BedrockAccessRole' , {
@@ -36,14 +38,6 @@ export class AiStack {
36
38
37
39
bedrockAccessRole . addToPolicy ( bedrockAccessPolicy )
38
40
39
- // aiRole.addToPolicy(
40
- // new iam.PolicyStatement({
41
- // actions: ['bedrock:InvokeModel'],
42
- // resources: config.ai.models?.map(model => `arn:aws:bedrock:us-east-1:${props.env.account}:foundation-model/${model}`),
43
- // effect: iam.Effect.ALLOW,
44
- // }),
45
- // )
46
-
47
41
const aiLambda = new lambda . Function ( scope , 'AiFunction' , {
48
42
functionName : `${ props . slug } -${ props . appEnv } -ai` ,
49
43
description : 'Lambda function to invoke the AI model' ,
@@ -55,16 +49,24 @@ export class AiStack {
55
49
timeout : Duration . seconds ( 30 ) ,
56
50
} )
57
51
58
- const api = new lambda . FunctionUrl ( scope , 'AiLambdaUrl' , {
59
- function : aiLambda ,
60
- authType : lambda . FunctionUrlAuthType . NONE ,
61
- cors : {
62
- allowedOrigins : [ '*' ] ,
63
- } ,
52
+ const api = new HttpApi ( scope , 'AiApi' )
53
+
54
+ api . addRoutes ( {
55
+ path : '/prompt' ,
56
+ methods : [ HttpMethod . POST ] ,
57
+ integration : new HttpLambdaIntegration ( 'AiIntegration' , aiLambda ) ,
64
58
} )
65
59
60
+ // const api = new lambda.FunctionUrl(scope, 'AiLambdaUrl', {
61
+ // function: aiLambda,
62
+ // authType: lambda.FunctionUrlAuthType.NONE,
63
+ // cors: {
64
+ // allowedOrigins: ['*'],
65
+ // },
66
+ // })
67
+
66
68
new Output ( scope , 'AiApiUrl' , {
67
- value : api . url ,
69
+ value : api . apiEndpoint ,
68
70
} )
69
71
}
70
72
}
0 commit comments