Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.
Tags: aws, awscommunity, serverless, devtool
Published: 2023-03-28 03:30:52



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](https://www.serverless.com/blog/flask-python-rest-api-serverless-lambda-dynamodb/#converting-an-existing-flask-application) 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](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-file.html).

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 

```

Remember: In this case, Codebuild is only used to run the deploy command. All the resources are built by serverless by automatically running a cloudformation.

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5n51ulma3twzipf83pt6.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3xyr8r64is33y00qjqd.png)

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


![CodePipeline success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nce9vyaltow0y5ewyq2o.png)

Here are the outputs from the cloudformation stack.


![Cloudformation outputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxjs2kp5mb19f4yc8v19.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxl7i306ordfkeq3uzvb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyw2ug7qovo6nabvfmut.png)

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