|
| 1 | +import * as cdk from "aws-cdk-lib"; |
| 2 | +import * as lambda from "aws-cdk-lib/aws-lambda"; |
| 3 | +import * as apigateway from "aws-cdk-lib/aws-apigateway"; |
| 4 | +import * as iam from "aws-cdk-lib/aws-iam"; |
| 5 | +import * as logs from "aws-cdk-lib/aws-logs"; |
| 6 | +import { Construct } from "constructs"; |
| 7 | + |
| 8 | +export class ApigwStreamingLambdaBedrockStack extends cdk.Stack { |
| 9 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 10 | + super(scope, id, props); |
| 11 | + |
| 12 | + const modelId = new cdk.CfnParameter(this, "BedrockModelId", { |
| 13 | + type: "String", |
| 14 | + default: "us.anthropic.claude-sonnet-4-20250514-v1:0", |
| 15 | + description: "Bedrock model ID (inference profile) to use", |
| 16 | + }); |
| 17 | + |
| 18 | + // Streaming Lambda function |
| 19 | + const fn = new lambda.Function(this, "StreamingBedrockFn", { |
| 20 | + runtime: lambda.Runtime.NODEJS_22_X, |
| 21 | + handler: "index.handler", |
| 22 | + code: lambda.Code.fromAsset("src"), |
| 23 | + timeout: cdk.Duration.minutes(5), // Must be >= API Gateway integration timeout (5 min) to avoid premature termination |
| 24 | + memorySize: 256, |
| 25 | + environment: { |
| 26 | + MODEL_ID: modelId.valueAsString, |
| 27 | + }, |
| 28 | + logRetention: logs.RetentionDays.ONE_WEEK, |
| 29 | + }); |
| 30 | + |
| 31 | + fn.addToRolePolicy( |
| 32 | + new iam.PolicyStatement({ |
| 33 | + actions: ["bedrock:InvokeModelWithResponseStream"], |
| 34 | + resources: [ |
| 35 | + `arn:aws:bedrock:${this.region}:${this.account}:inference-profile/${modelId.valueAsString}`, |
| 36 | + "arn:aws:bedrock:*::foundation-model/*", |
| 37 | + ], |
| 38 | + }) |
| 39 | + ); |
| 40 | + |
| 41 | + // REST API with streaming integration |
| 42 | + const api = new apigateway.RestApi(this, "StreamingApi", { |
| 43 | + restApiName: "Bedrock Streaming API", |
| 44 | + description: "API Gateway REST API with response streaming to Bedrock", |
| 45 | + deployOptions: { stageName: "prod" }, |
| 46 | + }); |
| 47 | + |
| 48 | + const chatResource = api.root.addResource("chat"); |
| 49 | + |
| 50 | + // Add POST method with standard Lambda proxy integration |
| 51 | + const method = chatResource.addMethod( |
| 52 | + "POST", |
| 53 | + // Override default 29s API Gateway timeout to allow streaming responses to complete |
| 54 | + new apigateway.LambdaIntegration(fn, { timeout: cdk.Duration.minutes(5) }) |
| 55 | + ); |
| 56 | + |
| 57 | + // Override integration URI to use response-streaming-invocations path |
| 58 | + const cfnMethod = method.node.defaultChild as apigateway.CfnMethod; |
| 59 | + cfnMethod.addPropertyOverride( |
| 60 | + "Integration.Uri", |
| 61 | + `arn:aws:apigateway:${this.region}:lambda:path/2021-11-15/functions/${fn.functionArn}/response-streaming-invocations` |
| 62 | + ); |
| 63 | + cfnMethod.addPropertyOverride( |
| 64 | + "Integration.ResponseTransferMode", |
| 65 | + "STREAM" |
| 66 | + ); |
| 67 | + cfnMethod.addPropertyOverride("Integration.TimeoutInMillis", 300000); |
| 68 | + |
| 69 | + new cdk.CfnOutput(this, "ApiEndpoint", { |
| 70 | + value: api.urlForPath("/chat"), |
| 71 | + description: "POST your prompt to this URL to stream Bedrock responses", |
| 72 | + }); |
| 73 | + new cdk.CfnOutput(this, "FunctionName", { value: fn.functionName }); |
| 74 | + } |
| 75 | +} |
0 commit comments