Git Product home page Git Product logo

chalice_dockerized's Introduction

How to use deploy with docker-compose for development purposes

1. Install chalice framework locally: learn more from chalice quick start

# check your python version
python3 --version
# if you are something like Python 3.7.3 and above, you are good to go!

# Next we’ll install Chalice using pip:
python3 -m pip install chalice

# You can verify you have chalice installed by running:
chalice --help

2. Create brand new project.

chalice new-project your-project-name

3. Check AWS settings:

# create this folder if you don't have it
mkdir ~/.aws
cat >> ~/.aws/config
[default]
aws_access_key_id=YOUR_ACCESS_KEY_HERE
aws_secret_access_key=YOUR_SECRET_ACCESS_KEY
region=YOUR_REGION (such as us-west-2, us-west-1, etc)

4. Copy your requirements file for docker.

cp requirements.txt requirements-docker.txt

# Add this one line to requirements-docker.txt
# you need it to make sure that you have chalice in your Docker container.

chalice

5. Let's start dockerizing!

  • Create custom Dockerfile
FROM alpine:3.7

WORKDIR /app
COPY requirements-docker.txt ./
RUN \
apk add --no-cache python3 postgresql-libs && \
apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev && \
apk add --no-cache postgresql-dev && \
python3 -m pip install -r requirements-docker.txt --no-cache-dir && \
python3 -m pip install --upgrade pip && \
apk --purge del .build-deps

COPY . .
CMD [ "/bin/sh"]
  • Create environment file. It will be used in your docker-compose file later.
touch .env
vi .env

######
# Add put there the following content:
#
# YOUR_AWS_ACCESS_KEY_ID
# YOUR_AWS_SECRET_ACCESS_KEY
# YOUR_REGION
# fill with actual aws credentials, taken from
# cat ~/.aws/config
######

APP_NAME=
APP_PATH=/app/${APP_NAME}
AWS_ACCESS_KEY_ID={YOUR_AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY={YOUR_AWS_SECRET_ACCESS_KEY}
AWS_DEFAULT_REGION={YOUR_REGION}
  • Create docker-compose.yaml file with following contents
version: "3.8"

services:
  app:
    build: .
    env_file:
      - .env
    ports:
      - "80:8000"
    volumes:
      - .:/app
    tty: true
    stdin_open: true
    working_dir: "${APP_PATH}"
    environment:
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
  • Build and run containers
docker-compose up --build -d

6. Code your simple lambda function

# just copy/paste it to your app.py
 
@app.lambda_function()
def mylambda(event, context):
    return {'response': 'great'}

7. Now you are ready to deploy your first lambda

docker-compose run app chalice deploy

8. List your functions

Here you might want to install AWS CLI first. PLease learn how to to it from here:

AWS CLI installation

aws lambda list-functions

You'll have listing of lambda files in json format. Look for something like "FunctionName": "your-lambda-function-name"

9. Invoke your function

In my case lambda function name is dockerized-dev, so, the command will look like this:

aws lambda invoke --function-name dockerized-dev result.txt

Where result.txt will keep the response from your lambda.

cat result.txt

And you'll get {'response': 'great'} in it. If not....

Check this repo: or drop me a line:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.