Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add API endpoint to invoke AWS Lambda function running docker

Im using Serverless Framework to deploy a Docker image running R to an AWS Lambda.

service: r-lambda

provider:
  name: aws
  region: eu-west-1
  timeout: 60
  environment:
    stage: ${sls:stage}
    R_AWS_REGION: ${aws:region}
  ecr:
    images:
      r-lambda:
        path: ./

functions:
  r-lambda-hello:
    image:
      name: r-lambda
      command:
        - functions.hello

This works fine and I can log into AWS and invoke the lambda function. But I also want to invoke by doing a curl to it, so I added an "events" property to the functions section:

functions:
  r-lambda-hello:
    image:
      name: r-lambda
      command:
        - functions.hello
    events:
      - http: GET r-lambda-hello

However, when I deploy with serverless, it does not output the API endpoint. And when I go to API Gateway in AWS, I dont see any APIs here. What am I doing wrong?

EDIT

As per Rovelcio Junior's answer, I went to AWS CloudFormation > Stacks > r-lambda-dev > Resources. But there is now Api Gateway listed in the resources...

EDIT

Heres my DockerFile:

FROM public.ecr.aws/lambda/provided:al2.2021.09.13.11

ENV R_VERSION=4.0.3

RUN yum -y install wget tar openssl-devel libxml2-devel

RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
    && wget https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm \
    && yum -y install R-${R_VERSION}-1-1.x86_64.rpm \
    && rm R-${R_VERSION}-1-1.x86_64.rpm

ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/"

RUN Rscript -e "install.packages(c('httr', 'jsonlite', 'logger', 'paws.storage', 'paws.database', 'readr', 'BiocManager'), repos = 'https://cloud.r-project.org/')"

COPY runtime.R functions.R ${LAMBDA_TASK_ROOT}/

RUN chmod 755 -R ${LAMBDA_TASK_ROOT}/

RUN printf '#!/bin/sh\ncd $LAMBDA_TASK_ROOT\nRscript runtime.R' > /var/runtime/bootstrap \
    && chmod +x /var/runtime/bootstrap

And the output when I deploy:

Serverless: Packaging service...
#1 [internal] load build definition from Dockerfile
#1 sha256:730ec5a8380df019470bdbb6091e9a29cd62f4ef4443be0c14ec2c4979da26ea
#1 transferring dockerfile: 37B 0.0s done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 sha256:553479c1392984ccf98fd0cf873e2e2da149ff9a1bc98a0abee6b3e558545181
#2 transferring context: 2B done
#2 DONE 0.0s

#3 [internal] load metadata for public.ecr.aws/lambda/provided:al2.2021.09.13.11
#3 sha256:8c254bed2a05020fafbb65f8dbd8b7925d24019ab56ee85272c4559290756324
#3 DONE 4.7s

#4 [ 1/8] FROM public.ecr.aws/lambda/provided:al2.2021.09.13.11@sha256:9628c6a5372a04289000f7cb9cb9aeb273d7381bdbe1283a07fb86981a06ac07
#4 sha256:2082eea955a6ae3398939e60fe10c5c7b34b262c2e5b82421ece4a9127883f58
#4 DONE 0.0s

#10 [internal] load build context
#10 sha256:8b61403d9fd75cf8a55c7294afa45fe717dc75c5783b7b749c304687556372c6
#10 transferring context: 108B done
#10 DONE 0.0s

#6 [ 3/8] RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm     && wget https://cdn.rstudio.com/r/centos-7/pkgs/R-4.0.3-1-1.x86_64.rpm     && yum -y install R-4.0.3-1-1.x86_64.rpm     && rm R-4.0.3-1-1.x86_64.rpm
#6 sha256:22644d17f1156ee8911a76c1f9af4c3894f22f41e347e611f4d382da3bf54356
#6 CACHED

#11 [ 4/8] COPY runtime.R functions.R /var/task/
#11 sha256:163032f10dc70da4ceb3d6a8824b7f81def9dda7d75e745074f7fdd2c639253e
#11 CACHED

#13 [ 5/8] RUN chmod 755 -R /var/task/
#13 sha256:606c9651f2ba1aadde5e6928c1fffa5e6a397762ef1abdf14aeea2940c16cfd8
#13 CACHED

#5 [ 6/8] RUN yum -y install wget tar openssl-devel libxml2-devel
#5 sha256:a5bb99c3107595ebcce135aec74510b7d5438acc6900e4bd5db1bec97f9c61b5
#5 CACHED

#7 [ 7/8] RUN Rscript -e "install.packages(c('httr', 'jsonlite', 'logger', 'paws.storage', 'paws.database', 'readr', 'BiocManager'), repos = 'https://cloud.r-project.org/')"
#7 sha256:465b4b4ff27a57cacb401f8b0c9335fadca31fa68081cd5f56f22c9b14e9c17a
#7 CACHED

#14 [8/8] RUN printf '#!/bin/sh\ncd $LAMBDA_TASK_ROOT\nRscript runtime.R' > /var/runtime/bootstrap     && chmod +x /var/runtime/bootstrap
#14 sha256:74b7d704dc21ccab7da6fd953240a5331d75229af210def5351bd5c5bf943eed
#14 CACHED

#15 exporting to image
#15 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#15 exporting layers done
#15 writing image sha256:9fabde8e59e85c4ffe09ec70550b3baeba6dd422cd54f05e17e5fac6c9c9db32 done
#15 naming to docker.io/library/serverless-r-lambda-dev:r-lambda done
#15 DONE 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Serverless: Login to Docker succeeded!
like image 470
Mark Avatar asked Nov 06 '22 23:11

Mark


1 Answers

The way your events.http is configured looks wrong. Try replacing it with:

- http:
      path: r-lambda-hello
      method: get     

This might be helpful as well: https://github.com/serverless/examples
I also found this blog useful: Build a serverless API with Amazon Lambda and API Gateway

like image 125
urirot Avatar answered Nov 11 '22 02:11

urirot