Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.

For the past couple of days. I have been working with the Serverless framework. I came to know that you can actually host your flask API that integrates several AWS services such as Lambda, API Gateway, and DynamoDB using this framework. I had to spend…

For the past couple of days. I have been working with the Serverless framework. I came to know that you can actually host your flask API that integrates several AWS services such as Lambda, API Gateway, and DynamoDB using this framework. I had to spend a lot of time to understand the process at first since there is not a lot of content regarding this implementation. So, here’s me contributing a little.

To make this swift, I wanted to integrate the entire process with a pipeline that contains the code and a build environment. Here’s how everything is carried out. Please understand that this is not a beginner level tutorial and the whole process is written in assumption that the readers have knowledge and experience with AWS services and Python Flask.

First, you need to have a flask api. An API that works. I couldn’t share the actual code that I first used since it is confidential . So, I have decided to go with a template of serverless framework. You can also convert your flask api to be compatible with serverless using the serverless-wsgi plugin. For this, you may need to make some changes to your serverless.yaml configurations. You can check it here to achieve that

Now that you have your api , lets upload it again to an aws codecommit repository. You can clone an empty repo, add the files, commit and push. Check it here.

Next, we need to create a build project in AWS CodeBuild.

In this codebuild project, use the following buildspec.yaml file. You can choose an Ubuntu environment for the build project to run.

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 14
    commands:
        - npm install -g serverless
        - npm install serverless-compose
        - serverless --version
        - npm install serverless-deployment-bucket
        - sls plugin install -n serverless-wsgi
        - sls plugin install -n serverless-python-requirements

  build: 
    commands: 
      - serverless deploy 

Now, we’re also gonna need another yaml file. This one is the configuration file for the serverless framework which will be deployed once we deploy it using the Aws pipeline.

Here’s the configuration code.

service: new12

frameworkVersion: '3'

custom:
  wsgi:
    app: app.hello

provider:
  name: aws
  runtime: python3.9

functions:
  api:
    handler: wsgi_handler.handler
    events:
      - httpApi: '*'

plugins:
  - serverless-wsgi
  - serverless-python-requirements


Also keep your flask api file in the same directory. Make sure to configure codebuild to look for the specific branch in the specific repo.

File structure in the repository

Once everything is created, we need to make sure that our Codebuild service role has enough permission to use cloudformation and all the other services associated with the resources built through the cloudformation stack.

In my case, I faced a lot of failed builds due to permission issues.Always make sure to check your build logs when your build fails, so that you can get a clear understanding of the underlying reasons and issues.

So, to make it quick I added some permission to the codebuild service role. I suggest to create your own permission policies based on the least privilege access policy rather than assigning managed policies with Full access.

Listed AIM Policies

Once the permissions are set, your pipeline will run swiftly. Here’s how my pipeline looks like with the two phases.

CodePipeline success

Here are the outputs from the cloudformation stack.

Cloudformation outputs

You invoke URL will be generated in Amazon API Gateway. Here in the build logs, you can see an invoke URL for the api.

Codbuild logs

The serverless Framework will also help you monitor your resources and their performance through the console. All you need to do is login to the console and connect to your aws account. The console will run another cloudformation stack to integrate with the resources in our aws account. Here is how the dashboard looks like.

Serverless console

Thank you for making it to the end, hope You found what you were looking for!


Print Share Comment Cite Upload Translate
APA
ajaydhungel23 | Sciencx (2024-03-29T05:53:34+00:00) » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.. Retrieved from https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/.
MLA
" » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.." ajaydhungel23 | Sciencx - Tuesday March 28, 2023, https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/
HARVARD
ajaydhungel23 | Sciencx Tuesday March 28, 2023 » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.., viewed 2024-03-29T05:53:34+00:00,<https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/>
VANCOUVER
ajaydhungel23 | Sciencx - » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.. [Internet]. [Accessed 2024-03-29T05:53:34+00:00]. Available from: https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/
CHICAGO
" » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.." ajaydhungel23 | Sciencx - Accessed 2024-03-29T05:53:34+00:00. https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/
IEEE
" » Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.." ajaydhungel23 | Sciencx [Online]. Available: https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/. [Accessed: 2024-03-29T05:53:34+00:00]
rf:citation
» Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API. | ajaydhungel23 | Sciencx | https://www.scien.cx/2023/03/28/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api/ | 2024-03-29T05:53:34+00:00
https://github.com/addpipe/simple-recorderjs-demo