Skip to content
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
4 changes: 3 additions & 1 deletion examples/php/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ target/
.vscode/

.mvn
.aws-sam
.aws-sam

/app/vendor/
17 changes: 17 additions & 0 deletions examples/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM public.ecr.aws/awsguru/php:82.2023.3.11.1 AS builder
COPY --from=composer /usr/bin/composer /usr/local/bin/composer

COPY app /var/task/app
WORKDIR /var/task/app

RUN composer install --prefer-dist --optimize-autoloader --no-dev --no-interaction

FROM public.ecr.aws/awsguru/php:82.2023.3.11.1
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.3 /lambda-adapter /opt/extensions/lambda-adapter
COPY --from=builder /var/task /var/task

# config files
ADD nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf
ADD php/php.ini /opt/php/php.ini
ADD php/etc/php-fpm.conf /opt/php/etc/php-fpm.conf
ADD php/php.d/extensions.ini /opt/php/php.d/extensions.ini
50 changes: 33 additions & 17 deletions examples/php/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# PHP example

A basic php application runs inside AWS Lambda.
A basic PHP application runs inside AWS Lambda.

Using AWS Lambda Adapter, you can package this php application into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.
You can package this PHP application 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/awsguru/serverless-php:0.1.0
FROM public.ecr.aws/awsguru/php:82.2023.3.11.1 AS builder
COPY --from=composer /usr/bin/composer /usr/local/bin/composer

ADD conf.d/ /etc/nginx/conf.d/
ADD php-fpm.d/ /etc/php-fpm.d/
ADD html/ /app/html
```
COPY app /var/task/app
WORKDIR /var/task/app

RUN composer install --prefer-dist --optimize-autoloader --no-dev --no-interaction

FROM public.ecr.aws/awsguru/php:82.2023.3.11.1
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.3 /lambda-adapter /opt/extensions/lambda-adapter
COPY --from=builder /var/task /var/task

# config files
ADD nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf
ADD php/php.ini /opt/php/php.ini
ADD php/etc/php-fpm.conf /opt/php/etc/php-fpm.conf
ADD php/php.d/extensions.ini /opt/php/php.d/extensions.ini
```

## Pre-requisites

Expand All @@ -25,23 +39,26 @@ 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.
This command compiles the application and prepares a deployment package in the `.aws-sam` subdirectory.

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
...
Expand All @@ -55,14 +72,13 @@ PhpFunction - URL for application https://xxxxxxxxxx.execute-api.us-w
$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/
```


## 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
5 changes: 0 additions & 5 deletions examples/php/app/Dockerfile

This file was deleted.

18 changes: 18 additions & 0 deletions examples/php/app/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lambda/php",
"autoload": {
"psr-4": {
"App\\": "./"
}
},
"require": {
"php": "^8.0.2"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
20 changes: 20 additions & 0 deletions examples/php/app/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions examples/php/app/conf.d/www.conf

This file was deleted.

2 changes: 0 additions & 2 deletions examples/php/app/html/index.php

This file was deleted.

Loading