SimpleTimeService is a containerized web application that provides a simple time service. It is deployed on AWS using Terraform and ECS Fargate. The application is accessible via an Application Load Balancer (ALB) and returns the current timestamp and the visitor's IP address in JSON format.
SimpleTimeService/
├── Dockerfile # Dockerfile for building the application image
├── Readme.md # Documentation for the project
├── app/
│ ├── app.py # Flask application code
│ ├── requirements.txt # Python dependencies
│ └── wsgi.py # WSGI entry point for the application
├── terraform/
│ ├── main.tf # Terraform configuration for AWS resources
│ ├── outputs.tf # Terraform outputs
│ ├── terraform.tfvars # Terraform variables (user-specific configuration)
│ └── variables.tf # Terraform variable definitions
- Infrastructure as Code (IaC): Uses Terraform to define and provision AWS resources.
- Containerized Application: The application is packaged as a Docker container.
- AWS ECS Fargate: Runs the application on a serverless container orchestration platform.
- Load Balancer: Configured with an ALB for routing traffic to the ECS service.
- Scalable Architecture: Supports scaling and high availability with public and private subnets.
- Language: Python
- Framework: Flask
- Port: 5000
- Response: JSON object with the current timestamp and visitor's IP address.
- VPC: A Virtual Private Cloud with public and private subnets.
- Subnets: Two public and two private subnets across availability zones.
- Internet Gateway & NAT Gateway: For internet access in public and private subnets.
- ECS Cluster: Hosts the containerized application.
- IAM Role: Grants permissions for ECS task execution.
- Security Group: Configures inbound and outbound traffic rules.
- Load Balancer: Routes traffic to the ECS service.
- AWS CLI: Installed and configured with appropriate credentials.
- Terraform: Installed on your local machine.
- Docker: Installed for building the container image.
-
Clone the Repository:
git clone https://github.com/your-repo/SimpleTimeService.git cd SimpleTimeService -
Build the Docker Image:
docker build -t simpletime-service . -
Push the Docker Image to a Registry:
- Tag the image:
docker tag simpletime-service:latest <your-dockerhub-username>/simpletime-service:latest
- Push the image:
docker push <your-dockerhub-username>/simpletime-service:latest
- Tag the image:
-
Configure Terraform:
- Update
terraform/terraform.tfvarswith your AWS credentials and desired configuration.
- Update
-
Deploy Infrastructure:
cd terraform terraform init terraform apply -
Access the Service:
- Retrieve the ALB DNS name from Terraform outputs:
terraform output alb_dns_name
- Open the DNS name in your browser to access the service.
- Retrieve the ALB DNS name from Terraform outputs:
When accessing the service, you will receive a JSON response like this:
{
"timestamp": "2025-04-22T12:34:56Z",
"ip": "203.0.113.42"
}To destroy the infrastructure and avoid incurring costs:
cd terraform
terraform destroyHere is a Jenkins pipeline script (Jenkinsfile) that automates the process of building a Docker image, pushing it to Docker Hub, and deploying infrastructure using Terraform
pipeline {
agent any
environment {
DOCKER_IMAGE = "your-dockerhub-username/simpletime-service:latest"
DOCKER_CREDENTIALS_ID = "docker-hub-credentials" // Jenkins credentials ID for Docker Hub
AWS_CREDENTIALS_ID = "aws-credentials" // Jenkins credentials ID for AWS
}
stages {
stage('Checkout Code') {
steps {
// Clone the repository
git branch: 'main', url: 'https://github.com/your-repo/SimpleTimeService.git'
}
}
stage('Build Docker Image') {
steps {
script {
// Build the Docker image
sh 'docker build -t ${DOCKER_IMAGE} .'
}
}
}
stage('Push Docker Image to Docker Hub') {
steps {
script {
// Log in to Docker Hub and push the image
withDockerRegistry([credentialsId: DOCKER_CREDENTIALS_ID]) {
sh 'docker push ${DOCKER_IMAGE}'
}
}
}
}
stage('Terraform Init') {
steps {
script {
// Initialize Terraform
dir('terraform') {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: AWS_CREDENTIALS_ID]]) {
sh 'terraform init'
}
}
}
}
}
stage('Terraform Apply') {
steps {
script {
// Apply Terraform configuration
dir('terraform') {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: AWS_CREDENTIALS_ID]]) {
sh 'terraform apply -auto-approve'
}
}
}
}
}
}
post {
always {
// Clean up workspace
cleanWs()
}
}
}