添加 API 端点以调用 AWS Lambda 函数 运行 docker
Add API endpoint to invoke AWS Lambda function running docker
我正在使用无服务器框架将 Docker 映像 运行 R 部署到 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
这很好用,我可以登录 AWS 并调用 lambda 函数。但我也想通过对其进行卷曲来调用,所以我在函数部分添加了一个“事件”属性:
functions:
r-lambda-hello:
image:
name: r-lambda
command:
- functions.hello
events:
- http: GET r-lambda-hello
但是,当我使用无服务器部署时,它不会输出 API 端点。当我转到 AWS 中的 API 网关时,我在这里看不到任何 API。我做错了什么?
编辑
根据 Rovelcio Junior 的回答,我转到了 AWS CloudFormation > Stacks > r-lambda-dev > Resources。但是现在资源中列出了 Api 网关...
编辑
这是我的Docker文件:
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
以及我部署时的输出:
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!
您的 events.http 配置方式看起来不对。
尝试将其替换为:
- http:
path: r-lambda-hello
method: get
这也可能有帮助:https://github.com/serverless/examples
我还发现这个博客很有用:Build a serverless API with Amazon Lambda and API Gateway
您的活动配置方式 looks right。只需向端点添加正斜杠:GET /r-lambda-hello
(以抑制警告,与问题完全无关)。
我用 hello world
模板(没有 Docker 图像)测试了你的 serverless.yml
,它在第一个 运行 上相应地生成了 APIs ,但没有在后续构建中输出 API 个端点。
请确保您位于正确的 AWS 区域 eu-west-1
。
此外,您可以访问 AWS CloudFormation > Stacks > r-lambda-dev
> Resources 以找到 AWS::ApiGateway::RestApi
、AWS::ApiGateway::Resource
、AWS::ApiGateway::Method
和 AWS::ApiGateway::Deployment
.
(想 post 这作为评论,但评论不允许代码块)
您的代码看起来不错。
我试过类似的设置:
functions:
test-r2:
image:
name: r_lambda
command:
- functions.hello
events:
- http: GET /test-r
输出:
Service Information
service: r-test
stage: dev
region: eu-west-1
stack: r-test-dev
resources: 11
api keys:
None
endpoints:
GET - https://8...2.execute-api.eu-west-1.amazonaws.com/dev/test-r
functions:
test-r2: r-test-dev-test-r2
您使用的是哪个版本的无服务器?您是否尝试过使用最新版本?
我正在使用无服务器框架将 Docker 映像 运行 R 部署到 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
这很好用,我可以登录 AWS 并调用 lambda 函数。但我也想通过对其进行卷曲来调用,所以我在函数部分添加了一个“事件”属性:
functions:
r-lambda-hello:
image:
name: r-lambda
command:
- functions.hello
events:
- http: GET r-lambda-hello
但是,当我使用无服务器部署时,它不会输出 API 端点。当我转到 AWS 中的 API 网关时,我在这里看不到任何 API。我做错了什么?
编辑
根据 Rovelcio Junior 的回答,我转到了 AWS CloudFormation > Stacks > r-lambda-dev > Resources。但是现在资源中列出了 Api 网关...
编辑
这是我的Docker文件:
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
以及我部署时的输出:
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!
您的 events.http 配置方式看起来不对。 尝试将其替换为:
- http:
path: r-lambda-hello
method: get
这也可能有帮助:https://github.com/serverless/examples
我还发现这个博客很有用:Build a serverless API with Amazon Lambda and API Gateway
您的活动配置方式 looks right。只需向端点添加正斜杠:GET /r-lambda-hello
(以抑制警告,与问题完全无关)。
我用 hello world
模板(没有 Docker 图像)测试了你的 serverless.yml
,它在第一个 运行 上相应地生成了 APIs ,但没有在后续构建中输出 API 个端点。
请确保您位于正确的 AWS 区域 eu-west-1
。
此外,您可以访问 AWS CloudFormation > Stacks > r-lambda-dev
> Resources 以找到 AWS::ApiGateway::RestApi
、AWS::ApiGateway::Resource
、AWS::ApiGateway::Method
和 AWS::ApiGateway::Deployment
.
(想 post 这作为评论,但评论不允许代码块)
您的代码看起来不错。
我试过类似的设置:
functions:
test-r2:
image:
name: r_lambda
command:
- functions.hello
events:
- http: GET /test-r
输出:
Service Information
service: r-test
stage: dev
region: eu-west-1
stack: r-test-dev
resources: 11
api keys:
None
endpoints:
GET - https://8...2.execute-api.eu-west-1.amazonaws.com/dev/test-r
functions:
test-r2: r-test-dev-test-r2
您使用的是哪个版本的无服务器?您是否尝试过使用最新版本?