Skip to main content

CDK constructs for defining an interaction between an AWS Lambda function and an Amazon Bedrock inference profile.

Project description

aws-lambda-bedrockinferenceprofile module

---

Stability: Experimental

All classes are under active development and subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


Reference Documentation: https://docs.aws.amazon.com/solutions/latest/constructs/
Language Package
Python Logo Python aws_solutions_constructs.aws_lambda_bedrockinferenceprofile
Typescript Logo Typescript @aws-solutions-constructs/aws-lambda-bedrockinferenceprofile
Java Logo Java software.amazon.awsconstructs.services.lambdabedrockinferenceprofile

Overview

This AWS Solutions Construct implements a Lambda function granted access to a new Bedrock Inference Profile. Inference profiles allow:

  • Greater scalability of applications by distributing Bedrock Invoke calls across multiple regions
  • Cost management by adding Cost Allocation Tags to an inference to track costs for specific applications.

Here is a minimal deployable pattern definition:

Typescript

import { Construct } from 'constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
import { LambdaToBedrockInferenceProfile } from "@aws-solutions-constructs/aws-lambda-bedrockinferenceprofile";
import * as lambda from 'aws-cdk-lib/aws-lambda';

new LambdaToBedrockInferenceProfile(this, 'LambdaToBedrockPattern', {
    lambdaFunctionProps: {
        runtime: lambda.Runtime.NODEJS_20_X,
        handler: 'index.handler',
        code: lambda.Code.fromAsset(`lambda`)
    },
    model: "amazon.nova-lite-v1:0"
});

Python

from constructs import Construct
from aws_cdk import (
    aws_lambda as _lambda,
    Stack
)

from aws_solutions_constructs import (
    aws_lambda_bedrockinferenceprofile as lambda_bedrock
)

lambda_bedrock.LambdaToBedrockinferenceprofile(
    self, 'bedrock-construct',
    bedrock_model_id="amazon.nova-lite-v1:0",
    lambda_function_props=_lambda.FunctionProps(
        runtime=_lambda.Runtime.NODEJS_20_X,
        code=_lambda.Code.from_asset('lambda'),
        handler='index.handler',
    )
)

Java

import software.constructs.Construct;

import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.services.lambda.*;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awsconstructs.services.lambdabedrockinferenceprofile.*;

  new LambdaToBedrockinferenceprofile(this, "ApiGatewayToLambdaPattern", new LambdaToBedrockinferenceprofileProps.Builder()
          .lambdaFunctionProps(new FunctionProps.Builder()
                  .runtime(Runtime.NODEJS_20_X)
                  .code(Code.fromAsset("lambda"))
                  .handler("index.handler")
                  .build())
          .bedrockModelId("amazon.nova-lite-v1:0")
          .build());

Pattern Construct Props

Name Type Description
existingLambdaObj? lambda.Function Existing instance of Lambda Function object, providing both this and lambdaFunctionProps will cause an error.
lambdaFunctionProps? lambda.FunctionProps Optional user provided props to override the default props for the Lambda function.
existingVpc? ec2.IVpc An optional, existing VPC into which this pattern should be deployed. When deployed in a VPC, the Lambda function will use ENIs in the VPC to access network resources and an Interface Endpoint will be created in the VPC for Amazon Bedrock and Bedrock-Runtime. If an existing VPC is provided, the deployVpc property cannot be true. This uses ec2.IVpc to allow clients to supply VPCs that exist outside the stack using the ec2.Vpc.fromLookup() method.
vpcProps? ec2.VpcProps Optional user provided properties to override the default properties for the new VPC. enableDnsHostnames, enableDnsSupport, natGateways and subnetConfiguration are set by the pattern, so any values for those properties supplied here will be overridden. If deployVpc is not true then this property will be ignored.
deployVpc? boolean Whether to create a new VPC based on vpcProps into which to deploy this pattern. Setting this to true will deploy the minimal, most private VPC to run the pattern:
  • One isolated subnet in each Availability Zone used by the CDK program
  • enableDnsHostnames and enableDnsSupport will both be set to true
If this property is true then existingVpc cannot be specified. Defaults to false.
bedrockModelId string The foundation model to use with the inference profile. Depending on whether the deployment is cross region or single region, he construct will create the correct inference profile name and and assign IAM permissions to the Lambda function allowing access to the foundation model in all appropriate regions. For all of this to occur, the model must be specified here and not in inferenceProfileProps. Be certain that the account is granted access to the foundation model in all the regions covered by the cross-region inference profile.
inferenceProfileProps? bedrock.CfnApplicationInferenceProfileProps This is where you set tags required for tracking inference calls. Do not populate the copyFrom attribute - the construct will populate this based upon the model sent in bedrockModelId (this allows the construct to correctly create all the other dependencies like the required IAM policies). If the copyFrom attribute is supplied here the construct will throw an error. The construct will also set a unique, stack specific inferenceProfileName - you may override that name here, but it is not recommended.
deployCrossRegionProfile boolean Whether to deploy a cross-region inference profile that will automatically distribute Invoke calls across multiple regions. Note that at the time of this writing, cross-region profiles are only available in US, EMEA and APAC. Single region profiles are available in every region supporting Bedrock models. Defaults to true
foundationModelEnvironmentVariableName? string Optional Name for the Lambda function environment variable set to the Model name. Defaults to BEDROCK_MODEL
inferenceProfileEnvironmentVariableName? string Optional Name for the Lambda function environment variable set to the inference profile arn. Defaults to BEDROCK_PROFILE

Pattern Properties

Name Type Description
lambdaFunction lambda.Function Returns an instance of the Lambda function created by the pattern.
inferenceProfile CfnApplicationInferenceProfile The inference profile created by the construct.
vpc? ec2.IVpc Returns an interface on the VPC used by the pattern (if any). This may be a VPC created by the pattern or the VPC supplied to the pattern constructor.

Default settings

Out of the box implementation of the Construct without any override will set the following defaults:

AWS Lambda Function

  • Configure limited privilege access IAM role for Lambda function, granting Invoke privileges for:

    • The new inference profile
    • The appropriate foundation model in all regions in the geographic area. For single region inference profiles, access is only granted to model in the current region.
  • Enable reusing connections with Keep-Alive for NodeJs Lambda function

  • Enable X-Ray Tracing

  • Set Environment Variables

    • (default) BEDROCK_PROFILE
    • (default) BEDROCK_MODEL

Amazon Bedrock Inference Profile

  • Cross-region inference profile for provided model by default
  • Geographic area prefix in arn defaults to value appropriate for deployment region (e.g. would us 'us' for us-east-1 deployment)

Architecture

Architecture Diagram


© Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file aws_solutions_constructs_aws_lambda_bedrockinferenceprofile-2.85.2.tar.gz.

File metadata

File hashes

Hashes for aws_solutions_constructs_aws_lambda_bedrockinferenceprofile-2.85.2.tar.gz
Algorithm Hash digest
SHA256 6df8bfa38216b04bc7f575b403e3f13f18942c49f52de33188c56845c9cf92cb
MD5 cc102a25cc99c61e31f5749897089753
BLAKE2b-256 cb4626954acd7ba5f397f184a9e174c5303a7bb5602105d68eb9ebb0bf3d1627

See more details on using hashes here.

File details

Details for the file aws_solutions_constructs_aws_lambda_bedrockinferenceprofile-2.85.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_solutions_constructs_aws_lambda_bedrockinferenceprofile-2.85.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8d06e3ca81b4859362f4684a9e613323b8f702459c9e22d9337f1376725882f7
MD5 385525574df3f7326a406b7fb6ecea31
BLAKE2b-256 a448e77eb9af1ced81c24c2ac8ad4aaf46ba361d6f5311ecf33d2e0bbed3d034

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page