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 terraform scripts #30

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
45 changes: 45 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI/CD Pipeline

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

- name: Build Docker image
run: docker build -t notification-api .

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

- name: Build, tag, and push image to ECR
run: |
docker tag notification-api:latest <account_id>.dkr.ecr.us-west-2.amazonaws.com/notification-api:latest
docker push <account_id>.dkr.ecr.us-west-2.amazonaws.com/notification-api:latest

- name: Deploy to ECS
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ecs-task-def.json
service: notification-api-service
cluster: notification-cluster
wait-for-service-stability: true
45 changes: 45 additions & 0 deletions terraform/autoscaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
resource "aws_appautoscaling_target" "notification_api" {
max_capacity = 10
min_capacity = 1
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.notification_api.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_policy" "notification_api_cpu" {
name = "cpu-scaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.notification_api.resource_id
scalable_dimension = aws_appautoscaling_target.notification_api.scalable_dimension
service_namespace = aws_appautoscaling_target.notification_api.service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 70.0
}
}

resource "aws_appautoscaling_target" "email_sender" {
max_capacity = 10
min_capacity = 1
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.email_sender.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_policy" "email_sender_cpu" {
name = "cpu-scaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.email_sender.resource_id
scalable_dimension = aws_appautoscaling_target.email_sender.scalable_dimension
service_namespace = aws_appautoscaling_target.email_sender.service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 70.0
}
}
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" "main" {
name = "notification-cluster"
}
7 changes: 7 additions & 0 deletions terraform/ecr.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_ecr_repository" "notification_api" {
name = "notification-api"
}

resource "aws_ecr_repository" "email_sender" {
name = "email-sender"
}
33 changes: 33 additions & 0 deletions terraform/ecs-task-def.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"family": "notification-api",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "notification-api",
"image": "<account_id>.dkr.ecr.us-west-2.amazonaws.com/notification-api:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/notification-api",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::<account_id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<account_id>:role/ecsTaskExecutionRole"
}

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 = "us-west-2"
}
41 changes: 41 additions & 0 deletions terraform/service.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "aws_ecs_service" "notification_api" {
name = "notification-api"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.notification_api.arn
desired_count = 2
launch_type = "FARGATE"
network_configuration {
subnets = [aws_subnet.main.id]
security_groups = [aws_security_group.main.id]
assign_public_ip = true
}
load_balancer {
target_group_arn = aws_lb_target_group.notification_api.arn
container_name = "notification-api"
container_port = 3000
}
service_registries {
registry_arn = aws_service_discovery_service.notification_api.arn
}
}

resource "aws_ecs_service" "email_sender" {
name = "email-sender"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.email_sender.arn
desired_count = 2
launch_type = "FARGATE"
network_configuration {
subnets = [aws_subnet.main.id]
security_groups = [aws_security_group.main.id]
assign_public_ip = true
}
load_balancer {
target_group_arn = aws_lb_target_group.email_sender.arn
container_name = "email-sender"
container_port = 3000
}
service_registries {
registry_arn = aws_service_discovery_service.email_sender.arn
}
}
63 changes: 63 additions & 0 deletions terraform/task_definitions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
resource "aws_ecs_task_definition" "notification_api" {
family = "notification-api"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
name = "notification-api",
image = "${aws_ecr_repository.notification_api.repository_url}:latest",
cpu = 256,
memory = 512,
essential = true,
portMappings = [
{
containerPort = 3000,
hostPort = 3000
}
],
logConfiguration = {
logDriver = "awslogs",
options = {
"awslogs-group" = "/ecs/notification-api",
"awslogs-region" = "us-west-2",
"awslogs-stream-prefix" = "ecs"
}
}
}
])
}

resource "aws_ecs_task_definition" "email_sender" {
family = "email-sender"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
name = "email-sender",
image = "${aws_ecr_repository.email_sender.repository_url}:latest",
cpu = 256,
memory = 512,
essential = true,
portMappings = [
{
containerPort = 3000,
hostPort = 3000
}
],
logConfiguration = {
logDriver = "awslogs",
options = {
"awslogs-group" = "/ecs/email-sender",
"awslogs-region" = "us-west-2",
"awslogs-stream-prefix" = "ecs"
}
}
}
])
}