This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "aws_region" { | |
description = "Region for the VPC" | |
default = "us-east-1" | |
} | |
variable "vpc_cidr" { | |
description = "CIDR for the VPC" | |
default = "10.0.0.0/16" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define AWS as our provider | |
provider "aws" { | |
region = "${var.aws_region}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define our VPC | |
resource "aws_vpc" "default" { | |
cidr_block = "${var.vpc_cidr}" | |
enable_dns_hostnames = true | |
tags { | |
Name = "test-vpc" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the public subnet | |
resource "aws_subnet" "public-subnet" { | |
vpc_id = "${aws_vpc.default.id}" | |
cidr_block = "${var.public_subnet_cidr}" | |
availability_zone = "us-east-1a" | |
tags { | |
Name = "Web Public Subnet" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the internet gateway | |
resource "aws_internet_gateway" "gw" { | |
vpc_id = "${aws_vpc.default.id}" | |
tags { | |
Name = "VPC IGW" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the route table | |
resource "aws_route_table" "web-public-rt" { | |
vpc_id = "${aws_vpc.default.id}" | |
route { | |
cidr_block = "0.0.0.0/0" | |
gateway_id = "${aws_internet_gateway.gw.id}" | |
} | |
tags { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the security group for public subnet | |
resource "aws_security_group" "sgweb" { | |
name = "vpc_test_web" | |
description = "Allow incoming HTTP connections & SSH access" | |
ingress { | |
from_port = 80 | |
to_port = 80 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the security group for private subnet | |
resource "aws_security_group" "sgdb"{ | |
name = "sg_test_web" | |
description = "Allow traffic from public subnet" | |
ingress { | |
from_port = 3306 | |
to_port = 3306 | |
protocol = "tcp" | |
cidr_blocks = ["${var.public_subnet_cidr}"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define SSH key pair for our instances | |
resource "aws_key_pair" "default" { | |
key_name = "vpctestkeypair" | |
public_key = "${file("${var.key_path}")}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define webserver inside the public subnet | |
resource "aws_instance" "wb" { | |
ami = "${var.ami}" | |
instance_type = "t1.micro" | |
key_name = "${aws_key_pair.default.id}" | |
subnet_id = "${aws_subnet.public-subnet.id}" | |
vpc_security_group_ids = ["${aws_security_group.sgweb.id}"] | |
associate_public_ip_address = true | |
source_dest_check = false | |
user_data = "${file("install.sh")}" |