Skip to content

Instantly share code, notes, and snippets.

@Lucas-Barret
Last active December 17, 2023 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lucas-Barret/32f71049d1365baa70e26cbc483da975 to your computer and use it in GitHub Desktop.
Save Lucas-Barret/32f71049d1365baa70e26cbc483da975 to your computer and use it in GitHub Desktop.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main"
}
}
variable "subnets_cidr" {
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
variable "azs" {
type = list(any)
default = ["us-east-1a", "us-east-1b"]
}
resource "aws_subnet" "public" {
count = length(var.subnets_cidr)
vpc_id = aws_vpc.main.id
cidr_block = element(var.subnets_cidr, count.index)
availability_zone = element(var.azs, count.index)
map_public_ip_on_launch = true
tags = {
Name = "Subnet-${count.index + 1}"
}
}
resource "aws_route_table" "public-rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "public-rt"
}
}
resource "aws_route_table_association" "a" {
count = length(var.subnets_cidr)
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.public-rt.id
}
resource "aws_db_subnet_group" "default" {
name = "main"
subnet_ids = [aws_subnet.public[0].id, aws_subnet.public[1].id]
tags = {
Name = "My DB subnet group"
}
}
resource "aws_security_group" "allow_postgres" {
name = "allow_postgres"
description = "Allow Postgres Inbound traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "allow postgres"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow postgres"
}
}
resource "aws_db_instance" "db-test1" {
allocated_storage = 10
identifier = "postgres-test2"
db_subnet_group_name = aws_db_subnet_group.default.id
engine = "postgres"
engine_version = "14"
instance_class = "db.t3.micro"
username = "postgres"
password = "postgres"
vpc_security_group_ids = [aws_security_group.allow_tls.id]
publicly_accessible = true
skip_final_snapshot = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment