Skip to content

Instantly share code, notes, and snippets.

@afcastano
Created August 19, 2019 11:02
Show Gist options
  • Save afcastano/6c86b37e5ba71711fcc68047cf2920a2 to your computer and use it in GitHub Desktop.
Save afcastano/6c86b37e5ba71711fcc68047cf2920a2 to your computer and use it in GitHub Desktop.
AWS Public Tier
#provision public subnet 1
resource "aws_subnet" "pub_subnet_1"{
vpc_id = "${aws_vpc.app_vpc.id}"
cidr_block = "${var.aws_pub_subnet_1_cidr}"
tags {
Name = "public subnet"
}
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}
#provision public subnet 2 (Required for load balancer)
resource "aws_subnet" "pub_subnet_2"{
vpc_id = "${aws_vpc.app_vpc.id}"
cidr_block = "${var.aws_pub_subnet_2_cidr}"
tags {
Name = "public subnet 2"
}
availability_zone = "${data.aws_availability_zones.available.names[1]}"
}
resource "aws_route_table" "public-routes" {
vpc_id = "${aws_vpc.app_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.app_igw.id}"
}
}
resource "aws_route_table_association" "public-subnet-routes-1" {
subnet_id = "${aws_subnet.pub_subnet_1.id}"
route_table_id = "${aws_route_table.public-routes.id}"
}
resource "aws_route_table_association" "public-subnet-routes-2" {
subnet_id = "${aws_subnet.pub_subnet_2.id}"
route_table_id = "${aws_route_table.public-routes.id}"
}
# NAT Gateway configuration for private subnetss
resource "aws_eip" "nat-eip" {
vpc = true
depends_on = ["aws_internet_gateway.app_igw", "aws_vpc_dhcp_options_association.dns_resolver"]
}
resource "aws_nat_gateway" "nat-gw" {
allocation_id = "${aws_eip.nat-eip.id}"
subnet_id = "${aws_subnet.pub_subnet_1.id}"
depends_on = ["aws_internet_gateway.app_igw"]
}
#bastion sg
resource "aws_security_group" "bastion" {
name = "bastion-secgroup"
vpc_id = "${aws_vpc.app_vpc.id}"
# ssh access from anywhere
ingress {
from_port = 22
to_port = 2
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
#LoadBalancer sg
resource "aws_security_group" "alb" {
name = "pub-secgroup"
vpc_id = "${aws_vpc.app_vpc.id}"
# ssh access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment