Skip to content

Commit aaf9119

Browse files
committed
Dockerised deployment of Pattern 2
with codebuild # Conflicts: # Dockerfile.optimized
1 parent 6a692c3 commit aaf9119

File tree

15 files changed

+7847
-5021
lines changed

15 files changed

+7847
-5021
lines changed

.dockerignore

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Git
2+
.git
3+
.gitignore
4+
.gitlab-ci.yml
5+
6+
# AWS SAM build artifacts
7+
.aws-sam/
8+
**/.aws-sam/
9+
10+
# Python
11+
__pycache__/
12+
*.py[cod]
13+
*$py.class
14+
*.so
15+
.Python
16+
env/
17+
venv/
18+
.venv/
19+
*.egg-info/
20+
dist/
21+
build/
22+
*.egg
23+
24+
# IDE
25+
.vscode/
26+
.idea/
27+
*.swp
28+
*.swo
29+
*~
30+
.DS_Store
31+
32+
# Documentation
33+
docs/
34+
*.md
35+
LICENSE
36+
NOTICE
37+
CONTRIBUTING.md
38+
CHANGELOG.md
39+
40+
# Test files
41+
tests/
42+
**/tests/
43+
**/test_*.py
44+
**/*_test.py
45+
pytest.ini
46+
.coverage
47+
htmlcov/
48+
49+
# Node (for UI components)
50+
node_modules/
51+
npm-debug.log
52+
yarn-error.log
53+
54+
# Temporary files
55+
*.log
56+
*.tmp
57+
*.temp
58+
.cache/
59+
tmp/
60+
temp/
61+
62+
# Build scripts and configs
63+
samconfig*.toml
64+
template.yaml
65+
template-*.yaml
66+
Makefile
67+
publish.py
68+
publish_container*.py
69+
deploy*.py
70+
update_templates.py
71+
72+
# Notebooks and samples
73+
notebooks/
74+
samples/
75+
76+
# CloudFormation packaged templates
77+
**/*.packaged.yaml
78+
**/packaged.yaml
79+
80+
# Docker files we don't need in context
81+
Dockerfile*
82+
docker-compose*.yml
83+
84+
# Other unnecessary files for Lambda
85+
images/
86+
scripts/
87+
memory-bank/
88+
*.xlsx
89+
*.pdf
90+
*.png
91+
*.jpg
92+
*.jpeg
93+
*.gif
94+
*.ico
95+
96+
# Keep only source code and requirements
97+
!patterns/*/src/**
98+
!lib/idp_common_pkg/**
99+
!src/lambda/**

Dockerfile.optimized

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Optimized Dockerfile for Lambda functions with minimal dependencies
2+
# This builds each function with ONLY the dependencies it needs
3+
4+
FROM public.ecr.aws/lambda/python:3.12-arm64 AS builder
5+
6+
# Copy uv from official distroless image
7+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
8+
9+
# Build argument for function path
10+
ARG FUNCTION_PATH
11+
ARG INSTALL_IDP_COMMON=true
12+
13+
# Create working directory
14+
WORKDIR /build
15+
16+
# Copy idp_common_pkg and requirements for installation
17+
COPY lib/idp_common_pkg /tmp/idp_common_pkg
18+
COPY ${FUNCTION_PATH}/requirements.txt* /build/
19+
20+
# Install all dependencies including idp_common_pkg in one step
21+
RUN --mount=type=cache,target=/root/.cache/uv \
22+
if [ -f /build/requirements.txt ]; then \
23+
sed 's|^\.\./\.\.\(/\.\.\)\?/lib/idp_common_pkg|/tmp/idp_common_pkg|' /build/requirements.txt > /tmp/requirements.txt && \
24+
uv pip install --python python3.12 --target /opt/python -r /tmp/requirements.txt && \
25+
rm /tmp/requirements.txt; \
26+
fi && \
27+
rm -rf /tmp/idp_common_pkg
28+
29+
# Final stage - minimal runtime
30+
FROM public.ecr.aws/lambda/python:3.12-arm64
31+
32+
# Copy only the installed packages
33+
COPY --from=builder /opt/python /opt/python
34+
35+
# Copy function code
36+
ARG FUNCTION_PATH
37+
COPY ${FUNCTION_PATH}/*.py ${LAMBDA_TASK_ROOT}/
38+
39+
# Set Python path
40+
ENV PYTHONPATH=/opt/python:${LAMBDA_TASK_ROOT}
41+
42+
# Set handler
43+
CMD ["index.handler"]

docs/deployment.md

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This guide covers how to deploy, build, publish, and test the GenAI Intelligent
1616

1717
### One-Click Deployment
1818

19-
| US East (N.Virginia) | us-east-1 | [![Launch Stack](https://cdn.rawgit.com/buildkite/cloudformation-launch-stack-button-svg/master/launch-stack.svg)](https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/create/review?templateURL=https://s3.us-east-1.amazonaws.com/aws-ml-blog-us-east-1/artifacts/genai-idp/idp-main.yaml&stackName=IDP) |
19+
| US East (N.Virginia) | us-east-1 | [![Launch Stack](https://cdn.rawgit.com/buildkite/cloudformation-launch-stack-button-svg/master/launch-stack.svg)](https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/create/review?templateURL=https://s3.us-east-1.amazonaws.com/aws-ml-blog-us-east-1/artifacts/genai-idp/idp-main.yaml&stackName=IDP) |
2020

2121
3. Review the template parameters and provide values as needed
2222
4. Check the acknowledgment box and click **Create stack**
@@ -39,47 +39,58 @@ You need to have the following packages installed on your computer:
3939
7. **Node.js 18+** and **npm** (required for UI validation in publish script)
4040

4141
For guidance on setting up a development environment, see:
42-
- [Development Environment Setup Guide on Linux](./setup-development-env-linux.md)
42+
43+
- [Development Environment Setup Guide on Linux](./setup-development-env-linux.md)
4344
- [Development Environment Setup Guide on macOS](./setup-development-env-macos.md)
4445
- [Development Environment Setup Guide on Windows (WSL)](./setup-development-env-WSL.md)
4546

4647
Copy the repo to your computer. Either:
48+
4749
- Use the git command to clone the repo, if you have access
4850
- OR, download and expand the ZIP file for the repo, or use the ZIP file that has been shared with you
4951

5052
### Build and Publish the Solution
5153

5254
To build and publish your own template to your own S3 bucket:
5355

54-
* `cfn_bucket_basename`: A prefix added to the beginning of the bucket name (e.g. `idp-1234567890` to ensure global uniqueness)
55-
* `cfn_prefix`: A prefix added to CloudFormation resources (e.g. `idp` or `idp-dev`)
56+
- `cfn_bucket_basename`: A prefix added to the beginning of the bucket name (e.g. `idp-1234567890` to ensure global uniqueness)
57+
- `cfn_prefix`: A prefix added to CloudFormation resources (e.g. `idp` or `idp-dev`)
5658

5759
Navigate into the project root directory and run:
5860

5961
#### Using publish.py (Recommended)
6062

6163
```bash
62-
python3 publish.py <cfn_bucket_basename> <cfn_prefix> <region> [--verbose]
64+
python3 publish.py <cfn_bucket_basename> <cfn_prefix> <region> [--verbose] [--no-validate] [--clean-build] [--max-workers N]
6365
```
6466

6567
**Parameters:**
68+
6669
- `cfn_bucket_basename`: A prefix for the S3 bucket name (e.g., `idp-1234567890`)
6770
- `cfn_prefix`: S3 prefix for artifacts (e.g., `idp`)
6871
- `region`: AWS region for deployment (e.g., `us-east-1`)
6972
- `--verbose` or `-v`: (Optional) Enable detailed error output for debugging build failures
73+
- Pattern-2 functions are built and deployed as container images automatically. Pattern-1 and Pattern-3 use ZIP-based Lambdas.
74+
75+
**Standard ZIP Deployment:**
7076

71-
Example:
7277
```bash
7378
python3 publish.py idp-1234567890 idp us-east-1
7479
```
7580

81+
Note: Pattern-2 container images are built and pushed automatically when Pattern-2 changes are detected. Ensure Docker is running and you have ECR permissions.
82+
83+
> **Note**: Container-based deployment is recommended when Lambda functions exceed the 250MB unzipped size limit. This allows deployment packages up to 10GB.
84+
7685
**Troubleshooting Build Issues:**
7786
If the build fails, use the `--verbose` flag to see detailed error messages:
87+
7888
```bash
7989
python3 publish.py idp-1234567890 idp us-east-1 --verbose
8090
```
8191

8292
This will show:
93+
8394
- Exact SAM build commands being executed
8495
- Complete error output from failed builds
8596
- Python version compatibility issues
@@ -92,26 +103,31 @@ This will show:
92103
```
93104

94105
Example:
106+
95107
```bash
96108
./publish.sh idp-1234567890 idp us-east-1
97109
```
98110

99111
Both scripts:
112+
100113
- Check your system dependencies for required packages
101114
- Create CloudFormation templates and asset zip files
102115
- Publish the templates and required assets to an S3 bucket in your account
103116
- The bucket will be named `<cfn_bucket_basename>-<region>` (created if it doesn't exist)
104117

105118
When completed, the script displays:
119+
106120
- The CloudFormation template's S3 URL
107121
- A 1-click URL for launching the stack creation in the CloudFormation console
108122

109123
### Deployment Options
110124

111125
#### Recommended: Deploy using AWS CloudFormation console
126+
112127
For your first deployment, use the `1-Click Launch URL` provided by the publish script. This lets you inspect the available parameter options in the console.
113128

114129
#### CLI Deployment
130+
115131
For scripted/automated deployments, use the AWS CLI:
116132

117133
```bash
@@ -126,27 +142,84 @@ aws cloudformation deploy \
126142
```
127143

128144
Or to update an already-deployed stack:
145+
129146
```bash
130147
aws cloudformation update-stack \
131148
--stack-name <your-stack-name> \
132149
--template-url <template URL output by publish script, e.g. https://s3.us-east-1.amazonaws.com/blahblah.yaml> \
133150
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
134151
--region <region> \
135-
--parameters ParameterKey=AdminEmail,ParameterValue="<your-email>" ParameterKey=IDPPattern,ParameterValue="<pattern-name>"
152+
--parameters ParameterKey=AdminEmail,ParameterValue="<your-email>" ParameterKey=IDPPattern,ParameterValue="<pattern-name>"
136153
```
137154

138-
139155
**Pattern Parameter Options:**
140-
* `Pattern1 - Packet or Media processing with Bedrock Data Automation (BDA)`
141-
* Can use an existing BDA project or create a new demo project
142-
* `Pattern2 - Packet processing with Textract and Bedrock`
143-
* Supports both page-level and holistic classification
144-
* Recommended for first-time users
145-
* `Pattern3 - Packet processing with Textract, SageMaker(UDOP), and Bedrock`
146-
* Requires a UDOP model in S3 that will be deployed on SageMaker
156+
157+
- `Pattern1 - Packet or Media processing with Bedrock Data Automation (BDA)`
158+
- Can use an existing BDA project or create a new demo project
159+
- `Pattern2 - Packet processing with Textract and Bedrock`
160+
- Supports both page-level and holistic classification
161+
- Recommended for first-time users
162+
- `Pattern3 - Packet processing with Textract, SageMaker(UDOP), and Bedrock`
163+
- Requires a UDOP model in S3 that will be deployed on SageMaker
147164

148165
After deployment, check the Outputs tab in the CloudFormation console to find links to dashboards, buckets, workflows, and other solution resources.
149166

167+
## Container-Based Lambda Deployment
168+
169+
When Lambda functions exceed the 250MB unzipped size limit, use the container-based deployment option. This allows deployment packages up to 10GB.
170+
171+
### When to Use Container Deployment
172+
173+
Use container-based deployment when:
174+
175+
- Lambda package size exceeds 250MB unzipped
176+
- You need additional system dependencies not available in the Lambda runtime
177+
- You want to use custom runtime environments
178+
- Your deployment includes large ML models or data files
179+
180+
### Container Deployment Process
181+
182+
1. **Pattern-2 Uses Containers Automatically:**
183+
- When Pattern-2 changes are detected, the script builds Docker images for Pattern-2 functions and pushes them to ECR.
184+
- Ensure Docker is running and your AWS credentials have ECR permissions.
185+
186+
2. **What Happens Behind the Scenes:**
187+
- Creates/verifies ECR repository for Lambda images
188+
- Builds Docker images for each Lambda function
189+
- Pushes images to ECR with appropriate tags
190+
- Updates CloudFormation templates to use container images
191+
- Uploads templates to S3 for deployment
192+
193+
3. **Architecture Support:**
194+
195+
- Default: ARM64 (Graviton2) for better price/performance
196+
- Optional: x86_64 for broader compatibility (adjust Docker build if needed)
197+
198+
### Container Image Structure
199+
200+
The solution uses optimized multi-stage Docker builds:
201+
202+
- **Base stage**: Python runtime and system dependencies
203+
- **Dependencies stage**: Python packages from requirements.txt
204+
- **Function stage**: Lambda function code and handler
205+
206+
### Monitoring Container Deployments
207+
208+
Check deployment status:
209+
210+
```bash
211+
# View ECR images
212+
aws ecr list-images --repository-name idp-<stack-name>-lambda
213+
214+
# Check Lambda function configuration
215+
aws lambda get-function --function-name <function-name>
216+
217+
# View container logs
218+
aws logs tail /aws/lambda/<function-name> --follow
219+
```
220+
221+
For detailed container deployment documentation, see [Container Lambda Deployment Guide](./container-lambda-deployment.md).
222+
150223
## Updating an Existing Stack
151224

152225
To update an existing GenAIIDP deployment to a new version:
@@ -156,7 +229,7 @@ To update an existing GenAIIDP deployment to a new version:
156229
3. Select your existing GenAIIDP stack
157230
4. Click on the "Update" button
158231
5. Select "Replace current template"
159-
6. Provide the new template URL:
232+
6. Provide the new template URL:
160233
- us-west-2: `https://s3.us-west-2.amazonaws.com/aws-ml-blog-us-west-2/artifacts/genai-idp/idp-main.yaml`
161234
- us-east-1: `https://s3.us-east-1.amazonaws.com/aws-ml-blog-us-east-1/artifacts/genai-idp/idp-main.yaml`
162235
7. Click "Next"
@@ -196,11 +269,14 @@ To update an existing GenAIIDP deployment to a new version:
196269
### Testing without the UI
197270

198271
You can test the solution without using the UI through the following methods:
272+
199273
1. Direct S3 uploads as described in the Basic Test section
200274
2. Using the AWS CLI to upload documents to the input bucket:
275+
201276
```bash
202277
aws s3 cp ./samples/lending_package.pdf s3://idp-inputbucket-kmsxxxxxxxxx/
203278
```
279+
204280
3. Using the AWS SDK in your application code to programmatically send documents for processing
205281

206282
### Upload Multiple Sample Files
@@ -221,6 +297,7 @@ To test any lambda function locally:
221297
3. Verify `./testing/env.json` and change the region if necessary
222298
4. Run `sam build` to package the function(s)
223299
5. Use `sam local` to run the function:
300+
224301
```bash
225302
sam local invoke OCRFunction -e testing/OCRFunction-event.json --env-vars testing/env.json
226303
```

docs/govcloud-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ You need to have the following packages installed on your computer:
4444
3. [sam (AWS SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
4545
4. python 3.11 or later
4646
5. A local Docker daemon
47-
6. Python packages for publish.py: `pip install boto3 rich PyYAML botocore setuptools`
47+
6. Python packages for publish.py: `pip install boto3 rich PyYAML botocore setuptools docker`
4848

4949
### Step 1: Generate GovCloud Template
5050

0 commit comments

Comments
 (0)