Skip to content

Instantly share code, notes, and snippets.

@ksoda
Last active November 28, 2018 12:24
Show Gist options
  • Save ksoda/1df2ecd5248602197e2ac5b7d0338b1b to your computer and use it in GitHub Desktop.
Save ksoda/1df2ecd5248602197e2ac5b7d0338b1b to your computer and use it in GitHub Desktop.
variable "db_username" {}
variable "db_password" {}
variable "aws_key_name" {}
provider "aws" {
region = "ap-northeast-1"
shared_credentials_file = "$HOME/.aws/credentials"
profile = "default"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public_web" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.0.0/24"
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = true
tags {
Name = "web server"
}
}
resource "aws_subnet" "private_db1" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-1a"
tags {
Name = "database"
}
}
resource "aws_subnet" "private_db2" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.2.0/24"
availability_zone = "ap-northeast-1c"
tags {
Name = "database"
}
}
resource "aws_internet_gateway" "default" {
vpc_id = "${aws_vpc.main.id}"
}
data "aws_route_table" "default" {
vpc_id = "${aws_vpc.main.id}"
}
resource "aws_route" "route" {
route_table_id = "${data.aws_route_table.default.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.default.id}"
}
resource "aws_route_table_association" "a" {
subnet_id = "${aws_subnet.public_web.id}"
route_table_id = "${data.aws_route_table.default.id}"
}
resource "aws_security_group" "web" {
name = "web"
description = "For main VPC"
vpc_id = "${aws_vpc.main.id}"
tags {
Name = "web sever"
}
}
resource "aws_security_group_rule" "ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.web.id}"
}
resource "aws_security_group_rule" "web" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.web.id}"
}
resource "aws_security_group_rule" "allow_all" {
type = "egress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.web.id}"
}
resource "aws_security_group" "db" {
name = "db_server"
description = "For main VPC"
vpc_id = "${aws_vpc.main.id}"
tags {
Name = "database"
}
}
resource "aws_security_group_rule" "db" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "tcp"
source_security_group_id = "${aws_security_group.web.id}"
security_group_id = "${aws_security_group.db.id}"
}
resource "aws_db_subnet_group" "main" {
name = "dbsubnet"
description = "For main VPC"
subnet_ids = ["${aws_subnet.private_db1.id}", "${aws_subnet.private_db2.id}"]
tags {
Name = "database"
}
}
resource "aws_db_instance" "db" {
instance_class = "db.t2.micro"
identifier = "my-database"
engine = "postgres"
name = "myapp"
username = "${var.db_username}"
password = "${var.db_password}"
allocated_storage = 5
storage_type = "standard"
vpc_security_group_ids = ["${aws_security_group.db.id}"]
db_subnet_group_name = "${aws_db_subnet_group.main.name}"
final_snapshot_identifier = "myenv-final-snapshot-${md5(timestamp())}"
}
resource "aws_instance" "web" {
ami = "ami-063fa8762cdc9a5a6"
instance_type = "t2.micro"
key_name = "${var.aws_key_name}"
vpc_security_group_ids = ["${aws_security_group.web.id}"]
subnet_id = "${aws_subnet.public_web.id}"
associate_public_ip_address = "true"
}
resource "aws_eip" "web" {
instance = "${aws_instance.web.id}"
vpc = true
}
output "elastic_ip_of_web" {
value = "${aws_eip.web.public_ip}"
}
@ksoda
Copy link
Author

ksoda commented Nov 27, 2018

❯ terraform version
Terraform v0.11.7
+ provider.aws v1.47.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment