Skip to content

Instantly share code, notes, and snippets.

@andypmw
Created June 19, 2020 14:28
Show Gist options
  • Save andypmw/b67bc1e66fdd1e3570a0726dc63d784b to your computer and use it in GitHub Desktop.
Save andypmw/b67bc1e66fdd1e3570a0726dc63d784b to your computer and use it in GitHub Desktop.
Dojotek Terraform Template untuk membuat Amazon VPC
/**
* Terraform template untuk membuat Amazon VPC, internet gateway, subnet, security group.
*
* Dibuat oleh Andy Primawan, Dojotek.
*/
provider "aws" {
version = "~> 2.67"
profile = "default"
region = "ap-southeast-1"
}
/**
* Buat EC2 Internet Gateway yaitu gateway virtual pengganti router.
*/
resource "aws_internet_gateway" "internetgw" {
vpc_id = "${aws_vpc.myvpc.id}"
tags = {
Name = "main"
}
}
/**
* Buat EC2 VPC yaitu jaringan komputer virtual yang terisolasi.
*/
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "dojotekvpc"
}
}
/**
* Buat entry ke Routing Table yang dipakai oleh Amazon VPC agar
* network packet dapat mengalir ke internet.
*/
resource "aws_route" "routetointernet" {
route_table_id = "${aws_vpc.myvpc.default_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.internetgw.id}"
}
/**
* Buat sebuah subnet (partisi) di Availability Zone ap-southeast-1a
*/
resource "aws_subnet" "subnet1" {
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "ap-southeast-1a"
tags = {
Name = "dojoteksubnet1"
}
}
/**
* Buat sebuah subnet (partisi) di Availability Zone ap-southeast-1b
*/
resource "aws_subnet" "subnet2" {
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "10.0.2.0/24"
availability_zone = "ap-southeast-1b"
tags = {
Name = "dojoteksubnet2"
}
}
/**
* Buat EC2 Security Group yaitu rule firewall
*/
resource "aws_security_group" "dojotekhttpserver" {
name = "dojotekhttpserver"
description = "Dojotek HTTP Server"
vpc_id = "${aws_vpc.myvpc.id}"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
/**
* Buka port SSH agar bisa diakses dari seluruh alamat IP.
*/
resource "aws_security_group_rule" "inboundssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.dojotekhttpserver.id}"
}
/**
* Buka port HTTP agar bisa diakses dari seluruh alamat IP.
*/
resource "aws_security_group_rule" "inboundhttp" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.dojotekhttpserver.id}"
}
/**
* Buka port HTTPS agar bisa diakses dari seluruh alamat IP.
*/
resource "aws_security_group_rule" "inboundhttps" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.dojotekhttpserver.id}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment