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

Improved Nginx Example #192

Merged
merged 9 commits into from
Mar 12, 2023
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
11 changes: 11 additions & 0 deletions examples/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM public.ecr.aws/awsguru/nginx:1.23.2023.3.11.1

COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.3 /lambda-adapter /opt/extensions/lambda-adapter

# config files
ADD nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf

# code files
COPY app /var/task/

EXPOSE 8080
52 changes: 32 additions & 20 deletions examples/nginx/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
# nginx example
# Nginx example

A basic nginx web server runs inside AWS Lambda.
A basic Nginx web server runs inside AWS Lambda.

Using AWS Lambda Adapter, you can package this web server into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.
You can package this web server into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.

The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition.
The application can be deployed in an AWS account using
the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in
the root folder contains the application definition.

The top level folder is a typical AWS SAM project. The `app` directory is the nginx configuration with a [Dockerfile](app/Dockerfile).
The top level folder is a typical AWS SAM project. The `app` directory is the nginx configuration with
a [Dockerfile](Dockerfile).

```dockerfile
FROM public.ecr.aws/docker/library/nginx:1.21.6
FROM public.ecr.aws/awsguru/nginx:1.23.2023.3.11.1

COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.3 /lambda-adapter /opt/extensions/lambda-adapter
WORKDIR "/tmp"
ADD config/ /etc/nginx/
ADD images/ /usr/share/nginx/html/images
CMD ["nginx", "-g", "daemon off;"]

# config files
ADD nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf

# code files
COPY app /var/task/

EXPOSE 8080
```

Line 2 copies lambda adapter binary into /opt/extensions. This is the main change to run the nginx server on Lambda.
Line 3 copies Lambda adapter binary into /opt/extensions. This is the main change to run the Nginx server on Lambda.

```dockerfile
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.3 /lambda-adapter /opt/extensions/lambda-adapter
Expand All @@ -31,49 +39,53 @@ The following tools should be installed and configured.
* [SAM CLI](https://github.com/awslabs/aws-sam-cli)
* [Docker](https://www.docker.com/products/docker-desktop)


## Deploy to Lambda

Navigate to the sample's folder and use the SAM CLI to build a container image

```shell
$ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
$ sam build
```

This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.

To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the
instructions on the screen

```shell
$ sam deploy --guided
```

Please take note of the container image name.
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use `curl` or a web browser to make a call to the URL
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You
can use `curl` or a web browser to make a call to the URL

```shell
...
---------------------------------------------------------------------------------------------------------
OutputKey-Description OutputValue
---------------------------------------------------------------------------------------------------------
PetStoreApi - URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
---------------------------------------------------------------------------------------------------------
...

$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
```

Lambda Adapter also automatic encode/decode binary data for you. Open the output link in your browser, add "images/space.jpeg" to the url, you will see a picture of the space.
Lambda Adapter also automatic encode/decode binary data for you. Open the output link in your browser, add "
images/space.jpeg" to the url, you will see a picture of the space.

https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/images/space.jpeg

![space](app/images/space.jpeg)
![space](app/public/images/space.jpeg)

## Run the docker locally

We can run the same docker image locally, so that we know it can be deployed to ECS Fargate and EKS EC2 without code changes.
We can run the same docker image locally, so that we know it can be deployed to ECS Fargate and EKS EC2 without code
changes.

```shell
$ docker run -d -p 8080:8080 {ECR Image}

```

Use curl to verify the docker container works.
Expand Down
6 changes: 0 additions & 6 deletions examples/nginx/app/Dockerfile

This file was deleted.

45 changes: 0 additions & 45 deletions examples/nginx/app/config/conf.d/default.conf

This file was deleted.

36 changes: 0 additions & 36 deletions examples/nginx/app/config/nginx.conf

This file was deleted.

24 changes: 24 additions & 0 deletions examples/nginx/app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/nginx/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
webserver:
build: .
platform: linux/amd64
ports:
- "8080:8080"
volumes:
- ./app:/var/task/app
- ./nginx/conf/nginx.conf:/opt/nginx/conf/nginx.conf
39 changes: 39 additions & 0 deletions examples/nginx/nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/

error_log /dev/stderr;
worker_rlimit_core 100m;
working_directory /tmp;
worker_processes 1;
pid /tmp/nginx.pid;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /dev/stdout main;
error_log /dev/stderr;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;

include /opt/nginx/conf/mime.types;
default_type application/octet-stream;

server {
listen 8080;
server_name _;
root /var/task/app/public;

index index.html index.htm;
}

}
24 changes: 16 additions & 8 deletions examples/nginx/template.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
serverless-nginx-demo

Sample SAM Template for sam-app
Sample SAM Template for serverless-nginx-demo

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 29

Resources:
NginxFunction:
Function:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Architectures:
- x86_64
MemorySize: 1024
Policies:
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
Environment:
Variables:
RUST_LOG: info
RUST_LOG: debug
PORT: 8080
Events:
Root:
Type: HttpApi
Expand All @@ -34,11 +37,16 @@ Resources:
Method: ANY
Metadata:
DockerTag: v1
DockerContext: ./app
DockerContext: .
Dockerfile: Dockerfile

Outputs:
NginxApi:
Description: "API Gateway endpoint URL for Prod stage for Nginx function"
URL:
Description: "API Gateway endpoint URL for Prod stage for Nginx application"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"

Function:
Description: "Lambda Function ARN"
Value: !GetAtt Function.Arn
FunctionRole:
Description: "Implicit IAM Role created for function"
Value: !GetAtt FunctionRole.Arn