-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f832ab5
commit 14dc255
Showing
27 changed files
with
935 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Lambda functions for managing sessions.""" | ||
import json | ||
import logging | ||
import os | ||
import time | ||
from decimal import Decimal | ||
from typing import Any, Dict | ||
|
||
import boto3 | ||
import create_env_variables # noqa: F401 | ||
from botocore.exceptions import ClientError | ||
from utilities.common_functions import api_wrapper, retry_config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
dynamodb = boto3.resource("dynamodb", region_name=os.environ["AWS_REGION"], config=retry_config) | ||
table = dynamodb.Table(os.environ["CONFIG_TABLE_NAME"]) | ||
|
||
|
||
@api_wrapper | ||
def get_configuration(event: dict, context: dict) -> Dict[str, Any]: | ||
"""List configuration entries by configScope from DynamoDB.""" | ||
config_scope = event["queryStringParameters"]["configScope"] | ||
|
||
response = {} | ||
try: | ||
response = table.query( | ||
KeyConditionExpression="#s = :configScope", | ||
ExpressionAttributeNames={"#s": "configScope"}, | ||
ExpressionAttributeValues={":configScope": config_scope}, | ||
ScanIndexForward=False, | ||
) | ||
except ClientError as error: | ||
if error.response["Error"]["Code"] == "ResourceNotFoundException": | ||
logger.warning(f"No record found with session id: {config_scope}") | ||
else: | ||
logger.exception("Error fetching session") | ||
return response.get("Items", {}) # type: ignore [no-any-return] | ||
|
||
|
||
@api_wrapper | ||
def update_configuration(event: dict, context: dict) -> None: | ||
"""Update configuration in DynamoDB.""" | ||
# from https://stackoverflow.com/a/71446846 | ||
body = json.loads(event["body"], parse_float=Decimal) | ||
body["created_at"] = str(Decimal(time.time())) | ||
|
||
try: | ||
table.put_item(Item=body) | ||
except ClientError: | ||
logger.exception("Error updating session in DynamoDB") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/** | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { IAuthorizer, RestApi } from 'aws-cdk-lib/aws-apigateway'; | ||
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'; | ||
import { ISecurityGroup } from 'aws-cdk-lib/aws-ec2'; | ||
import { Role } from 'aws-cdk-lib/aws-iam'; | ||
import { LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda'; | ||
import { StringParameter } from 'aws-cdk-lib/aws-ssm'; | ||
import { Construct } from 'constructs'; | ||
|
||
import { PythonLambdaFunction, registerAPIEndpoint } from '../../api-base/utils'; | ||
import { BaseProps } from '../../schema'; | ||
import { createLambdaRole } from '../../core/utils'; | ||
import { Vpc } from '../../networking/vpc'; | ||
import { AwsCustomResource, PhysicalResourceId } from 'aws-cdk-lib/custom-resources'; | ||
|
||
/** | ||
* Properties for ConfigurationApi Construct. | ||
* | ||
* @property {IVpc} vpc - Stack VPC | ||
* @property {Layer} commonLayer - Lambda layer for all Lambdas. | ||
* @property {IRestApi} restAPI - REST APIGW for UI and Lambdas | ||
* @property {IRole} lambdaExecutionRole - Execution role for lambdas | ||
* @property {IAuthorizer} authorizer - APIGW authorizer | ||
* @property {ISecurityGroup[]} securityGroups - Security groups for Lambdas | ||
* @property {Map<number, ISubnet> }importedSubnets for application. | ||
*/ | ||
type ConfigurationApiProps = { | ||
authorizer: IAuthorizer; | ||
restApiId: string; | ||
rootResourceId: string; | ||
securityGroups?: ISecurityGroup[]; | ||
vpc?: Vpc; | ||
} & BaseProps; | ||
|
||
/** | ||
* API which Maintains config state in DynamoDB | ||
*/ | ||
export class ConfigurationApi extends Construct { | ||
constructor (scope: Construct, id: string, props: ConfigurationApiProps) { | ||
super(scope, id); | ||
|
||
const { authorizer, config, restApiId, rootResourceId, securityGroups, vpc } = props; | ||
|
||
// Get common layer based on arn from SSM due to issues with cross stack references | ||
const commonLambdaLayer = LayerVersion.fromLayerVersionArn( | ||
this, | ||
'configuration-common-lambda-layer', | ||
StringParameter.valueForStringParameter(this, `${config.deploymentPrefix}/layerVersion/common`), | ||
); | ||
|
||
// Create DynamoDB table to handle config data | ||
const configTable = new dynamodb.Table(this, 'ConfigurationTable', { | ||
partitionKey: { | ||
name: 'configScope', | ||
type: dynamodb.AttributeType.STRING, | ||
}, | ||
sortKey: { | ||
name: 'versionId', | ||
type: dynamodb.AttributeType.NUMBER, | ||
}, | ||
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, | ||
encryption: dynamodb.TableEncryption.AWS_MANAGED, | ||
removalPolicy: config.removalPolicy, | ||
}); | ||
|
||
const lambdaRole: Role = createLambdaRole(this, config.deploymentName, 'ConfigurationApi', configTable.tableArn); | ||
|
||
// Populate the App Config table with default config | ||
const date = new Date(); | ||
new AwsCustomResource(this, 'lisa-init-ddb-config', { | ||
onCreate: { | ||
service: 'DynamoDB', | ||
action: 'putItem', | ||
physicalResourceId: PhysicalResourceId.of('initConfigData'), | ||
parameters: { | ||
TableName: configTable.tableName, | ||
Item: { | ||
'versionId': {'N': '0'}, | ||
'changedBy': {'S': 'System'}, | ||
'configScope': {'S': 'global'}, | ||
'changeReason': {'S': 'Initial deployment default config'}, | ||
'createdAt': {'S': Math.round(date.getTime() / 1000).toString()}, | ||
'configuration': {'M': { | ||
'enabledComponents': {'M': { | ||
'deleteSessionHistory': {'BOOL': 'True'}, | ||
'viewMetaData': {'BOOL': 'True'}, | ||
'editKwargs': {'BOOL': 'True'}, | ||
'editPromptTemplate': {'BOOL': 'True'}, | ||
'editChatHistoryBuffer': {'BOOL': 'True'}, | ||
'editNumOfRagDocument': {'BOOL': 'True'}, | ||
'uploadRagDocs': {'BOOL': 'True'}, | ||
'uploadContextDocs': {'BOOL': 'True'} | ||
}}, | ||
'systemBanner': {'M': { | ||
'isEnabled': {'BOOL': 'False'}, | ||
'text': {'S': ''}, | ||
'textColor': {'S': ''}, | ||
'backgroundColor': {'S': ''} | ||
}} | ||
}} | ||
}, | ||
}, | ||
}, | ||
role: lambdaRole | ||
}); | ||
|
||
const restApi = RestApi.fromRestApiAttributes(this, 'RestApi', { | ||
restApiId: restApiId, | ||
rootResourceId: rootResourceId, | ||
}); | ||
|
||
// Create API Lambda functions | ||
const apis: PythonLambdaFunction[] = [ | ||
{ | ||
name: 'get_configuration', | ||
resource: 'configuration', | ||
description: 'Get configuration', | ||
path: 'configuration', | ||
method: 'GET', | ||
environment: { | ||
CONFIG_TABLE_NAME: configTable.tableName | ||
}, | ||
}, | ||
{ | ||
name: 'update_configuration', | ||
resource: 'configuration', | ||
description: 'Updates config data', | ||
path: 'configuration/{configScope}', | ||
method: 'PUT', | ||
environment: { | ||
CONFIG_TABLE_NAME: configTable.tableName, | ||
}, | ||
}, | ||
]; | ||
|
||
apis.forEach((f) => { | ||
const lambdaFunction = registerAPIEndpoint( | ||
this, | ||
restApi, | ||
authorizer, | ||
'./lambda', | ||
[commonLambdaLayer], | ||
f, | ||
Runtime.PYTHON_3_10, | ||
lambdaRole, | ||
vpc, | ||
securityGroups, | ||
); | ||
if (f.method === 'POST' || f.method === 'PUT') { | ||
configTable.grantWriteData(lambdaFunction); | ||
} else if (f.method === 'GET') { | ||
configTable.grantReadData(lambdaFunction); | ||
} else if (f.method === 'DELETE') { | ||
configTable.grantReadWriteData(lambdaFunction); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.