This project involves creating a Python application that provides a REST endpoint. This endpoint returns a JSON response containing the current timestamp along with a static message.:
{
“message”: “Automate all the things!”,
“timestamp”: 1529729125
}
The application is then deployed into a kubernetes cluster created using kubeadm. This repository contains the Terraform code to deploy resources on your AWS account. This README provides step-by-step instructions on how to execute the Terraform code in this project to deploy resources on your AWS account.
Infrastructure as code (IAC) – Terraform
Cloud infrastructure - AWS core services (VPC, Subnet, ec2 instance, load balancer, etc.)
Version Control System (VCS) – Github
Configuration Management - Ansible
Programming Language - Python
Entry point to cluster - HAProxy
This code creates a basic web application that provides two routes. The first route ("/get_data") returns a JSON response with a message and a timestamp, while the second route ("/") displays a welcome message when the main page is accessed.
The code starts by importing two libraries: Flask and jsonify. Flask is a Python framework used for building web applications. jsonify is a utility function provided by Flask to create JSON responses.
app = Flask(name) creates an instance of the Flask application. This instance is used to define routes and handle incoming requests.
@app.route("/get_data", methods=["GET"]) is a decorator indicating that the function below it should be executed when a request is made to the "/get_data" route. The methods=["GET"] part specifies that this route will only respond to HTTP GET requests.
Inside the get_data() function, the current timestamp is calculated using the time.time() function. It creates a dictionary named data containing a message and the timestamp.
The function returns jsonify(data), which converts the data dictionary into a JSON-formatted response.
@app.route("/", methods=["GET"]) sets up a route for the root URL path ("/"). This means that when a user visits the main page of the application, the function index() will be called.
The index() function returns a simple string: "Welcome to my Flask app!". This will be displayed in the browser when a user visits the root URL.
The final block if name == "main": checks if the script is being run directly. If it is being run directly, it starts the Flask application with app.run(). The debug=True flag provides additional information for development, and host="0.0.0.0" makes the app accessible from all network interfaces on the machine.
Before you begin, make sure you have the following prerequisites:
- An AWS account - You'll need an active AWS account to deploy resources using this Terraform code.
- AWS CLI - Install the AWS Command Line Interface (CLI) on your local machine and configure it with your AWS credentials.
- Terraform - Install Terraform on your local machine.
- Git - Install Git on your local machine to clone this repository.
- AWS hosted zone should be create in your AWS account.
Follow these steps to deploy resources on your AWS account using Terraform:
Clone this GitHub repository to your local machine using the following command:
git clone https://github.com/eamanze/api-project.git
Change your working directory to the cloned repository:
cd api-project/terraform/
Important: Before applying the Terraform configuration, ensure that you update the following variables with your own values in the variable.tf file:
profile = ""
region = ""
project-name = ""
ami = ""
cidr = "10.0.0.0/16"
az = ["us-west-2a", "us-west-2b"]
public-cidr = ["10.0.1.0/24", "10.0.2.0/24"]
private-cidr = ["10.0.3.0/24", "10.0.4.0/24"]
Run the following command to initialize Terraform and download the necessary providers:
terraform init
Run the following command to see what changes Terraform will apply without actually deploying anything:
terraform plan -var-file credentials.tfvars
Review the output to ensure that Terraform will create the desired resources with the expected changes.
If everything looks good in the plan, proceed with deploying the resources:
terraform apply -var-file credentials.tfvars -auto-approve
When a user accesses the URL path /get_data with a GET request
terraform destroy -var-file credentials.tfvars -auto-approve
Thanks!!!