Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deploying as Lambda #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ jobs:
tags: |
type=ref,event=branch
type=semver,pattern={{version}}

- name: Extract Docker metadata lambda
id: meta-lambda
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}-lambda
# Build and push Docker image with Buildx (don't push on PR)
# Also make use of Github Actions cache to speed up pipeline runs
# https://github.com/docker/build-push-action
Expand All @@ -94,6 +101,17 @@ jobs:
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push Docker image lambda
id: build-and-push-lambda
uses: docker/build-push-action@1cb9d22b932e4832bb29793b7777ec860fc1cde0
with:
context: awsenergylabeler-lambda
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-lambda.outputs.tags }}
labels: ${{ steps.meta-lambda.outputs.labels }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
Expand All @@ -112,4 +130,9 @@ jobs:
-a "repo=${{ github.repository }}" \
-a "workflow=${{ github.workflow }}" \
-a "ref=${{ github.sha }}" \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} && \
cosign sign --key env://COSIGN_PRIVATE_KEY \
-a "repo=${{ github.repository }}" \
-a "workflow=${{ github.workflow }}" \
-a "ref=${{ github.sha }}" \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta-lambda.outputs.version }}
13 changes: 13 additions & 0 deletions awsenergylabeler-lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install --no-cache-dir -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
34 changes: 34 additions & 0 deletions awsenergylabeler-lambda/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import subprocess
import logging

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger()
logger.setLevel(os.environ["log_level"])

def event_to_args(event):
args = []
for key, value in event.items():
if not isinstance(value, bool) or value:
args.append(f'--{key}')
if not isinstance(value, bool):
args.append(str(value))
return args

def handler(event, context):
# Execute the aws-energy-labeler CLI command
command = ['aws-energy-labeler']
command.extend(event_to_args(event))
env = os.environ.copy()
try:
result = subprocess.run(command, env=env, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logger.debug(f"Command '{' '.join(command)}' executed succesfully {result.stdout.decode('utf-8')}")
return result.stdout.decode('utf-8')
except subprocess.CalledProcessError as e:
logger.error(f"Command '{' '.join(command)}' failed with exit code {e.returncode}: {e.stderr.decode('utf-8')}")
# Raise a RuntimeError to indicate failure to AWS Lambda
raise RuntimeError("Command execution failed with a non-zero exit code")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
# Raise the caught exception to indicate failure to AWS Lambda
raise e
1 change: 1 addition & 0 deletions awsenergylabeler-lambda/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
awsenergylabelercli==3.2.6
Loading