I want to deploy a lambda function that call OpenAI, do some processing for text and return result.
def openaipreprocessing(text):
# call OpenAI api
return vector
def save_result_to_s3(text, vector):
s3 = boto3.client("s3")
file_key = f"results/{datetime.now().isoformat()}.json"
s3.put_object(
Bucket=S3_BUCKET_NAME,
Key=file_key,
Body=json.dumps({"text": text, "vector": vector})
)
return file_key
def lambda_handler(event, context):
try:
body = event.get("body", {})
text = body.get("text")
if not text:
return {"statusCode": 400, "body": json.dumps({"error": "Text is required"})}
vector= openaipreprocessing(text=text)
file_key = save_result_to_s3(text=text, vector=vector)
return {
"statusCode": 200,
"body": json.dumps({"message": "Vector stored", "file_key": file_key})
}
except Exception as e:
return {"statusCode": 500, "body": json.dumps({"error": str(e)})}
#Create S3 bucket
aws s3api create-bucket --bucket $S3_BUCKET_NAME --region $AWS_REGION --profile $AWS_PROFILE
aws s3api put-bucket-versioning --bucket $S3_BUCKET_NAME --versioning-configuration Status=Enabled --profile $AWS_PROFILE
#docker file:
FROM public.ecr.aws/lambda/python:3.9
#Install dependencies
COPY requirements.txt /var/task/src/
RUN pip install -r /var/task/src/requirements.txt
# Copy the src directory into the Lambda container
COPY . /var/task/src/
CMD ["src.lambda_function.lambda_handler"]
#I build locally the docker image and run :
docker run --rm -p 9000:8080 openai-service-api
#Testing the lambda function locally fails in:
s3=boto3.client("s3") with the error: The config profile (my-account) could not be found
#I fix this by mounting the local ~/.aws when I run the docker:
docker run --rm -p 9000:8080 ~/.aws:/root/.aws openai-service-api-v
I call the lambda function as following and it is working well:
curl -X POST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body": "{\"text\": \"Hello world\"}"}'
#I create the ECR repo and the lambda function and I push lambda using this script:
#Load environment variables
source ./src/.env
#Login to AWS ECR
aws ecr create-repository --repository-name $ECR_REPO_NAME --region $AWS_REGION --profile $AWS_PROFILE
aws ecr get-login-password --region $AWS_REGION --profile $AWS_PROFILE| docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws
#Build and push Docker image
docker build -t $ECR_REPO_NAME ./src
docker tag $ECR_REPO_NAME:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws/$ECR_REPO_NAME:latest
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws/$ECR_REPO_NAME:latest
#Create a Lambda function in AWS
aws lambda create-function \
--function-name TextEmbeddingLambda \
--package-type Image \
--code ImageUri=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws/$ECR_REPO_NAME:latest \
--role arn:aws:iam::$AWS_ACCOUNT_ID:role/AWSLambdaBasicExecutionRole \
--region $AWS_REGION --profile $AWS_PROFILE
#Deploy to AWS Lambda
aws lambda update-function-code --function-name TextEmbeddingLambda \
--image-uri $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws/$ECR_REPO_NAME:latest --region $AWS_REGION --profile $AWS_PROFILE
#Testing the lambda on the console fails in the same line:
s3 = boto3.client("s3") for the same error: [ERROR] ProfileNotFound: The config profile (my-account) could not be found Traceback (most recent call last)
Removing the saving to S3. Lambda is working fine.
What is the error and why docker does not see the IAM role and still rely on my aws profile on CLI.