Skip to content

Instantly share code, notes, and snippets.

@rtancman rtancman/1) Dockerfile
Last active Feb 7, 2019

Embed
What would you like to do?
Python install depencies for AWS Lambda with Amazon Linux AMI
FROM amazonlinux
RUN yum update -y
RUN yum groupinstall "Development Tools" -y
RUN yum install -y \
gcc \
bzip2-devel \
libffi \
openssl-devel \
perl-core \
zlib-devel \
libxml2-devel \
libxslt-devel \
libffi-devel \
libcffi-devel \
wget \
postgresql96-devel \
mysql56-devel \
sqlite
RUN curl -O http://download.redis.io/redis-stable.tar.gz && \
tar xvf redis-stable.tar.gz && \
cd redis-stable && \
make && make install && rm -rf redis-stable.tar.gz
ENV PYTHON_VERSION 3.7.2
RUN cd /usr/src/ && \
wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz && \
tar xzf Python-$PYTHON_VERSION.tgz && \
cd Python-$PYTHON_VERSION && \
./configure --enable-optimizations && \
make altinstall && \
cd / && rm -rf /usr/src/Python-$PYTHON_VERSION.tgz && python3.7 -V
RUN pip3.7 install --upgrade pip && pip3.7 install awscli setuptools wheel
RUN mkdir /app
WORKDIR /app
CMD /bin/bash
lambda.docker.build:
@echo "---- Create amazon linux image base ----"
@docker build -t amazon-linux-python37 .
lambda.docker.pkgcreate:
@echo "---- Create Lambda Package using amazon linux image base ----"
@docker run --rm -u `id -u` \
-v ~/.aws/:/home/lambda/.aws/ \
-v `pwd`:/app \
-e "HOME=/home/lambda" \
amazon-linux-python37 pip3.7 install -r requirements.txt -t .
#criando um novo dir
mkdir docker_awslambdas_python_dependencias && cd docker_awslambdas_python_dependencias
#criando o arquivo main.py
echo -e "
import requests
def handle(event, context):
r = requests.get('https://api.github.com/events')
return {
'texto': r.text,
}
" >> main.py
#criando o requirements.txt
echo -e "requests" >> requirements.txt
# Rodar o container para instalar as dependências com os comandos do Makefile
make lambda.docker.build lambda.docker.pkgcreate
# zipando
zip -qr dockerhellorequests.zip .
# criar a nossa função via awscli.
aws lambda create-function \
--region us-east-1 \
--handler main.handle \
--runtime python3.7 \
--function-name 'dockerhellorequests' \
--zip-file fileb://./dockerhellorequests.zip \
--role 'PEGUE_SUA_ARN'
#executando o lambda
aws lambda invoke \
--region us-east-1 \
--function-name 'dockerhellorequests' \
--payload '{}' \
dockerhellorequests.output
cat dockerhellorequests.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.