Skip to content

Commit

Permalink
Added IAC for managing ECR using terraform
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Singh <[email protected]>
  • Loading branch information
SkySingh04 committed May 19, 2024
1 parent d076dde commit 97ef616
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
86 changes: 86 additions & 0 deletions terraform/ecr/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
provider "aws" {
region = var.region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}

# VPC setup
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = {
Name = "main"
}
}

resource "aws_subnet" "subnet_a" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet_cidrs[0]
map_public_ip_on_launch = true
availability_zone = var.availability_zones[0]
}

resource "aws_subnet" "subnet_b" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet_cidrs[1]
map_public_ip_on_launch = true
availability_zone = var.availability_zones[1]
}

resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.main.id
tags = {
Name = "internet_gateway"
}
}

resource "aws_route_table" "route_table" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
}
}

resource "aws_route_table_association" "subnet_a_route" {
subnet_id = aws_subnet.subnet_a.id
route_table_id = aws_route_table.route_table.id
}

resource "aws_route_table_association" "subnet_b_route" {
subnet_id = aws_subnet.subnet_b.id
route_table_id = aws_route_table.route_table.id
}

resource "aws_security_group" "security_group" {
name = "ecs-security-group"
vpc_id = aws_vpc.main.id

ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
}

# ECR repositories
resource "aws_ecr_repository" "repositories" {
for_each = toset(var.ecr_repositories)
name = each.key

image_scanning_configuration {
scan_on_push = true
}

tags = {
Name = each.key
}
}
28 changes: 28 additions & 0 deletions terraform/ecr/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "region" {
description = "The AWS region to deploy into"
type = string
default = "ap-south-1"
}

variable "aws_access_key" {
description = "AWS access key"
type = string
sensitive = true
}

variable "aws_secret_key" {
description = "AWS secret key"
type = string
sensitive = true
}

variable "ecr_repositories" {
description = "List of ECR repositories to create"
type = list(string)
default = ["haalsamachar-graphql", "haalsamachar-users", "haalsamachar-blogs", "haalsamachar-auth", "haalsamachar-comments", "haalsamachar-frontend"]
}
variable "vpc_cidr" {
description = "CIDR block for the VPC"
type = string
default = "10.0.0.0/16"
}

0 comments on commit 97ef616

Please sign in to comment.