Skip to content

Instantly share code, notes, and snippets.

View mlabouardy's full-sized avatar
☁️
Subscribe to my newsletter ➡️ https://devopsbulletin.com

LABOUARDY Mohamed mlabouardy

☁️
Subscribe to my newsletter ➡️ https://devopsbulletin.com
View GitHub Profile
@mlabouardy
mlabouardy / variables.tf
Created October 27, 2017 18:58
AWS VPC Global Variables for Terraform
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"
}
@mlabouardy
mlabouardy / provider.tf
Created October 27, 2017 18:58
Define AWS as provider for terraform
# Define AWS as our provider
provider "aws" {
region = "${var.aws_region}"
}
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 18:59
Define AWS VPC with Terraform
# Define our VPC
resource "aws_vpc" "default" {
cidr_block = "${var.vpc_cidr}"
enable_dns_hostnames = true
tags {
Name = "test-vpc"
}
}
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 19:00
Define 2 Subnets in VPC
# 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"
}
}
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 19:00
Define an internet gateway
# Define the internet gateway
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.default.id}"
tags {
Name = "VPC IGW"
}
}
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 19:01
Define Public Route table to route internet traffic to IGW
# 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 {
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 19:01
Define security group for webserver
# 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"]
@mlabouardy
mlabouardy / vpc.tf
Created October 27, 2017 19:02
Define a security group for database
# 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}"]
@mlabouardy
mlabouardy / resources.tf
Created October 27, 2017 19:03
Define an SSH KeyPair
# Define SSH key pair for our instances
resource "aws_key_pair" "default" {
key_name = "vpctestkeypair"
public_key = "${file("${var.key_path}")}"
}
@mlabouardy
mlabouardy / resources.tf
Created October 27, 2017 19:03
Webserver Instance
# 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")}"