automated terminal push

This commit is contained in:
lenape
2025-08-05 13:41:43 +00:00
parent 5602f1d7b0
commit bceec59b17
4 changed files with 1616 additions and 0 deletions

765
infrastructure/services/Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,765 @@
// Phase 2: ECS Fargate + Blue/Green Deployment Pipeline
// File: infrastructure/services/Jenkinsfile
pipeline {
agent {
label 'xochi'
}
parameters {
choice(
name: 'ACTION',
choices: ['deploy', 'destroy', 'plan'],
description: 'Action to perform'
)
choice(
name: 'ENVIRONMENT',
choices: ['dev', 'staging', 'prod'],
description: 'Environment to deploy to'
)
choice(
name: 'DEPLOYMENT_STRATEGY',
choices: ['blue_green', 'rolling', 'infrastructure_only'],
description: 'Deployment strategy'
)
string(
name: 'IMAGE_TAG',
defaultValue: 'latest',
description: 'Docker image tag to deploy'
)
booleanParam(
name: 'SKIP_TESTS',
defaultValue: false,
description: 'Skip application tests'
)
}
environment {
PROJECT_NAME = 'nvhi-atsila-microservice'
AWS_CREDENTIALS = 'aws-ci'
AWS_REGION_CREDENTIAL = 'AWS_REGION'
SONAR_PROJECT_KEY = 'nvhi-atsila-microservice-services'
DOCKER_BUILDKIT = '1'
ECR_REPO_NAME = "nvhi-atsila-app"
}
stages {
stage('🔍 Checkout & Validation') {
steps {
echo "=== Enterprise ECS Services Pipeline ==="
echo "Action: ${params.ACTION}"
echo "Environment: ${params.ENVIRONMENT}"
echo "Deployment Strategy: ${params.DEPLOYMENT_STRATEGY}"
echo "Image Tag: ${params.IMAGE_TAG}"
echo "Build: #${env.BUILD_NUMBER}"
echo "Working Directory: infrastructure/services"
deleteDir()
checkout scm
script {
// Validate repository structure
sh '''
echo "Repository structure validation:"
# Check for services infrastructure
if [ ! -d "infrastructure/services" ]; then
echo "❌ Missing infrastructure/services directory"
exit 1
fi
cd infrastructure/services
if [ ! -f "main.tf" ]; then echo "❌ Missing main.tf"; exit 1; fi
echo "✅ Found: main.tf"
if [ ! -f "variables.tf" ]; then echo "❌ Missing variables.tf"; exit 1; fi
echo "✅ Found: variables.tf"
if [ ! -f "outputs.tf" ]; then echo "❌ Missing outputs.tf"; exit 1; fi
echo "✅ Found: outputs.tf"
# Check for application files
cd ../../
if [ ! -f "app.py" ]; then echo "❌ Missing app.py"; exit 1; fi
echo "✅ Found: app.py"
if [ ! -f "Dockerfile" ]; then echo "❌ Missing Dockerfile"; exit 1; fi
echo "✅ Found: Dockerfile"
if [ ! -f "requirements.txt" ]; then echo "❌ Missing requirements.txt"; exit 1; fi
echo "✅ Found: requirements.txt"
echo "✅ Repository structure validated"
'''
}
}
}
stage('🔧 Setup Tools') {
steps {
script {
sh '''
echo "=== Tool Validation ==="
# Terraform
TF_VERSION_ACTUAL=$(terraform version | head -n1 | cut -d' ' -f2 | sed 's/^v//')
echo "✅ Terraform found: v${TF_VERSION_ACTUAL}"
# Docker
DOCKER_VERSION=$(docker --version | cut -d' ' -f3 | sed 's/,//')
echo "✅ Docker found: ${DOCKER_VERSION}"
echo "=== Tool Validation Complete ==="
'''
// Verify AWS credentials
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
sh '''
echo "AWS CLI version:"
aws --version
echo "Verifying AWS credentials..."
aws sts get-caller-identity
echo "Testing AWS permissions..."
aws ecs list-clusters --region $AWS_REGION --max-items 1 || echo "⚠️ ECS permissions check"
aws ecr describe-repositories --region $AWS_REGION --max-items 1 || echo "⚠️ ECR permissions check"
echo "✅ AWS authentication verified"
'''
}
}
}
}
stage('🔍 SonarQube Analysis') {
steps {
dir('infrastructure/services') {
script {
// Create SonarQube project properties
writeFile file: 'sonar-project.properties', text: """
sonar.projectKey=${env.SONAR_PROJECT_KEY}
sonar.projectName=nvhi-atsila-microservice Services Layer
sonar.projectVersion=1.0
sonar.sources=.
sonar.inclusions=**/*.tf,**/*.py,**/*.sh
sonar.exclusions=**/*.tfstate,**/*.tfstate.backup,**/.terraform/**,**/*.tfplan
sonar.sourceEncoding=UTF-8
sonar.terraform.provider.aws=true
"""
tool name: 'SonarScanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withSonarQubeEnv('SonarQube') {
sh '/tmp/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarScanner/bin/sonar-scanner'
}
}
}
}
}
stage('🎯 Quality Gate') {
steps {
script {
timeout(time: 5, unit: 'MINUTES') {
try {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
echo "❌ Quality Gate failed: ${qg.status}"
// Don't fail the build for demo purposes, but log the issue
echo "💡 Continuing despite Quality Gate failure for demo"
} else {
echo "✅ Quality Gate passed!"
}
} catch (Exception e) {
echo "❌ Quality Gate check failed: ${e.getMessage()}"
echo "💡 This might be due to SonarQube server issues or configuration problems"
// Manual approval as fallback
input message: 'SonarQube Quality Gate failed or unavailable. Continue anyway?', ok: 'Continue'
}
}
}
}
}
stage('🧪 Application Tests') {
when {
not { params.SKIP_TESTS }
}
steps {
script {
sh '''
echo "=== Application Testing ==="
# Install Python dependencies
python3 -m pip install --user -r requirements.txt
python3 -m pip install --user pytest flask
# Basic syntax check
python3 -m py_compile app.py
echo "✅ Python syntax check passed"
# Test Flask app imports and basic functionality
python3 -c "
import sys
sys.path.append('.')
from app import app
print('✅ Flask app import successful')
# Test app creation
with app.test_client() as client:
response = client.get('/health')
assert response.status_code == 200
print('✅ Health endpoint test passed')
print(f'Health response: {response.get_json()}')
"
echo "✅ Application tests completed"
'''
}
}
}
stage('🐳 Build & Push Docker Image') {
when {
anyOf {
equals expected: 'deploy', actual: params.ACTION
allOf {
equals expected: 'plan', actual: params.ACTION
anyOf {
equals expected: 'blue_green', actual: params.DEPLOYMENT_STRATEGY
equals expected: 'rolling', actual: params.DEPLOYMENT_STRATEGY
}
}
}
}
steps {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Docker Build & Push ==="
# Get AWS account ID
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
ECR_REGISTRY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
ECR_REPO_URI="${ECR_REGISTRY}/${ECR_REPO_NAME}"
echo "ECR Registry: ${ECR_REGISTRY}"
echo "ECR Repository: ${ECR_REPO_URI}"
echo "Image Tag: ${IMAGE_TAG}"
# Login to ECR
echo "🔐 Logging into ECR..."
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
# Check if ECR repository exists, create if not
echo "📦 Checking ECR repository..."
if ! aws ecr describe-repositories --repository-names $ECR_REPO_NAME --region $AWS_REGION 2>/dev/null; then
echo "🏗️ Creating ECR repository..."
aws ecr create-repository --repository-name $ECR_REPO_NAME --region $AWS_REGION
fi
# Build Docker image
echo "🔨 Building Docker image..."
docker build -t $ECR_REPO_NAME:${IMAGE_TAG} .
docker build -t $ECR_REPO_NAME:build-${BUILD_NUMBER} .
# Tag for ECR
docker tag $ECR_REPO_NAME:${IMAGE_TAG} $ECR_REPO_URI:${IMAGE_TAG}
docker tag $ECR_REPO_NAME:${IMAGE_TAG} $ECR_REPO_URI:build-${BUILD_NUMBER}
# Push to ECR
echo "🚀 Pushing to ECR..."
docker push $ECR_REPO_URI:${IMAGE_TAG}
docker push $ECR_REPO_URI:build-${BUILD_NUMBER}
# Clean up local images
docker rmi $ECR_REPO_NAME:${IMAGE_TAG} || true
docker rmi $ECR_REPO_NAME:build-${BUILD_NUMBER} || true
docker rmi $ECR_REPO_URI:${IMAGE_TAG} || true
docker rmi $ECR_REPO_URI:build-${BUILD_NUMBER} || true
echo "✅ Docker build and push completed"
echo "📍 Image available at: $ECR_REPO_URI:${IMAGE_TAG}"
'''
}
}
}
}
stage('🚀 Infrastructure Bootstrap') {
when {
not { equals expected: 'destroy', actual: params.ACTION }
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Services Layer Bootstrap ==="
# Generate backend configuration for services layer
cat > backend.tf << EOF
terraform {
backend "s3" {
bucket = "nvhi-atsila-microservice-terraform-state-c4ae0f80"
key = "services/terraform.tfstate"
region = "us-east-2"
dynamodb_table = "nvhi-atsila-microservice-terraform-locks"
encrypt = true
}
}
EOF
echo "✅ Backend configuration generated"
echo "Generated backend.tf:"
cat backend.tf
'''
}
}
}
}
}
stage('🔄 Terraform Init & Validate') {
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Terraform Initialization ==="
# Create terraform.tfvars
cat > terraform.tfvars << EOF
# Generated by Jenkins Pipeline Build #${BUILD_NUMBER}
project_name = "${PROJECT_NAME}"
environment = "${ENVIRONMENT}"
aws_region = "$AWS_REGION"
image_tag = "${IMAGE_TAG}"
# Free tier optimized settings
task_cpu = "256"
task_memory = "512"
desired_count = 1
enable_auto_scaling = false
# Jenkins-managed tags
common_tags = {
Terraform = "true"
Project = "${PROJECT_NAME}"
Environment = "${ENVIRONMENT}"
ManagedBy = "jenkins"
Pipeline = "services-layer"
BuildNumber = "${BUILD_NUMBER}"
GitCommit = "${GIT_COMMIT}"
ImageTag = "${IMAGE_TAG}"
}
EOF
echo "Current terraform.tfvars:"
cat terraform.tfvars
# Initialize Terraform
terraform init -upgrade
terraform validate
# Format check
if ! terraform fmt -check=true; then
echo "⚠️ Terraform files need formatting"
terraform fmt -diff=true
fi
echo "✅ Terraform initialized and validated"
'''
}
}
}
}
}
stage('📊 Terraform Plan') {
when {
not { equals expected: 'destroy', actual: params.ACTION }
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Terraform Plan ==="
terraform plan \
-var="project_name=${PROJECT_NAME}" \
-var="environment=${ENVIRONMENT}" \
-var="aws_region=$AWS_REGION" \
-var="image_tag=${IMAGE_TAG}" \
-out=tfplan \
-detailed-exitcode
PLAN_EXIT_CODE=$?
if [ $PLAN_EXIT_CODE -eq 2 ]; then
echo "📝 Changes detected - plan saved to tfplan"
elif [ $PLAN_EXIT_CODE -eq 0 ]; then
echo "📋 No changes detected"
else
echo "❌ Plan failed"
exit 1
fi
echo "=== Plan Summary ==="
terraform show -no-color tfplan | grep -E "(Plan:|No changes|Error:)" || true
'''
// Archive the plan
archiveArtifacts artifacts: 'tfplan', allowEmptyArchive: true
}
}
}
}
}
stage('🚦 Deployment Approval') {
when {
equals expected: 'deploy', actual: params.ACTION
}
steps {
script {
dir('infrastructure/services') {
def planSummary = sh(
script: 'terraform show -no-color tfplan | grep "Plan:" || echo "No changes"',
returnStdout: true
).trim()
echo "=== Manual Approval Required ==="
echo "Environment: ${params.ENVIRONMENT}"
echo "Deployment Strategy: ${params.DEPLOYMENT_STRATEGY}"
echo "Image Tag: ${params.IMAGE_TAG}"
echo "Plan Summary: ${planSummary}"
def approver = input(
message: "Deploy services to ${params.ENVIRONMENT}?",
ok: 'Deploy',
submitterParameter: 'APPROVER'
)
echo "✅ Deployment approved by: ${approver}"
env.APPROVED_BY = approver
}
}
}
}
stage('🚀 Terraform Apply') {
when {
equals expected: 'deploy', actual: params.ACTION
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Terraform Apply ==="
echo "✅ Approved by: ${APPROVED_BY}"
terraform apply -auto-approve tfplan
echo "=== Deployment Outputs ==="
terraform output
'''
// Archive outputs
sh 'terraform output -json > terraform-outputs.json'
archiveArtifacts artifacts: 'terraform-outputs.json', allowEmptyArchive: true
}
}
}
}
}
stage('🔄 Blue/Green Deployment') {
when {
allOf {
equals expected: 'deploy', actual: params.ACTION
equals expected: 'blue_green', actual: params.DEPLOYMENT_STRATEGY
}
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Blue/Green Deployment ==="
# Get cluster and service info
CLUSTER_NAME=$(terraform output -raw ecs_cluster_name)
SERVICE_NAME=$(terraform output -raw ecs_service_name)
APP_URL=$(terraform output -raw application_url)
HEALTH_URL=$(terraform output -raw health_check_url)
echo "🔄 Terraform has updated ECS service with new container image"
echo "📋 Deployment Details:"
echo " • Cluster: $CLUSTER_NAME"
echo " • Service: $SERVICE_NAME"
echo " • Image Tag: ${IMAGE_TAG}"
echo "⏳ Waiting for ECS service to stabilize..."
aws ecs wait services-stable \
--cluster $CLUSTER_NAME \
--services $SERVICE_NAME \
--region $AWS_REGION
echo "✅ ECS service deployment completed successfully"
# Validate deployment health
echo "🔍 Validating deployment health..."
SERVICE_STATUS=$(aws ecs describe-services \
--cluster $CLUSTER_NAME \
--services $SERVICE_NAME \
--query 'services[0].status' \
--output text)
RUNNING_COUNT=$(aws ecs describe-services \
--cluster $CLUSTER_NAME \
--services $SERVICE_NAME \
--query 'services[0].runningCount' \
--output text)
DESIRED_COUNT=$(aws ecs describe-services \
--cluster $CLUSTER_NAME \
--services $SERVICE_NAME \
--query 'services[0].desiredCount' \
--output text)
echo "📊 Service Health Check:"
echo " • Status: $SERVICE_STATUS"
echo " • Running Tasks: $RUNNING_COUNT/$DESIRED_COUNT"
if [ "$SERVICE_STATUS" = "ACTIVE" ] && [ "$RUNNING_COUNT" -eq "$DESIRED_COUNT" ]; then
echo "✅ All health checks passed"
else
echo "❌ Health check failed - service not fully ready"
exit 1
fi
echo "🎉 Blue/Green deployment completed successfully!"
echo "🌐 Application URL: $APP_URL"
echo "💚 Health Endpoint: $HEALTH_URL"
echo ""
echo "🏢 Enterprise Pattern: Infrastructure as Code managed the entire deployment lifecycle"
'''
}
}
}
}
}
stage('💥 Terraform Destroy') {
when {
equals expected: 'destroy', actual: params.ACTION
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
def approver = input(
message: "⚠️ DESTROY all services infrastructure?",
ok: 'Destroy',
submitterParameter: 'DESTROYER'
)
sh '''
echo "=== Terraform Destroy ==="
echo "🔥 Approved by: ''' + approver + '''"
# Initialize if needed
terraform init
# Destroy
terraform destroy -auto-approve \
-var="project_name=${PROJECT_NAME}" \
-var="environment=${ENVIRONMENT}" \
-var="aws_region=$AWS_REGION"
echo "💥 Infrastructure destroyed"
'''
}
}
}
}
}
stage('📈 Post-Deployment Validation') {
when {
equals expected: 'deploy', actual: params.ACTION
}
steps {
dir('infrastructure/services') {
withCredentials([
string(credentialsId: env.AWS_CREDENTIALS, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws-secret', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: env.AWS_REGION_CREDENTIAL, variable: 'AWS_REGION')
]) {
script {
sh '''
echo "=== Post-Deployment Validation ==="
# Get deployment info
APP_URL=$(terraform output -raw application_url)
HEALTH_URL=$(terraform output -raw health_check_url)
CLUSTER_NAME=$(terraform output -raw ecs_cluster_name)
SERVICE_NAME=$(terraform output -raw ecs_service_name)
echo "🔍 Validating ECS service..."
SERVICE_STATUS=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].status' --output text)
RUNNING_COUNT=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].runningCount' --output text)
DESIRED_COUNT=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].desiredCount' --output text)
echo "Service Status: $SERVICE_STATUS"
echo "Running Tasks: $RUNNING_COUNT/$DESIRED_COUNT"
if [ "$SERVICE_STATUS" = "ACTIVE" ] && [ "$RUNNING_COUNT" = "$DESIRED_COUNT" ]; then
echo "✅ ECS service is healthy"
else
echo "⚠️ ECS service health check failed"
fi
echo "🔍 Testing application endpoints..."
# Wait for ALB to be ready
echo "⏳ Waiting for ALB to be ready (60 seconds)..."
sleep 60
# Test health endpoint
echo "Testing health endpoint: $HEALTH_URL"
if curl -f -s "$HEALTH_URL"; then
echo "✅ Health endpoint is responding"
else
echo "⚠️ Health endpoint check failed (this may be normal during initial deployment)"
fi
echo "=== Deployment Summary ==="
echo "🌐 Application URL: $APP_URL"
echo "💚 Health Check: $HEALTH_URL"
echo "🐳 Image Tag: ${IMAGE_TAG}"
echo "🏗️ Build: #${BUILD_NUMBER}"
echo "👤 Approved by: ${APPROVED_BY}"
'''
}
}
}
}
}
}
post {
always {
script {
echo "=== Pipeline Execution Summary ==="
echo "🔹 Build: #${env.BUILD_NUMBER}"
echo "🔹 Action: ${params.ACTION}"
echo "🔹 Environment: ${params.ENVIRONMENT}"
echo "🔹 Deployment Strategy: ${params.DEPLOYMENT_STRATEGY}"
echo "🔹 Image Tag: ${params.IMAGE_TAG}"
echo "🔹 Duration: ${currentBuild.durationString}"
echo "🔹 Result: ${currentBuild.currentResult}"
// Archive terraform files
dir('infrastructure/services') {
archiveArtifacts artifacts: '*.tf,*.tfvars,terraform-outputs.json', allowEmptyArchive: true
}
}
}
success {
script {
if (params.ACTION == 'deploy') {
echo """
✅ ECS Services Pipeline Completed Successfully!
🎉 Phase 2: Services Deployment Complete!
📊 Deployment Details:
- Environment: ${params.ENVIRONMENT}
- Deployment Strategy: ${params.DEPLOYMENT_STRATEGY}
- Image Tag: ${params.IMAGE_TAG}
- Build: #${env.BUILD_NUMBER}
- Duration: ${currentBuild.durationString}"""
if (env.APPROVED_BY) {
echo "- Approved by: ${env.APPROVED_BY}"
}
echo """
🏗️ Infrastructure Created:
• ECS Fargate cluster with auto-scaling
• Application Load Balancer with health checks
• Blue/Green target groups for zero-downtime deployment
• ECR repository with image scanning
• CloudWatch monitoring and alarms
• IAM roles with least-privilege access
💰 Estimated Cost: ~$18-37/month
🚀 What's Working:
• Containerized Flask microservice
• Automatic health checks
• Blue/Green deployment capability
• CloudWatch monitoring
• Container insights
🌐 Access Your Application:
Check the archived terraform-outputs.json for URLs
📋 Next Steps:
• Phase 3: Advanced monitoring and alerting
• Phase 4: CI/CD automation improvements
• Consider setting up custom domain and SSL
📊 Monitoring: Check CloudWatch for metrics and logs
"""
}
}
}
failure {
echo "❌ Pipeline failed. Check logs for details."
}
cleanup {
// Clean up any temporary files
dir('infrastructure/services') {
sh 'rm -f tfplan terraform-outputs.json new-task-def.json || true'
}
}
}
}

View File

@@ -0,0 +1,486 @@
# Phase 2: ECS Fargate + Application Load Balancer
# File: infrastructure/services/main.tf
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
# This will be configured by bootstrap script
# bucket = "nvhi-atsila-microservice-terraform-state-c4ae0f80"
# key = "services/terraform.tfstate"
# region = "us-east-2"
# dynamodb_table = "nvhi-atsila-microservice-terraform-locks"
# encrypt = true
}
}
provider "aws" {
region = var.aws_region
default_tags {
tags = var.common_tags
}
}
# Data sources to reference Foundation Layer outputs
data "terraform_remote_state" "foundation" {
backend = "s3"
config = {
bucket = "nvhi-atsila-microservice-terraform-state-c4ae0f80"
key = "foundation/terraform.tfstate"
region = var.aws_region
}
}
# Enterprise naming convention with AWS resource limits
locals {
# Shortened base name for AWS resource naming limits
base_name = "nvhi-atsila"
# Computed names ensuring AWS limits compliance
# Target Groups (32 char limit): "nvhi-atsila-blue-tg" = 17 chars ✅
blue_tg_name = "${local.base_name}-blue-tg"
green_tg_name = "${local.base_name}-green-tg"
# ALB name (32 char limit): "nvhi-atsila-alb" = 15 chars ✅
alb_name = "${local.base_name}-alb"
# ECS names for consistency
ecs_cluster_name = "${local.base_name}-cluster"
ecs_service_name = "${local.base_name}-service"
# ECR name
ecr_repo_name = "${local.base_name}-app"
# Common resource prefix for tagging and organization
resource_prefix = local.base_name
}
# ECR Repository for container images
resource "aws_ecr_repository" "app" {
name = local.ecr_repo_name
image_tag_mutability = "MUTABLE"
force_delete = true # For demo purposes
image_scanning_configuration {
scan_on_push = true
}
tags = {
Name = "${var.project_name}-ecr"
Environment = var.environment
Project = var.project_name
}
}
# ECR Lifecycle Policy
resource "aws_ecr_lifecycle_policy" "app" {
repository = aws_ecr_repository.app.name
policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep last 10 images"
selection = {
tagStatus = "tagged"
tagPrefixList = ["v"]
countType = "imageCountMoreThan"
countNumber = 10
}
action = {
type = "expire"
}
},
{
rulePriority = 2
description = "Delete untagged images older than 1 day"
selection = {
tagStatus = "untagged"
countType = "sinceImagePushed"
countUnit = "days"
countNumber = 1
}
action = {
type = "expire"
}
}
]
})
}
# ECS Cluster
resource "aws_ecs_cluster" "main" {
name = local.ecs_cluster_name
configuration {
execute_command_configuration {
logging = "OVERRIDE"
log_configuration {
cloud_watch_log_group_name = aws_cloudwatch_log_group.ecs_cluster.name
}
}
}
setting {
name = "containerInsights"
value = "enabled"
}
tags = {
Name = "${var.project_name}-ecs-cluster"
Environment = var.environment
Project = var.project_name
}
}
# CloudWatch Log Group for ECS
resource "aws_cloudwatch_log_group" "ecs_cluster" {
name = "/aws/ecs/${local.ecs_cluster_name}"
retention_in_days = 7 # Free tier friendly
skip_destroy = false
tags = {
Name = "${local.ecs_cluster_name}-logs"
Environment = var.environment
Project = var.project_name
}
}
resource "aws_cloudwatch_log_group" "app" {
name = "/aws/ecs/${local.ecr_repo_name}"
retention_in_days = 7 # Free tier friendly
skip_destroy = false
tags = {
Name = "${local.ecr_repo_name}-logs"
Environment = var.environment
Project = var.project_name
}
}
# Application Load Balancer
resource "aws_lb" "main" {
name = local.alb_name
internal = false
load_balancer_type = "application"
security_groups = [data.terraform_remote_state.foundation.outputs.alb_security_group_id]
subnets = data.terraform_remote_state.foundation.outputs.public_subnet_ids
enable_deletion_protection = false # For demo purposes
tags = {
Name = "${var.project_name}-alb"
Environment = var.environment
Project = var.project_name
}
}
# ALB Target Group for Blue Environment
resource "aws_lb_target_group" "blue" {
name = local.blue_tg_name
port = 8080
protocol = "HTTP"
vpc_id = data.terraform_remote_state.foundation.outputs.vpc_id
target_type = "ip"
health_check {
enabled = true
healthy_threshold = 2
interval = 30
matcher = "200"
path = "/health"
port = "traffic-port"
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 3
}
deregistration_delay = 30 # Fast for demo
tags = {
Name = "${var.project_name}-blue-tg"
Environment = var.environment
Project = var.project_name
}
}
# ALB Target Group for Green Environment (Blue/Green Deployment)
resource "aws_lb_target_group" "green" {
name = local.green_tg_name
port = 8080
protocol = "HTTP"
vpc_id = data.terraform_remote_state.foundation.outputs.vpc_id
target_type = "ip"
health_check {
enabled = true
healthy_threshold = 2
interval = 30
matcher = "200"
path = "/health"
port = "traffic-port"
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 3
}
deregistration_delay = 30 # Fast for demo
tags = {
Name = "${var.project_name}-green-tg"
Environment = var.environment
Project = var.project_name
}
}
# ALB Listener (defaults to Blue)
resource "aws_lb_listener" "main" {
load_balancer_arn = aws_lb.main.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
forward {
target_group {
arn = aws_lb_target_group.blue.arn
}
}
}
tags = {
Name = "${var.project_name}-alb-listener"
Environment = var.environment
Project = var.project_name
}
}
# IAM Role for ECS Task Execution
resource "aws_iam_role" "ecs_task_execution" {
name = "${var.project_name}-ecs-task-execution-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})
tags = {
Name = "${var.project_name}-ecs-task-execution-role"
Environment = var.environment
Project = var.project_name
}
}
# IAM Role Policy Attachment for ECS Task Execution
resource "aws_iam_role_policy_attachment" "ecs_task_execution" {
role = aws_iam_role.ecs_task_execution.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# Additional IAM Policy for ECR and CloudWatch
resource "aws_iam_role_policy" "ecs_task_execution_custom" {
name = "${var.project_name}-ecs-task-execution-custom"
role = aws_iam_role.ecs_task_execution.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "${aws_cloudwatch_log_group.app.arn}:*"
}
]
})
}
# IAM Role for ECS Task (application permissions)
resource "aws_iam_role" "ecs_task" {
name = "${var.project_name}-ecs-task-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})
tags = {
Name = "${var.project_name}-ecs-task-role"
Environment = var.environment
Project = var.project_name
}
}
# ECS Task Definition
resource "aws_ecs_task_definition" "app" {
family = local.ecr_repo_name
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_cpu
memory = var.task_memory
execution_role_arn = aws_iam_role.ecs_task_execution.arn
task_role_arn = aws_iam_role.ecs_task.arn
container_definitions = jsonencode([
{
name = "app"
image = "${aws_ecr_repository.app.repository_url}:${var.image_tag}"
essential = true
portMappings = [
{
containerPort = 8080
protocol = "tcp"
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.app.name
awslogs-region = var.aws_region
awslogs-stream-prefix = "ecs"
}
}
environment = [
{
name = "ENV"
value = var.environment
},
{
name = "PROJECT_NAME"
value = var.project_name
}
]
}
])
tags = {
Name = "${var.project_name}-task-definition"
Environment = var.environment
Project = var.project_name
}
}
# ECS Service (Blue Environment)
resource "aws_ecs_service" "app" {
name = local.ecs_service_name
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.desired_count
launch_type = "FARGATE"
network_configuration {
subnets = data.terraform_remote_state.foundation.outputs.app_subnet_ids
security_groups = [data.terraform_remote_state.foundation.outputs.ecs_tasks_security_group_id]
assign_public_ip = true # Required for public subnets without NAT gateway
}
load_balancer {
target_group_arn = aws_lb_target_group.blue.arn
container_name = "app"
container_port = 8080
}
depends_on = [aws_lb_listener.main]
# Blue/Green deployment configuration
deployment_configuration {
deployment_circuit_breaker {
enable = true
rollback = true
}
maximum_percent = 200
minimum_healthy_percent = 50
}
enable_execute_command = true
tags = {
Name = "${var.project_name}-ecs-service"
Environment = var.environment
Project = var.project_name
}
}
# CloudWatch Alarms for monitoring
resource "aws_cloudwatch_metric_alarm" "high_cpu" {
alarm_name = "${var.project_name}-high-cpu"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "300"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ECS service CPU utilization"
alarm_actions = [] # Add SNS topic ARN for notifications
dimensions = {
ServiceName = aws_ecs_service.app.name
ClusterName = aws_ecs_cluster.main.name
}
tags = {
Name = "${var.project_name}-high-cpu-alarm"
Environment = var.environment
Project = var.project_name
}
}
resource "aws_cloudwatch_metric_alarm" "high_memory" {
alarm_name = "${var.project_name}-high-memory"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "2"
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
period = "300"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ECS service memory utilization"
alarm_actions = [] # Add SNS topic ARN for notifications
dimensions = {
ServiceName = aws_ecs_service.app.name
ClusterName = aws_ecs_cluster.main.name
}
tags = {
Name = "${var.project_name}-high-memory-alarm"
Environment = var.environment
Project = var.project_name
}
}

View File

@@ -0,0 +1,195 @@
# Phase 2: Outputs
# File: infrastructure/services/outputs.tf
# Application Load Balancer Information
output "alb_arn" {
description = "ARN of the Application Load Balancer"
value = aws_lb.main.arn
}
output "alb_dns_name" {
description = "DNS name of the Application Load Balancer"
value = aws_lb.main.dns_name
}
output "alb_zone_id" {
description = "Zone ID of the Application Load Balancer"
value = aws_lb.main.zone_id
}
output "application_url" {
description = "URL to access the application"
value = "http://${aws_lb.main.dns_name}"
}
output "health_check_url" {
description = "URL for health check endpoint"
value = "http://${aws_lb.main.dns_name}/health"
}
# ECS Information
output "ecs_cluster_name" {
description = "Name of the ECS cluster"
value = aws_ecs_cluster.main.name
}
output "ecs_cluster_arn" {
description = "ARN of the ECS cluster"
value = aws_ecs_cluster.main.arn
}
output "ecs_service_name" {
description = "Name of the ECS service"
value = aws_ecs_service.app.name
}
output "ecs_service_arn" {
description = "ARN of the ECS service"
value = aws_ecs_service.app.id
}
output "ecs_task_definition_arn" {
description = "ARN of the ECS task definition"
value = aws_ecs_task_definition.app.arn
}
output "ecs_task_definition_family" {
description = "Family of the ECS task definition"
value = aws_ecs_task_definition.app.family
}
output "ecs_task_definition_revision" {
description = "Revision of the ECS task definition"
value = aws_ecs_task_definition.app.revision
}
# ECR Information
output "ecr_repository_url" {
description = "URL of the ECR repository"
value = aws_ecr_repository.app.repository_url
}
output "ecr_repository_name" {
description = "Name of the ECR repository"
value = aws_ecr_repository.app.name
}
output "ecr_repository_arn" {
description = "ARN of the ECR repository"
value = aws_ecr_repository.app.arn
}
# Target Groups for Blue/Green Deployment
output "blue_target_group_arn" {
description = "ARN of the Blue target group"
value = aws_lb_target_group.blue.arn
}
output "green_target_group_arn" {
description = "ARN of the Green target group"
value = aws_lb_target_group.green.arn
}
output "blue_target_group_name" {
description = "Name of the Blue target group"
value = aws_lb_target_group.blue.name
}
output "green_target_group_name" {
description = "Name of the Green target group"
value = aws_lb_target_group.green.name
}
# IAM Role Information
output "ecs_task_execution_role_arn" {
description = "ARN of the ECS task execution role"
value = aws_iam_role.ecs_task_execution.arn
}
output "ecs_task_role_arn" {
description = "ARN of the ECS task role"
value = aws_iam_role.ecs_task.arn
}
# CloudWatch Information
output "cloudwatch_log_group_name" {
description = "Name of the CloudWatch log group"
value = aws_cloudwatch_log_group.app.name
}
output "cloudwatch_log_group_arn" {
description = "ARN of the CloudWatch log group"
value = aws_cloudwatch_log_group.app.arn
}
# Monitoring Information
output "cpu_alarm_name" {
description = "Name of the CPU utilization alarm"
value = aws_cloudwatch_metric_alarm.high_cpu.alarm_name
}
output "memory_alarm_name" {
description = "Name of the memory utilization alarm"
value = aws_cloudwatch_metric_alarm.high_memory.alarm_name
}
# Docker Build Information (for CI/CD)
output "docker_build_commands" {
description = "Commands to build and push Docker image"
value = {
login = "aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${aws_ecr_repository.app.repository_url}"
build = "docker build -t ${aws_ecr_repository.app.name} ."
tag = "docker tag ${aws_ecr_repository.app.name}:latest ${aws_ecr_repository.app.repository_url}:latest"
push = "docker push ${aws_ecr_repository.app.repository_url}:latest"
}
}
# ECS Deployment Commands (for CI/CD)
output "ecs_deployment_commands" {
description = "Commands for ECS deployment"
value = {
update_service = "aws ecs update-service --cluster ${aws_ecs_cluster.main.name} --service ${aws_ecs_service.app.name} --force-new-deployment"
wait_stable = "aws ecs wait services-stable --cluster ${aws_ecs_cluster.main.name} --services ${aws_ecs_service.app.name}"
describe = "aws ecs describe-services --cluster ${aws_ecs_cluster.main.name} --services ${aws_ecs_service.app.name}"
}
}
# Blue/Green Deployment Commands
output "blue_green_commands" {
description = "Commands for Blue/Green deployment"
value = {
switch_to_green = "aws elbv2 modify-listener --listener-arn ${aws_lb_listener.main.arn} --default-actions Type=forward,TargetGroupArn=${aws_lb_target_group.green.arn}"
switch_to_blue = "aws elbv2 modify-listener --listener-arn ${aws_lb_listener.main.arn} --default-actions Type=forward,TargetGroupArn=${aws_lb_target_group.blue.arn}"
check_health_green = "aws elbv2 describe-target-health --target-group-arn ${aws_lb_target_group.green.arn}"
check_health_blue = "aws elbv2 describe-target-health --target-group-arn ${aws_lb_target_group.blue.arn}"
}
}
# Project Information
output "project_name" {
description = "Name of the project"
value = var.project_name
}
output "environment" {
description = "Environment name"
value = var.environment
}
output "aws_region" {
description = "AWS region"
value = var.aws_region
}
# Cost Optimization Information
output "cost_estimate" {
description = "Estimated monthly cost breakdown"
value = {
ecs_fargate = "$0-15/month (depends on usage)"
alb = "$18/month (fixed cost)"
ecr = "$0-1/month (500MB free)"
cloudwatch = "$0-3/month (free tier)"
total_estimate = "$18-37/month"
note = "Actual costs depend on traffic and usage patterns"
}
}

View File

@@ -0,0 +1,170 @@
# Phase 2: Variables
# File: infrastructure/services/variables.tf
variable "project_name" {
description = "Name of the project"
type = string
default = "nvhi-atsila-microservice"
}
variable "environment" {
description = "Environment name (e.g., dev, staging, prod)"
type = string
default = "dev"
}
variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-2"
}
variable "common_tags" {
description = "Common tags to be applied to all resources"
type = map(string)
default = {
Terraform = "true"
Project = "nvhi-atsila-microservice"
Environment = "dev"
ManagedBy = "jenkins"
Pipeline = "services-layer"
}
}
# ECS Configuration
variable "task_cpu" {
description = "CPU units for the ECS task (256, 512, 1024, 2048, 4096)"
type = string
default = "256" # Free tier friendly
}
variable "task_memory" {
description = "Memory for the ECS task (512, 1024, 2048, 4096, 8192)"
type = string
default = "512" # Free tier friendly
}
variable "desired_count" {
description = "Desired number of ECS tasks"
type = number
default = 1 # Start with 1 for cost efficiency
}
variable "image_tag" {
description = "Docker image tag to deploy"
type = string
default = "latest"
}
# Auto Scaling Configuration
variable "enable_auto_scaling" {
description = "Enable auto scaling for the ECS service"
type = bool
default = false # Disable for free tier
}
variable "min_capacity" {
description = "Minimum number of tasks"
type = number
default = 1
}
variable "max_capacity" {
description = "Maximum number of tasks"
type = number
default = 3
}
# Health Check Configuration
variable "health_check_path" {
description = "Health check path for the application"
type = string
default = "/health"
}
variable "health_check_interval" {
description = "Health check interval in seconds"
type = number
default = 30
}
variable "health_check_timeout" {
description = "Health check timeout in seconds"
type = number
default = 5
}
variable "health_check_healthy_threshold" {
description = "Number of consecutive successful health checks"
type = number
default = 2
}
variable "health_check_unhealthy_threshold" {
description = "Number of consecutive failed health checks"
type = number
default = 3
}
# Load Balancer Configuration
variable "enable_https" {
description = "Enable HTTPS listener on ALB"
type = bool
default = false # Keep simple for demo
}
variable "ssl_certificate_arn" {
description = "ARN of SSL certificate for HTTPS"
type = string
default = null
}
# Blue/Green Deployment Configuration
variable "deployment_type" {
description = "Deployment type: blue_green or rolling"
type = string
default = "blue_green"
validation {
condition = contains(["blue_green", "rolling"], var.deployment_type)
error_message = "Deployment type must be either 'blue_green' or 'rolling'."
}
}
variable "deployment_maximum_percent" {
description = "Maximum percentage of tasks during deployment"
type = number
default = 200
}
variable "deployment_minimum_healthy_percent" {
description = "Minimum percentage of healthy tasks during deployment"
type = number
default = 50
}
# CloudWatch Configuration
variable "log_retention_days" {
description = "CloudWatch log retention in days"
type = number
default = 7 # Free tier friendly
}
variable "enable_container_insights" {
description = "Enable CloudWatch Container Insights"
type = bool
default = true
}
# Security Configuration
variable "enable_execute_command" {
description = "Enable ECS Exec for debugging"
type = bool
default = true
}
variable "enable_service_connect" {
description = "Enable ECS Service Connect"
type = bool
default = false # Keep simple for demo
}