Skip to content

biagolini/PythonAwsBedrockActionGroupDemo

Repository files navigation

Holiday Checker Lambda Functions

This project is designed for educational purposes to demonstrate AWS Lambda integration with the holidays Python library. It includes two Lambda functions created for a course where lambda_function_regular.py shows a simple initial version demonstrating the holidays library usage, and lambda_function_bedrock.py is designed for AWS Bedrock Agent integration.

Project Structure

repository/
├── test/                         # Test events for AWS Lambda console
│   ├── lambda_function_bedrock/  # Test events for Bedrock function
│   └── lambda_function_regular/  # Test events for regular function
├── lambda_function_bedrock.py    # Lambda for AWS Bedrock Agent integration
├── lambda_function_regular.py    # Standalone Lambda for testing/demo
├── requirements.txt              # Python dependencies
└── Dockerfile                    # Docker configuration for layer creation

Lambda Functions

Both functions check if a specific date is a holiday in a given country, but serve different educational purposes:

  • lambda_function_regular.py: Simple version demonstrating basic holidays library usage
  • lambda_function_bedrock.py: Advanced version for AWS Bedrock Agent integration

Prerequisites

  • Python 3.9+
  • holidays library

Setup Instructions

1. Create Lambda Layer

Option 1: Using Virtual Environment

Create Virtual Environment

virtualenv venv

# Or specify Python version:
virtualenv -p python3.13 venv

Activate Virtual Environment

  • macOS/Linux:
    source venv/bin/activate
  • Windows:
    venv\Scripts\activate
# Install dependencies to layer directory
pip install -r requirements.txt -t python/

# Create layer ZIP file
zip -r aws-lambda-layer.zip python/

Option 2: Using Docker

  1. Prepare the Layer Package:
    • If you prefer to skip this step, use the pre-built python_dependencies.zip file provided in the tutorial directory. This file contains all required dependencies and can be uploaded directly as a layer.
    • If you want to build your own layer:
      • Detailed instructions are available in the Managing Dependencies in AWS Lambda with Docker-Generated Layers tutorial.
      • Ensure you have the requirements.txt and Dockerfile files provided in the tutorial directory.
      • Execute the following commands on your local machine to build a .zip file for your Lambda Layer:
        • Build the Docker Image:
          docker build -t lambda_layer .
        • Run a Container:
          docker run --name my_lambda_layer_container lambda_layer
        • Export the Layer:
          docker cp my_lambda_layer_container:/home/python_dependencies.zip .
        • Cleanup (Optional):
          docker stop my_lambda_layer_container
          docker rm my_lambda_layer_container
          docker rmi lambda_layer

Upload Layer to AWS

# Upload layer via AWS CLI
aws lambda publish-layer-version \
    --layer-name holiday-checker-layer \
    --zip-file fileb://aws-lambda-layer.zip \
    --compatible-runtimes python3.9 python3.10 python3.11 python3.12

2. Configure Lambda Function

  • Runtime: Python 3.9+
  • Handler: lambda_function.lambda_handler
  • Layer: Add the layer created above

3. Configure Lambda Permissions for Bedrock

Note: AWS Bedrock requires Lambda resource-based policies to invoke the function.

  • Navigate to the Configuration tab.
  • In the left-hand menu, select Permissions.
  • Under the Resource-based policy statements section, click Add permissions.
  • In the dialog, go to AWS account tab and configure the following:
    • Statement ID: AllowBedrockInvoke
    • Principal: bedrock.amazonaws.com
    • Action: lambda:InvokeFunction
  • Click Save to apply the policy.
  • The new policy should appear in the Resource-based policy statements section. Ensure it matches the following structure:
    {
        "Version": "2012-10-17",
        "Id": "default",
        "Statement": [
           {
              "Sid": "AllowBedrockInvoke",
              "Effect": "Allow",
              "Principal": {
              "Service": "bedrock.amazonaws.com"
              },
              "Action": "lambda:InvokeFunction",
              "Resource": "arn:aws:lambda:<region>:<account-id>:function:<function-name>"
           }
        ]
     }

Testing

JSON test files for AWS Lambda console testing are available in the test/ directory. These files contain various test scenarios that can be used to validate the Lambda functions.

References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published