convert flask application into aws lambda using Zappa

702 views Asked by At

I have a flask_application which will do some simple task.

My Requirement: I want to deploy this flask_application as a aws lambda_function with the help of zappa

To run my flask_application as a lambda function, i am performing

  1. zappa init
  2. zappa package

and then i am uploading that zipped file into lambda and running my lambda function.

My Issue: when ever i run zappa init command, it is creating a zappa_setting.json file with some info like below

{
    "dev": {
        "app_function": "flask.main.app",
        "aws_region": "us-east-1",
        "profile_name": "dev",
        "project_name": "flask",
        "runtime": "python3.6",
        "s3_bucket": "zappa-p5m0kuurp"
    }
}

I have 3 aws environments and seperate buckets for each environment like

test  ==> zappa_test_bucket
stage ==> zappa_stage_bucket
prod  ==> zappa_prod_bucket

I want to deploy my flask application in all 3 aws environments by providing custom zappa_settings.json.

Question: Is there a way to provide zappa_settings.json as a file and then whenever i run zappa init, zappa will take this file as a input and set these environmental varible according to environment.

zappa_settings.json

{
    "test": {
        "app_function": "flask.main.app",
        "aws_region": "us-east-1",
        "profile_name": "dev",
        "project_name": "flask",
        "runtime": "python3.6",
        "s3_bucket": "zappa_test_bucket"
    },
        "stage": {
        "app_function": "flask.main.app",
        "aws_region": "us-east-1",
        "profile_name": "dev",
        "project_name": "flask",
        "runtime": "python3.6",
        "s3_bucket": "zappa_stage_bucket"
    },

    "prod": {
        "app_function": "flask.main.app",
        "aws_region": "us-east-1",
        "profile_name": "dev",
        "project_name": "flask",
        "runtime": "python3.6",
        "s3_bucket": "zappa_prod_bucket"
    }


}

if anyone have an idea, please let me know

1

There are 1 answers

6
amsh On BEST ANSWER

What I understand is that you use zappa init each time you deploy the code. After that you upload your code to lambda manually.

You must rather use the functionalities of zappa to the fullest:

  • Use zappa init just once.
  • Update your zappa_settings.json file just once.
  • From now own, do your deployments with zappa deploy dev/stage/prod

See this minimal flask zappa tutorial to know more.

If there's something I'm missing and you really have to pass zappa_settings.json file, you can't pass it, but you can keep a backup in your repository as zappa_settings.json.backup and right after zappa init you can call cp zappa_settings.json.backup zappa_settings.json. This will save you from creating that file from scratch again and again.