Serverless Resume API with Terraform and GitHub Actions
Tags: aws, api, serverless, python
Published: 2024-07-12 10:55:32

**Introduction**

In today's world, building applications that automatically adjust to changing demands is crucial. A unique way to showcase this is by deploying your resume in JSON format using serverless AWS services. This guide will walk you through deploying your JSON resume data with Terraform, leveraging AWS Lambda, DynamoDB, and API Gateway. This setup ensures your resume is always available, scales seamlessly, and can be accessed through a user-friendly API.
Before we begin, ensure you have the following:

1. AWS Account: An active AWS account with the necessary permissions.
2. Terraform: Installed on your local machine.
3. AWS CLI: Installed and configured on your local machine.

**Step 1: Define Your JSON Resume Data**
Create your resume in JSON format. You can use a JSON schema from the link below:
               https://jsonresume.org/schema

**Step 2: Define Your Lambda function**
We will be creating a python code that our lambda function will use to retrieve the resume data from DynamoDB. See complete code in the repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nyli6y6p9g0ptmtd6y7.png)

**Step 3: Create Terraform Configuration**
Create a new directory for your Terraform configuration files. Inside this directory, we will create a sub-directory for our modules. Each folder in the modules directory will contain the infrastructures required to be deployed for each AWS service. See below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3zgktlnh1e6pb3t1no2.png)



-  **Api Gateway module:** This describes the detailed terraform configuration for our Api Gateway. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9y5kgn18w188ibubs9h.png)

 

-  **DynamoDB Module:**
  This module contains the terraform codes for deploying DynamoDB and also uploading the resume JSON data on it.


 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw7q029htn91vik3smwc.png)


- **Iam_Role Module:** This module contains the terraform codes that defines the necessary permissions for lambda function to access DynamoDB and CloudWatch.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f8i6cufoah89602w9gb.png)
 

- **Lambda Module:** This module defines the terraform codes for archiving the python code in a zip file and also create a lambda function from same.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jngeevtdtbyoqlznusv.png)

The full terraform configuration codes can be found in the repository below:
https://github.com/Samuel7050/AWS_RESUME_CHALLENGE.git

**Step 3: Test your codes locally**
Run `terraform init` to initialize the terraform plugins and providers


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ucurw0eybribhlr6222.png)

Run `terraform validate` to validate the terraform codes 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzgiihsusfctp46eacti.png)

Run `terraform plan` to preview the infrastructures to be deployed. Also note that the Zip file for the lambda function will also be created in the project directory.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/608dd4t5rc4qwbcr60f5.png)

Run `terraform apply --auto-approve` to deploy the infrastructure on AWS.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50q3h40ml1f7am6pufda.png)

Copy the** api_gateway_invoke_url** as indicated in the output above and run a test locally from VScode . Ensure you include **/path_part **of the api gateway. See below 

https://susia7qsb6.execute-api.us-east-1.amazonaws.com/dev/data


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwdyc0dy90rsp1bo13x1.png)

Paste on a web browser 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5uj7qbq5nlo4qp9deg0.png)

To clean up locally, run terraform `destroy --auto-approve`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq00u6mbemrbkv9902x3.png)

**Step 4: Configure GitHub Actions**
To automate the deployment process using GitHub Actions, create a .github/workflows directory in your project root directory and add a file named deploy.yml


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pomii27ot5cbcjiq4wm3.png)

**Step 5: Configure GitHub Secrets**
In your GitHub repository, navigate to Settings > Secrets and variables and add the following secrets and variable, as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7cil23scirw0jr8cpj6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7w1my7z4h3w4n2y7vm7u.png)

Whenever there is a push action on the main branch or a pull request to the main branch, the GitHub action is automatically triggered. See result of a push action to the main branch of the repository.

The PR from my feature branch to main branch failed as seen below 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpdb7iqn0ann09n67wtm.png)

The workflow failed because of already existing policy as seen in the error code below 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5foljqrc5qybwq6l59o.png)

Lets fix the error and do another PR the to main branch again to trigger the workflow


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkp5wptaxozeho6qerkn.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uravui8718v3mhjfa985.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz2lza0do3fj25av68ge.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6fcg0sr59ftdip6czi4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dooigt1qri3rali9dqb.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxjxb8fhbdg59obcr1br.png)

To verify, copy the api_gateway_invoke_url/data as shown below and run on your browser. Note that /data is my api gateway path_part

https://sx60706aef.execute-api.us-east-1.amazonaws.com/dev/data

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25zgg24d6udfynnmqr9w.png)

**Conclusion**
By following these steps, you've deployed your resume in JSON format using AWS Lambda, DynamoDB, API Gateway, and automated the process using GitHub Actions. This setup ensures your resume is highly available, scalable, and easily accessible via a REST API, with the added benefit of continuous integration and deployment. You can now share your resume API endpoint with potential employers or integrate it into your personal website.