Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Devops assessment code #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Push the Docker image to AWS ECR Repo & ECR Service
on:
push:
branches:
- chirag-devops-assessment

jobs:
Build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Get commit hash
id: get-commit-hash
run: echo "::set-output name=commit-hash::$(git rev-parse --short HEAD)"

- name: Get timestamp
id: get-timestamp
run: echo "::set-output name=timestamp::$(date +'%Y-%m-%d-%H-%M')"

- name: Build, tag, and push the image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.REPO_NAME }}
IMAGE_TAG: ${{ steps.get-commit-hash.outputs.commit-hash }}-${{ steps.get-timestamp.outputs.timestamp }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

- name: Setup Terraform with specified version on the runner
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.7.4

- name: Terraform Apply
id: init
run: |
cd terraform/
terraform init
terraform validate
terraform plan
terraform apply -auto-approve -var image_tag=${{ steps.get-commit-hash.outputs.commit-hash }}-${{ steps.get-timestamp.outputs.timestamp }}

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ testem.log
.DS_Store
Thumbs.db

.nx/cache
.nx/cache

.terraform
.terraform.lock.hcl
terraform.tfstate
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:21.7.3-alpine3.20

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the application
CMD ["npx", "nx", "serve", "pt-notification-service"]
27 changes: 27 additions & 0 deletions terraform/autoscaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "aws_appautoscaling_target" "ecs_target" {
max_capacity = 2
min_capacity = 1
resource_id = "service/${aws_ecs_cluster.devops_assessment_cluster.name}/${aws_ecs_service.devops_assessment_ecs_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}


resource "aws_appautoscaling_policy" "ecs_policy" {
name = "scale-down"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"

step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
3 changes: 3 additions & 0 deletions terraform/cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_ecs_cluster" "devops_assessment_cluster" {
name = "devopsAssessmentCluster"
}
3 changes: 3 additions & 0 deletions terraform/ecr.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_ecr_repository" "devops_assessment_repository" {
name = "devops-assessment-repository"
}
7 changes: 7 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
backend "s3" {
bucket = "devops-assessment-chirag-terraform"
key = "terraform.tfstate"
region = "ap-south-1"
}
}
3 changes: 3 additions & 0 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "ap-south-1"
}
Empty file removed terraform/secrets-manager.tf
Empty file.
33 changes: 33 additions & 0 deletions terraform/service.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "aws_ecs_task_definition" "devops_assessment_etd" {
family = "devopsAssessmentTd"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = "arn:aws:iam::279660579228:role/ecsTaskExecutionRole"

container_definitions = jsonencode([{
name = "devops-assessment"
image = "${aws_ecr_repository.devops_assessment_repository.repository_url}:${var.image_tag}"
essential = true
portMappings = [{
containerPort = 3000
hostPort = 3000
}]
}])
}

resource "aws_ecs_service" "devops_assessment_ecs_service" {
task_definition = aws_ecs_task_definition.devops_assessment_etd.id
cluster = aws_ecs_cluster.devops_assessment_cluster.id
launch_type = "FARGATE"
name = "devopsAssessmentEcsService"
desired_count = 1
network_configuration {
subnets = aws_subnet.devops_assessment_subnet[*].id
security_groups = [
aws_security_group.sg.id
]
assign_public_ip = true
}
}
Loading