Created
June 14, 2020 01:05
-
-
Save kukielp/6bedfb522d50c3e13a5d0414ef70137d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Update packages and install needed compilation dependencies | |
sudo yum update -y | |
sudo yum install autoconf bison gcc gcc-c++ libcurl-devel libxml2-devel -y | |
# Compile OpenSSL v1.0.1 from source, as Amazon Linux uses a newer version than the Lambda Execution Environment, which | |
# would otherwise produce an incompatible binary. | |
curl -sL http://www.openssl.org/source/openssl-1.0.1k.tar.gz | tar -xvz | |
cd openssl-1.0.1k | |
./config && make && sudo make install | |
cd ~ | |
# Download the PHP 7.3.0 source | |
mkdir -p ~/environment/php-7-bin | |
curl -sL https://github.com/php/php-src/archive/php-7.3.0.tar.gz | tar -xvz | |
cd php-src-php-7.3.0 | |
# Compile PHP 7.3.0 with OpenSSL 1.0.1 support, and install to /home/ec2-user/php-7-bin | |
./buildconf --force | |
./configure --prefix=/home/ec2-user/environment/php-7-bin/ --with-openssl=/usr/local/ssl --with-curl --with-zlib | |
make install | |
# CD to the directory we compiled the PHP source code to, here we can view the binaries: | |
cd /home/ec2-user/environment/php-7-bin | |
ls -la | |
# We need a sample bootstrap for lambda, this is a executable script and is the entry point for the custom run time. | |
wget https://raw.githubusercontent.com/aws-samples/php-examples-for-aws-lambda/master/bootstrap | |
# ensure the bootstrap is executable | |
chmod +x bootstrap | |
# zip up the ./bin folder and files and the bootstrap. This is now ready to deploy. | |
zip -r runtime.zip bin bootstrap | |
# The bootstrap uses a few other php utilities to operate. If we look inside the bootstrap file we can see where this is used. These 2 layers could be just one, but this allows us to keep the PHP runtime separate to the vendor layer in case there is a update to the vendor layer we only need to rebuild/deploy that layer. | |
# This command will download composer ( a php dependency manager ) and then use that to install the guzzlehttp/guzzle packages. | |
curl -sS https://getcomposer.org/installer | ./bin/php | |
./bin/php composer.phar require guzzlehttp/guzzle | |
# zip up the ./vendor folder and files. This is now ready to deploy. There is no bootstrap file required for this layer, this layer is dependent on the PHP runtime that already contains the entry point. | |
zip -r vendor.zip vendor/ | |
# In the example we will deploy to the same region that we launched Cloud9 in. This command will call the metadata end point available on all AWS ec2 instances to get the region and save it to an environment variable: | |
AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/') | |
# For example purpose lets have a quick look, install a terminal web browser | |
sudo yum install w3m | |
# Open the URL in the browser, press q to exit, press y to conform then press enter. | |
w3m http://169.254.169.254/latest/meta-data/placement/availability-zone | |
# We can now deploy both the PHP runtime | |
aws lambda publish-layer-version \ | |
--layer-name PHP-73-runtime \ | |
--zip-file fileb://runtime.zip \ | |
--region $AWS_REGION | |
# And the vendor layer to AWS | |
aws lambda publish-layer-version \ | |
--layer-name PHP-73-vendor \ | |
--zip-file fileb://vendor.zip \ | |
--region $AWS_REGION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment