Skip to content

Commit c595b98

Browse files
authored
Merge pull request #21 from aws-samples/dev
upgrade to lambda_http v0.5.1
2 parents a88510c + 19750c2 commit c595b98

File tree

10 files changed

+462
-354
lines changed

10 files changed

+462
-354
lines changed

Cargo.lock

Lines changed: 426 additions & 286 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
[package]
22
name = "aws-lambda-adapter"
3-
version = "0.1.2"
3+
version = "0.3.0"
44
authors = ["Harold Sun <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
log = "0.4.14"
98
env_logger = "0.8.3"
10-
tokio = { version = "1.4", features = ["macros", "io-util", "sync", "rt-multi-thread", "process"] }
11-
tokio-retry = "0.3"
12-
lambda_http = {git = "https://github.com/bnusunny/aws-lambda-rust-runtime", branch = "extensions"}
13-
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json"] }
149
http = "0.2.4"
10+
lambda-extension = "0.5.0"
11+
lambda_http = "0.5.1"
12+
log = "0.4.14"
1513
once_cell = "1.7.2"
16-
futures = "0.3.16"
17-
url = "2.1.1"
18-
serde_json = "1.0"
14+
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json"] }
15+
tokio = { version = "1.0", features = ["macros", "io-util", "sync", "rt-multi-thread"] }
16+
tokio-retry = "0.3"
1917

2018
[[bin]]
2119
name = "lambda-adapter"

Dockerfile.mac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
FROM scratch
22
ARG ARCH=x86_64
3-
COPY target/${ARCH}-unknown-linux-musl/release/bootstrap /opt/bootstrap
3+
COPY target/${ARCH}-unknown-linux-musl/release/lambda-adapter /opt/extensions/lambda-adapter

examples/expressjs-zip/hello-world/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const app = express()
33
const port = process.env['PORT'] || 8080
44

55

6-
app.get('/', (req, res) => {
6+
app.get('/Prod', (req, res) => {
77
res.send('Hi there!')
88
})
99

examples/expressjs/app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM public.ecr.aws/docker/library/node:16.13.2-stretch-slim
2-
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.2.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
2+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.3.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
33
EXPOSE 8080
44
WORKDIR "/var/task"
55
ADD src/package.json /var/task/package.json

examples/flask/app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM public.ecr.aws/docker/library/python:3.8.12-slim-buster
2-
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.2.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
2+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.3.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
33
WORKDIR /var/task
44
COPY app.py requirements.txt ./
55
RUN python -m pip install -r requirements.txt

examples/nginx/app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM public.ecr.aws/docker/library/nginx:1.21.6
2-
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.2.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
2+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.3.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
33
WORKDIR "/tmp"
44
ADD config/ /etc/nginx/
55
ADD images/ /usr/share/nginx/html/images

examples/php/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM public.ecr.aws/amazonlinux/amazonlinux:2.0.20220121.0
22

3-
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.2.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
3+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.3.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
44

55
RUN amazon-linux-extras install -y nginx1 php7.4 && yum clean all && rm -rf /var/cache/yum
66

examples/springboot/app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ COPY pom.xml ./
55
RUN mvn -q clean package
66

77
FROM public.ecr.aws/docker/library/amazoncorretto:8u322-al2
8-
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.2.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
8+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.3.0 /opt/extensions/lambda-adapter /opt/extensions/lambda-adapter
99
EXPOSE 8080
1010
WORKDIR /opt
1111
COPY --from=build-image /task/target/petstore-0.0.1-SNAPSHOT.jar /opt

src/main.rs

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use lambda_http::{
5-
handler,
6-
lambda_runtime::{self, Context},
7-
Body, IntoResponse, Request, RequestExt, Response,
8-
};
4+
use lambda_extension::{service_fn as extension_handler, Extension};
5+
use lambda_http::{service_fn as http_handler, Body, IntoResponse, Request, Response};
96
use log::*;
107
use once_cell::sync::OnceCell;
118
use reqwest::{redirect, Client};
12-
use serde_json::json;
13-
use std::{env, mem, thread};
14-
use tokio_retry::strategy::FixedInterval;
15-
use tokio_retry::Retry;
16-
use url::form_urlencoded::Serializer;
9+
use std::{env, mem};
10+
use tokio::runtime::Handle;
11+
use tokio_retry::{strategy::FixedInterval, Retry};
1712

1813
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
1914
static HTTP_CLIENT: OnceCell<Client> = OnceCell::new();
@@ -23,25 +18,16 @@ async fn main() -> Result<(), Error> {
2318
env_logger::init();
2419

2520
// register as an external extension
26-
let aws_lambda_runtime_api = env::var("AWS_LAMBDA_RUNTIME_API").unwrap();
27-
let extension_next_url = format!("http://{}/2020-01-01/extension/event/next", aws_lambda_runtime_api);
28-
let extension_register_url = format!("http://{}/2020-01-01/extension/register", aws_lambda_runtime_api);
29-
let executable_name = env::current_exe().unwrap().file_name().unwrap().to_string_lossy().to_string();
30-
let client = reqwest::blocking::ClientBuilder::new().timeout(None).build().unwrap();
31-
let resp = client
32-
.post(extension_register_url)
33-
.header("Lambda-Extension-Name", executable_name)
34-
.json(&json!({"events": []}))
35-
.send()?;
36-
let extension_id = resp.headers().get("Lambda-Extension-Identifier").unwrap().clone();
37-
thread::spawn(move || {
38-
let extension_id_str = extension_id.to_str().unwrap();
39-
debug!("[extension] enter event loop for extension id: '{}'", extension_id_str);
40-
client
41-
.get(extension_next_url)
42-
.header("Lambda-Extension-Identifier", extension_id_str)
43-
.send()
44-
.unwrap();
21+
let handle = Handle::current();
22+
tokio::task::spawn_blocking(move || {
23+
handle.spawn(async {
24+
Extension::new()
25+
.with_events(&[])
26+
.with_events_processor(extension_handler(|_| async { Ok::<(), Error>(()) }))
27+
.run()
28+
.await
29+
.expect("extension thread error");
30+
})
4531
});
4632

4733
// check if the application is ready every 10 milliseconds
@@ -57,30 +43,15 @@ async fn main() -> Result<(), Error> {
5743

5844
// start lambda runtime
5945
HTTP_CLIENT.set(Client::builder().redirect(redirect::Policy::none()).build().unwrap()).unwrap();
60-
lambda_runtime::run(handler(http_proxy_handler)).await?;
46+
lambda_http::run(http_handler(http_proxy_handler)).await?;
6147
Ok(())
6248
}
6349

64-
async fn http_proxy_handler(event: Request, _: Context) -> Result<impl IntoResponse, Error> {
50+
async fn http_proxy_handler(event: Request) -> Result<impl IntoResponse, Error> {
6551
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
6652
let app_host = format!("http://127.0.0.1:{}", port);
67-
let query_params = event.query_string_parameters();
68-
debug!("query_params are {:#?}", query_params);
69-
7053
let (parts, body) = event.into_parts();
71-
let mut app_url = app_host + parts.uri.path();
72-
73-
// append query parameters to app_url
74-
if !query_params.is_empty() {
75-
app_url.push('?');
76-
let mut serializer = Serializer::new(&mut app_url);
77-
for (key, _) in query_params.iter() {
78-
for value in query_params.get_all(key).unwrap().iter() {
79-
serializer.append_pair(key, value);
80-
}
81-
}
82-
serializer.finish();
83-
}
54+
let app_url = app_host + parts.uri.path_and_query().unwrap().as_str();
8455
debug!("app_url is {:#?}", app_url);
8556
debug!("request headers are {:#?}", parts.headers);
8657

@@ -102,7 +73,6 @@ async fn http_proxy_handler(event: Request, _: Context) -> Result<impl IntoRespo
10273
}
10374

10475
async fn convert_body(app_response: reqwest::Response) -> Result<Body, Error> {
105-
let content_type;
10676
debug!("app response headers are {:#?}", app_response.headers());
10777

10878
if app_response.headers().get(http::header::CONTENT_ENCODING).is_some() {
@@ -111,11 +81,11 @@ async fn convert_body(app_response: reqwest::Response) -> Result<Body, Error> {
11181
return Ok(Body::Binary(content.to_vec()));
11282
}
11383

114-
if let Some(value) = app_response.headers().get(http::header::CONTENT_TYPE) {
115-
content_type = value.to_str().unwrap_or_default();
84+
let content_type = if let Some(value) = app_response.headers().get(http::header::CONTENT_TYPE) {
85+
value.to_str().unwrap_or_default()
11686
} else {
117-
content_type = "";
118-
}
87+
""
88+
};
11989
debug!("content_type is {:?}", content_type);
12090

12191
if content_type.starts_with("text")

0 commit comments

Comments
 (0)