Pierre-AntoineAETP
Pierre-AntoineAETP

Reputation: 21

How to execute a lambda function of a docker image that executes an x64 binary by taking input parameters

I created a docker image that runs at x64 binary with parameters like this : ./my-exec --sourcename "source" --targetname "target"

I then put my image in AWS ECR to use it to create a lambda function from an image. Here's my DockerFile:

RUN apt-get update && \
    apt-get install -y \
    libc6 \
    libstdc++6 \
    libicu-dev \
    unzip \
    awscli \
    curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /tmp

RUN curl -o /tmp/my-exec.zip https://mys3.s3.eu-west-1.amazonaws.com/myExec/myExec.zip 

RUN unzip /tmp/myExec.zip -d /usr/local/bin/

RUN chmod +x /usr/local/bin/myExec

WORKDIR /usr/local/bin

ENTRYPOINT ["./myExec"]

So my question is how to execute my lambda function with parameters so that my executable is launched with the parameters

Upvotes: 0

Views: 36

Answers (1)

divyang4481
divyang4481

Reputation: 1793

you need script e.g.

entrypoint.sh

#!/bin/bash
set -e

# Read Lambda event JSON from standard input
EVENT_DATA=$(cat)

# Extract parameters using jq
SOURCE_NAME=$(echo "$EVENT_DATA" | jq -r '.sourcename // empty')
TARGET_NAME=$(echo "$EVENT_DATA" | jq -r '.targetname // empty')

# Validate input
if [[ -z "$SOURCE_NAME" || -z "$TARGET_NAME" ]]; then
    echo "Error: Missing required parameters 'sourcename' or 'targetname'."
    exit 1
fi

# Execute the binary with parameters
echo "Executing: ./myExec --sourcename '$SOURCE_NAME' --targetname '$TARGET_NAME'"
exec ./myExec --sourcename "$SOURCE_NAME" --targetname "$TARGET_NAME"

then make it executable

chmod +x entrypoint.sh

then take that script in your docker

so your docker can be

FROM amazonlinux:latest

# Install required dependencies
RUN yum install -y \
    libc6 \
    libstdc++6 \
    libicu \
    unzip \
    jq \
    aws-cli \
    curl \
    && yum clean all

WORKDIR /tmp

# Download and unzip binary
RUN curl -o /tmp/myExec.zip https://mys3.s3.eu-west-1.amazonaws.com/myExec/myExec.zip && \
    unzip /tmp/myExec.zip -d /usr/local/bin/ && \
    chmod +x /usr/local/bin/myExec

WORKDIR /usr/local/bin

# Copy the wrapper script into the container
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Set entrypoint to the wrapper script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

so now you this docker build it and push it ECR/dockerhub

then deploy it

aws lambda create-function --function-name MyExecLambda \
    --package-type Image \
    --code ImageUri=<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/my-lambda-image:latest \
    --role arn:aws:iam::<AWS_ACCOUNT_ID>:role/<LAMBDA_ROLE>

Upvotes: -1

Related Questions