|
| 1 | +name: Build Lambda Layers |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - 'layers/**' |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-publish-layers: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + env: |
| 14 | + S3_BUCKET: hackathon-lambda-ap-ai-cyberark |
| 15 | + PYTHON_VERSION: python3.12 |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v5 |
| 23 | + with: |
| 24 | + python-version: '3.12' |
| 25 | + |
| 26 | + - name: Configure AWS credentials |
| 27 | + uses: aws-actions/configure-aws-credentials@v4 |
| 28 | + with: |
| 29 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 30 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 31 | + aws-region: us-east-1 |
| 32 | + |
| 33 | + - name: Build and publish each layer |
| 34 | + run: | |
| 35 | + for dir in layers/*; do |
| 36 | + if [ -f "$dir/requirements.txt" ]; then |
| 37 | + LAYER_NAME=$(basename "$dir") |
| 38 | + echo "Building layer: $LAYER_NAME" |
| 39 | +
|
| 40 | + # Create target directory |
| 41 | + mkdir -p "$dir/python" |
| 42 | +
|
| 43 | + # Install Python packages |
| 44 | + pip install --upgrade --no-cache-dir -r "$dir/requirements.txt" -t "$dir/python" |
| 45 | +
|
| 46 | + # Clean up unnecessary files |
| 47 | + find "$dir/python" -type d -name "__pycache__" -exec rm -rf {} + |
| 48 | + find "$dir/python" -type d -name "tests" -exec rm -rf {} + |
| 49 | + find "$dir/python" -type f -name "*.pyc" -delete |
| 50 | + find "$dir/python" -type d -name "*.dist-info" -exec rm -rf {} + |
| 51 | +
|
| 52 | + # Zip the layer |
| 53 | + cd "$dir" |
| 54 | + zip -r "../$LAYER_NAME.zip" python > /dev/null |
| 55 | + cd ../.. |
| 56 | +
|
| 57 | + # Upload to S3 |
| 58 | + echo "Uploading $LAYER_NAME.zip to S3" |
| 59 | + aws s3 cp "$dir.zip" s3://$S3_BUCKET/layers/$LAYER_NAME.zip |
| 60 | +
|
| 61 | + # Publish to Lambda |
| 62 | + echo "Publishing Lambda layer: $LAYER_NAME" |
| 63 | + LAYER_ARN=$(aws lambda publish-layer-version \ |
| 64 | + --layer-name "$LAYER_NAME" \ |
| 65 | + --description "Layer for $LAYER_NAME" \ |
| 66 | + --content S3Bucket=$S3_BUCKET,S3Key=layers/$LAYER_NAME.zip \ |
| 67 | + --compatible-runtimes $PYTHON_VERSION \ |
| 68 | + --query 'LayerVersionArn' --output text) |
| 69 | +
|
| 70 | + echo "Published $LAYER_NAME: $LAYER_ARN" |
| 71 | + fi |
| 72 | + done |
0 commit comments