Terraform 0.12 (#20)

* Terraform 0.12
This commit is contained in:
Edward Viaene
2019-10-06 13:46:10 +02:00
committed by GitHub
parent 9e31795a58
commit 5d9eeb6c4c
201 changed files with 2528 additions and 2006 deletions
+4
View File
@@ -3,6 +3,10 @@
* These files are part of my Udemy course about Terraform * These files are part of my Udemy course about Terraform
* Course URL: https://www.udemy.com/learn-devops-infrastructure-automation-with-terraform/?couponCode=TERRAFORM_GIT * Course URL: https://www.udemy.com/learn-devops-infrastructure-automation-with-terraform/?couponCode=TERRAFORM_GIT
# Compatibility
* This is the >=terraform-0.12 branch. For compatibility with older versions, use the terraform-0.11 branch.
# Demo overview # Demo overview
Demo Directory | Description Demo Directory | Description
------------ | ------------- ------------ | -------------
+2 -1
View File
@@ -1,4 +1,5 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
} }
+3 -3
View File
@@ -1,6 +1,6 @@
provider "aws" { provider "aws" {
access_key = "${var.AWS_ACCESS_KEY}" access_key = var.AWS_ACCESS_KEY
secret_key = "${var.AWS_SECRET_KEY}" secret_key = var.AWS_SECRET_KEY
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+9 -3
View File
@@ -1,13 +1,19 @@
variable "AWS_ACCESS_KEY" {} variable "AWS_ACCESS_KEY" {
variable "AWS_SECRET_KEY" {} }
variable "AWS_SECRET_KEY" {
}
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-0d729a60" eu-west-1 = "ami-0d729a60"
} }
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+15 -14
View File
@@ -1,29 +1,30 @@
data "template_file" "init-script" { data "template_file" "init-script" {
template = "${file("scripts/init.cfg")}" template = file("scripts/init.cfg")
vars { vars = {
REGION = "${var.AWS_REGION}" REGION = var.AWS_REGION
} }
} }
data "template_file" "shell-script" {
template = "${file("scripts/volumes.sh")}"
vars {
DEVICE = "${var.INSTANCE_DEVICE_NAME}"
}
}
data "template_cloudinit_config" "cloudinit-example" {
gzip = false data "template_file" "shell-script" {
template = file("scripts/volumes.sh")
vars = {
DEVICE = var.INSTANCE_DEVICE_NAME
}
}
data "template_cloudinit_config" "cloudinit-example" {
gzip = false
base64_encode = false base64_encode = false
part { part {
filename = "init.cfg" filename = "init.cfg"
content_type = "text/cloud-config" content_type = "text/cloud-config"
content = "${data.template_file.init-script.rendered}" content = data.template_file.init-script.rendered
} }
part { part {
content_type = "text/x-shellscript" content_type = "text/x-shellscript"
content = "${data.template_file.shell-script.rendered}" content = data.template_file.shell-script.rendered
} }
} }
+14 -15
View File
@@ -1,33 +1,32 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
# the VPC subnet # the VPC subnet
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
# the security group # the security group
vpc_security_group_ids = ["${aws_security_group.allow-ssh.id}"] vpc_security_group_ids = [aws_security_group.allow-ssh.id]
# the public SSH key # the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
# user data # user data
user_data = "${data.template_cloudinit_config.cloudinit-example.rendered}" user_data = data.template_cloudinit_config.cloudinit-example.rendered
} }
resource "aws_ebs_volume" "ebs-volume-1" { resource "aws_ebs_volume" "ebs-volume-1" {
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
size = 20 size = 20
type = "gp2" type = "gp2"
tags { tags = {
Name = "extra volume data" Name = "extra volume data"
} }
} }
resource "aws_volume_attachment" "ebs-volume-1-attachment" { resource "aws_volume_attachment" "ebs-volume-1-attachment" {
device_name = "${var.INSTANCE_DEVICE_NAME}" device_name = var.INSTANCE_DEVICE_NAME
volume_id = "${aws_ebs_volume.ebs-volume-1.id}" volume_id = aws_ebs_volume.ebs-volume-1.id
instance_id = "${aws_instance.example.id}" instance_id = aws_instance.example.id
} }
+3 -2
View File
@@ -1,4 +1,5 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+12 -11
View File
@@ -1,21 +1,22 @@
resource "aws_security_group" "allow-ssh" { resource "aws_security_group" "allow-ssh" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "allow-ssh" Name = "allow-ssh"
} }
} }
+6 -1
View File
@@ -1,20 +1,25 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
variable "INSTANCE_DEVICE_NAME" { variable "INSTANCE_DEVICE_NAME" {
default = "/dev/xvdh" default = "/dev/xvdh"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+77 -70
View File
@@ -1,110 +1,117 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" { resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}" subnet_id = aws_subnet.main-public-2.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" { resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}" subnet_id = aws_subnet.main-public-3.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+27 -23
View File
@@ -1,34 +1,38 @@
resource "aws_route53_zone" "newtech-academy" { resource "aws_route53_zone" "newtech-academy" {
name = "newtech.academy" name = "newtech.academy"
} }
resource "aws_route53_record" "server1-record" { resource "aws_route53_record" "server1-record" {
zone_id = "${aws_route53_zone.newtech-academy.zone_id}" zone_id = aws_route53_zone.newtech-academy.zone_id
name = "server1.newtech.academy" name = "server1.newtech.academy"
type = "A" type = "A"
ttl = "300" ttl = "300"
records = ["104.236.247.8"] records = ["104.236.247.8"]
} }
resource "aws_route53_record" "www-record" { resource "aws_route53_record" "www-record" {
zone_id = "${aws_route53_zone.newtech-academy.zone_id}" zone_id = aws_route53_zone.newtech-academy.zone_id
name = "www.newtech.academy" name = "www.newtech.academy"
type = "A" type = "A"
ttl = "300" ttl = "300"
records = ["104.236.247.8"] records = ["104.236.247.8"]
} }
resource "aws_route53_record" "mail1-record" { resource "aws_route53_record" "mail1-record" {
zone_id = "${aws_route53_zone.newtech-academy.zone_id}" zone_id = aws_route53_zone.newtech-academy.zone_id
name = "newtech.academy" name = "newtech.academy"
type = "MX" type = "MX"
ttl = "300" ttl = "300"
records = [ records = [
"1 aspmx.l.google.com.", "1 aspmx.l.google.com.",
"5 alt1.aspmx.l.google.com.", "5 alt1.aspmx.l.google.com.",
"5 alt2.aspmx.l.google.com.", "5 alt2.aspmx.l.google.com.",
"10 aspmx2.googlemail.com.", "10 aspmx2.googlemail.com.",
"10 aspmx3.googlemail.com." "10 aspmx3.googlemail.com.",
] ]
} }
output "ns-servers" { output "ns-servers" {
value = "${aws_route53_zone.newtech-academy.name_servers}" value = aws_route53_zone.newtech-academy.name_servers
} }
+1
View File
@@ -1,3 +1,4 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+5 -5
View File
@@ -1,14 +1,14 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
# the VPC subnet # the VPC subnet
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
# the security group # the security group
vpc_security_group_ids = ["${aws_security_group.example-instance.id}"] vpc_security_group_ids = [aws_security_group.example-instance.id]
# the public SSH key # the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
} }
+3 -2
View File
@@ -1,4 +1,5 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
+4 -2
View File
@@ -1,6 +1,8 @@
output "instance" { output "instance" {
value = "${aws_instance.example.public_ip}" value = aws_instance.example.public_ip
} }
output "rds" { output "rds" {
value = "${aws_db_instance.mariadb.endpoint}" value = aws_db_instance.mariadb.endpoint
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+32 -33
View File
@@ -1,40 +1,39 @@
resource "aws_db_subnet_group" "mariadb-subnet" { resource "aws_db_subnet_group" "mariadb-subnet" {
name = "mariadb-subnet" name = "mariadb-subnet"
description = "RDS subnet group" description = "RDS subnet group"
subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"] subnet_ids = [aws_subnet.main-private-1.id, aws_subnet.main-private-2.id]
} }
resource "aws_db_parameter_group" "mariadb-parameters" { resource "aws_db_parameter_group" "mariadb-parameters" {
name = "mariadb-parameters" name = "mariadb-parameters"
family = "mariadb10.1" family = "mariadb10.1"
description = "MariaDB parameter group" description = "MariaDB parameter group"
parameter { parameter {
name = "max_allowed_packet" name = "max_allowed_packet"
value = "16777216" value = "16777216"
}
}
resource "aws_db_instance" "mariadb" {
allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number
engine = "mariadb"
engine_version = "10.1.14"
instance_class = "db.t2.small" # use micro if you want to use the free tier
identifier = "mariadb"
name = "mariadb"
username = "root" # username
password = "${var.RDS_PASSWORD}" # password
db_subnet_group_name = "${aws_db_subnet_group.mariadb-subnet.name}"
parameter_group_name = "${aws_db_parameter_group.mariadb-parameters.name}"
multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other
vpc_security_group_ids = ["${aws_security_group.allow-mariadb.id}"]
storage_type = "gp2"
backup_retention_period = 30 # how long youre going to keep your backups
availability_zone = "${aws_subnet.main-private-1.availability_zone}" # prefered AZ
skip_final_snapshot = true # skip final snapshot when doing terraform destroy
tags {
Name = "mariadb-instance"
} }
} }
resource "aws_db_instance" "mariadb" {
allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number
engine = "mariadb"
engine_version = "10.1.14"
instance_class = "db.t2.small" # use micro if you want to use the free tier
identifier = "mariadb"
name = "mariadb"
username = "root" # username
password = var.RDS_PASSWORD # password
db_subnet_group_name = aws_db_subnet_group.mariadb-subnet.name
parameter_group_name = aws_db_parameter_group.mariadb-parameters.name
multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other
vpc_security_group_ids = [aws_security_group.allow-mariadb.id]
storage_type = "gp2"
backup_retention_period = 30 # how long youre going to keep your backups
availability_zone = aws_subnet.main-private-1.availability_zone # prefered AZ
skip_final_snapshot = true # skip final snapshot when doing terraform destroy
tags = {
Name = "mariadb-instance"
}
}
+23 -23
View File
@@ -1,43 +1,43 @@
resource "aws_security_group" "example-instance" { resource "aws_security_group" "example-instance" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "example-instance" Name = "example-instance"
} }
} }
resource "aws_security_group" "allow-mariadb" { resource "aws_security_group" "allow-mariadb" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-mariadb" name = "allow-mariadb"
description = "allow-mariadb" description = "allow-mariadb"
ingress { ingress {
from_port = 3306 from_port = 3306
to_port = 3306 to_port = 3306
protocol = "tcp" protocol = "tcp"
security_groups = ["${aws_security_group.example-instance.id}"] # allowing access from our example instance security_groups = [aws_security_group.example-instance.id] # allowing access from our example instance
} }
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
self = true self = true
} }
tags { tags = {
Name = "allow-mariadb" Name = "allow-mariadb"
} }
} }
+8 -2
View File
@@ -1,18 +1,24 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
variable "RDS_PASSWORD" { }
variable "RDS_PASSWORD" {
}
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+77 -70
View File
@@ -1,110 +1,117 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" { resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}" subnet_id = aws_subnet.main-public-2.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" { resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}" subnet_id = aws_subnet.main-public-3.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
+18 -13
View File
@@ -1,28 +1,33 @@
# group definition # group definition
resource "aws_iam_group" "administrators" { resource "aws_iam_group" "administrators" {
name = "administrators" name = "administrators"
} }
resource "aws_iam_policy_attachment" "administrators-attach" { resource "aws_iam_policy_attachment" "administrators-attach" {
name = "administrators-attach" name = "administrators-attach"
groups = ["${aws_iam_group.administrators.name}"] groups = [aws_iam_group.administrators.name]
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess" policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
} }
# user # user
resource "aws_iam_user" "admin1" { resource "aws_iam_user" "admin1" {
name = "admin1" name = "admin1"
} }
resource "aws_iam_user" "admin2" { resource "aws_iam_user" "admin2" {
name = "admin2" name = "admin2"
} }
resource "aws_iam_group_membership" "administrators-users" { resource "aws_iam_group_membership" "administrators-users" {
name = "administrators-users" name = "administrators-users"
users = [ users = [
"${aws_iam_user.admin1.name}", aws_iam_user.admin1.name,
"${aws_iam_user.admin2.name}", aws_iam_user.admin2.name,
] ]
group = "${aws_iam_group.administrators.name}" group = aws_iam_group.administrators.name
} }
output "warning" { output "warning" {
value = "WARNING: make sure you're not using the AdministratorAccess policy for other users/groups/roles. If this is the case, don't run terraform destroy, but manually unlink the created resources" value = "WARNING: make sure you're not using the AdministratorAccess policy for other users/groups/roles. If this is the case, don't run terraform destroy, but manually unlink the created resources"
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+1
View File
@@ -1,3 +1,4 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+9 -7
View File
@@ -1,6 +1,6 @@
resource "aws_iam_role" "s3-mybucket-role" { resource "aws_iam_role" "s3-mybucket-role" {
name = "s3-mybucket-role" name = "s3-mybucket-role"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
@@ -15,17 +15,18 @@ resource "aws_iam_role" "s3-mybucket-role" {
] ]
} }
EOF EOF
} }
resource "aws_iam_instance_profile" "s3-mybucket-role-instanceprofile" { resource "aws_iam_instance_profile" "s3-mybucket-role-instanceprofile" {
name = "s3-mybucket-role" name = "s3-mybucket-role"
role = "${aws_iam_role.s3-mybucket-role.name}" role = aws_iam_role.s3-mybucket-role.name
} }
resource "aws_iam_role_policy" "s3-mybucket-role-policy" { resource "aws_iam_role_policy" "s3-mybucket-role-policy" {
name = "s3-mybucket-role-policy" name = "s3-mybucket-role-policy"
role = "${aws_iam_role.s3-mybucket-role.id}" role = aws_iam_role.s3-mybucket-role.id
policy = <<EOF policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
@@ -42,5 +43,6 @@ resource "aws_iam_role_policy" "s3-mybucket-role-policy" {
] ]
} }
EOF EOF
} }
+6 -6
View File
@@ -1,17 +1,17 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
# the VPC subnet # the VPC subnet
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
# the security group # the security group
vpc_security_group_ids = ["${aws_security_group.example-instance.id}"] vpc_security_group_ids = [aws_security_group.example-instance.id]
# the public SSH key # the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
# role: # role:
iam_instance_profile = "${aws_iam_instance_profile.s3-mybucket-role-instanceprofile.name}" iam_instance_profile = aws_iam_instance_profile.s3-mybucket-role-instanceprofile.name
} }
+3 -2
View File
@@ -1,4 +1,5 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
+2 -1
View File
@@ -1,3 +1,4 @@
output "instance" { output "instance" {
value = "${aws_instance.example.public_ip}" value = aws_instance.example.public_ip
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+6 -5
View File
@@ -1,8 +1,9 @@
resource "aws_s3_bucket" "b" { resource "aws_s3_bucket" "b" {
bucket = "mybucket-c29df1" bucket = "mybucket-c29df1"
acl = "private" acl = "private"
tags { tags = {
Name = "mybucket-c29df1" Name = "mybucket-c29df1"
} }
} }
+11 -11
View File
@@ -1,21 +1,21 @@
resource "aws_security_group" "example-instance" { resource "aws_security_group" "example-instance" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "example-instance" Name = "example-instance"
} }
} }
+5 -1
View File
@@ -1,17 +1,21 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+77 -70
View File
@@ -1,110 +1,117 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" { resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}" subnet_id = aws_subnet.main-public-2.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" { resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}" subnet_id = aws_subnet.main-public-3.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
+15 -15
View File
@@ -1,25 +1,25 @@
resource "aws_launch_configuration" "example-launchconfig" { resource "aws_launch_configuration" "example-launchconfig" {
name_prefix = "example-launchconfig" name_prefix = "example-launchconfig"
image_id = "${lookup(var.AMIS, var.AWS_REGION)}" image_id = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
security_groups = ["${aws_security_group.allow-ssh.id}"] security_groups = [aws_security_group.allow-ssh.id]
} }
resource "aws_autoscaling_group" "example-autoscaling" { resource "aws_autoscaling_group" "example-autoscaling" {
name = "example-autoscaling" name = "example-autoscaling"
vpc_zone_identifier = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"] vpc_zone_identifier = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
launch_configuration = "${aws_launch_configuration.example-launchconfig.name}" launch_configuration = aws_launch_configuration.example-launchconfig.name
min_size = 1 min_size = 1
max_size = 2 max_size = 2
health_check_grace_period = 300 health_check_grace_period = 300
health_check_type = "EC2" health_check_type = "EC2"
force_delete = true force_delete = true
tag { tag {
key = "Name" key = "Name"
value = "ec2 instance" value = "ec2 instance"
propagate_at_launch = true propagate_at_launch = true
} }
} }
+7 -6
View File
@@ -2,7 +2,7 @@
resource "aws_autoscaling_policy" "example-cpu-policy" { resource "aws_autoscaling_policy" "example-cpu-policy" {
name = "example-cpu-policy" name = "example-cpu-policy"
autoscaling_group_name = "${aws_autoscaling_group.example-autoscaling.name}" autoscaling_group_name = aws_autoscaling_group.example-autoscaling.name
adjustment_type = "ChangeInCapacity" adjustment_type = "ChangeInCapacity"
scaling_adjustment = "1" scaling_adjustment = "1"
cooldown = "300" cooldown = "300"
@@ -21,17 +21,17 @@ resource "aws_cloudwatch_metric_alarm" "example-cpu-alarm" {
threshold = "30" threshold = "30"
dimensions = { dimensions = {
"AutoScalingGroupName" = "${aws_autoscaling_group.example-autoscaling.name}" "AutoScalingGroupName" = aws_autoscaling_group.example-autoscaling.name
} }
actions_enabled = true actions_enabled = true
alarm_actions = ["${aws_autoscaling_policy.example-cpu-policy.arn}"] alarm_actions = [aws_autoscaling_policy.example-cpu-policy.arn]
} }
# scale down alarm # scale down alarm
resource "aws_autoscaling_policy" "example-cpu-policy-scaledown" { resource "aws_autoscaling_policy" "example-cpu-policy-scaledown" {
name = "example-cpu-policy-scaledown" name = "example-cpu-policy-scaledown"
autoscaling_group_name = "${aws_autoscaling_group.example-autoscaling.name}" autoscaling_group_name = aws_autoscaling_group.example-autoscaling.name
adjustment_type = "ChangeInCapacity" adjustment_type = "ChangeInCapacity"
scaling_adjustment = "-1" scaling_adjustment = "-1"
cooldown = "300" cooldown = "300"
@@ -50,9 +50,10 @@ resource "aws_cloudwatch_metric_alarm" "example-cpu-alarm-scaledown" {
threshold = "5" threshold = "5"
dimensions = { dimensions = {
"AutoScalingGroupName" = "${aws_autoscaling_group.example-autoscaling.name}" "AutoScalingGroupName" = aws_autoscaling_group.example-autoscaling.name
} }
actions_enabled = true actions_enabled = true
alarm_actions = ["${aws_autoscaling_policy.example-cpu-policy-scaledown.arn}"] alarm_actions = [aws_autoscaling_policy.example-cpu-policy-scaledown.arn]
} }
+4 -3
View File
@@ -1,7 +1,8 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
lifecycle { lifecycle {
ignore_changes = ["public_key"] ignore_changes = [public_key]
} }
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+12 -11
View File
@@ -1,21 +1,22 @@
resource "aws_security_group" "allow-ssh" { resource "aws_security_group" "allow-ssh" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "allow-ssh" Name = "allow-ssh"
} }
} }
-1
View File
@@ -1,5 +1,4 @@
# Uncomment if you want to have autoscaling notifications # Uncomment if you want to have autoscaling notifications
#resource "aws_sns_topic" "example-sns" { #resource "aws_sns_topic" "example-sns" {
# name = "sg-sns" # name = "sg-sns"
# display_name = "example ASG SNS topic" # display_name = "example ASG SNS topic"
+5 -1
View File
@@ -1,17 +1,21 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+77 -70
View File
@@ -1,110 +1,117 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" { resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}" subnet_id = aws_subnet.main-public-2.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" { resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}" subnet_id = aws_subnet.main-public-3.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
+26 -24
View File
@@ -1,28 +1,30 @@
resource "aws_launch_configuration" "example-launchconfig" { resource "aws_launch_configuration" "example-launchconfig" {
name_prefix = "example-launchconfig" name_prefix = "example-launchconfig"
image_id = "${lookup(var.AMIS, var.AWS_REGION)}" image_id = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
security_groups = ["${aws_security_group.myinstance.id}"] security_groups = [aws_security_group.myinstance.id]
user_data = "#!/bin/bash\napt-get update\napt-get -y install nginx\nMYIP=`ifconfig | grep 'addr:10' | awk '{ print $2 }' | cut -d ':' -f2`\necho 'this is: '$MYIP > /var/www/html/index.html" user_data = "#!/bin/bash\napt-get update\napt-get -y install nginx\nMYIP=`ifconfig | grep 'addr:10' | awk '{ print $2 }' | cut -d ':' -f2`\necho 'this is: '$MYIP > /var/www/html/index.html"
lifecycle { create_before_destroy = true } lifecycle {
} create_before_destroy = true
}
resource "aws_autoscaling_group" "example-autoscaling" { }
name = "example-autoscaling"
vpc_zone_identifier = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"] resource "aws_autoscaling_group" "example-autoscaling" {
launch_configuration = "${aws_launch_configuration.example-launchconfig.name}" name = "example-autoscaling"
min_size = 2 vpc_zone_identifier = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
max_size = 2 launch_configuration = aws_launch_configuration.example-launchconfig.name
health_check_grace_period = 300 min_size = 2
health_check_type = "ELB" max_size = 2
load_balancers = ["${aws_elb.my-elb.name}"] health_check_grace_period = 300
force_delete = true health_check_type = "ELB"
load_balancers = [aws_elb.my-elb.name]
tag { force_delete = true
key = "Name"
value = "ec2 instance" tag {
propagate_at_launch = true key = "Name"
value = "ec2 instance"
propagate_at_launch = true
} }
} }
+14 -14
View File
@@ -1,25 +1,25 @@
resource "aws_elb" "my-elb" { resource "aws_elb" "my-elb" {
name = "my-elb" name = "my-elb"
subnets = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"] subnets = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
security_groups = ["${aws_security_group.elb-securitygroup.id}"] security_groups = [aws_security_group.elb-securitygroup.id]
listener { listener {
instance_port = 80 instance_port = 80
instance_protocol = "http" instance_protocol = "http"
lb_port = 80 lb_port = 80
lb_protocol = "http" lb_protocol = "http"
} }
health_check { health_check {
healthy_threshold = 2 healthy_threshold = 2
unhealthy_threshold = 2 unhealthy_threshold = 2
timeout = 3 timeout = 3
target = "HTTP:80/" target = "HTTP:80/"
interval = 30 interval = 30
} }
cross_zone_load_balancing = true cross_zone_load_balancing = true
connection_draining = true connection_draining = true
connection_draining_timeout = 400 connection_draining_timeout = 400
tags { tags = {
Name = "my-elb" Name = "my-elb"
} }
} }
+4 -3
View File
@@ -1,7 +1,8 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
lifecycle { lifecycle {
ignore_changes = ["public_key"] ignore_changes = [public_key]
} }
} }
+2 -1
View File
@@ -1,3 +1,4 @@
output "ELB" { output "ELB" {
value = "${aws_elb.my-elb.dns_name}" value = aws_elb.my-elb.dns_name
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+28 -26
View File
@@ -1,50 +1,52 @@
resource "aws_security_group" "myinstance" { resource "aws_security_group" "myinstance" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "myinstance" name = "myinstance"
description = "security group for my instance" description = "security group for my instance"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 80 from_port = 80
to_port = 80 to_port = 80
protocol = "tcp" protocol = "tcp"
security_groups = ["${aws_security_group.elb-securitygroup.id}"] security_groups = [aws_security_group.elb-securitygroup.id]
} }
tags { tags = {
Name = "myinstance" Name = "myinstance"
} }
} }
resource "aws_security_group" "elb-securitygroup" { resource "aws_security_group" "elb-securitygroup" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "elb" name = "elb"
description = "security group for load balancer" description = "security group for load balancer"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 80 from_port = 80
to_port = 80 to_port = 80
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "elb" Name = "elb"
} }
} }
+5 -1
View File
@@ -1,17 +1,21 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+77 -70
View File
@@ -1,110 +1,117 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" { resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}" subnet_id = aws_subnet.main-public-2.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" { resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}" subnet_id = aws_subnet.main-public-3.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
+42 -43
View File
@@ -1,112 +1,111 @@
resource "aws_elastic_beanstalk_application" "app" { resource "aws_elastic_beanstalk_application" "app" {
name = "app" name = "app"
description = "app" description = "app"
} }
resource "aws_elastic_beanstalk_environment" "app-prod" { resource "aws_elastic_beanstalk_environment" "app-prod" {
name = "app-prod" name = "app-prod"
application = "${aws_elastic_beanstalk_application.app.name}" application = aws_elastic_beanstalk_application.app.name
solution_stack_name = "64bit Amazon Linux 2016.09 v2.3.0 running PHP 7.0" solution_stack_name = "64bit Amazon Linux 2016.09 v2.3.0 running PHP 7.0"
setting { setting {
namespace = "aws:ec2:vpc" namespace = "aws:ec2:vpc"
name = "VPCId" name = "VPCId"
value = "${aws_vpc.main.id}" value = aws_vpc.main.id
} }
setting { setting {
namespace = "aws:ec2:vpc" namespace = "aws:ec2:vpc"
name = "Subnets" name = "Subnets"
value = "${aws_subnet.main-private-1.id},${aws_subnet.main-private-2.id}" value = "${aws_subnet.main-private-1.id},${aws_subnet.main-private-2.id}"
} }
setting { setting {
namespace = "aws:ec2:vpc" namespace = "aws:ec2:vpc"
name = "AssociatePublicIpAddress" name = "AssociatePublicIpAddress"
value = "false" value = "false"
} }
setting { setting {
namespace = "aws:autoscaling:launchconfiguration" namespace = "aws:autoscaling:launchconfiguration"
name = "IamInstanceProfile" name = "IamInstanceProfile"
value = "${aws_iam_instance_profile.app-ec2-role.name}" value = aws_iam_instance_profile.app-ec2-role.name
} }
setting { setting {
namespace = "aws:autoscaling:launchconfiguration" namespace = "aws:autoscaling:launchconfiguration"
name = "SecurityGroups" name = "SecurityGroups"
value = "${aws_security_group.app-prod.id}" value = aws_security_group.app-prod.id
} }
setting { setting {
namespace = "aws:autoscaling:launchconfiguration" namespace = "aws:autoscaling:launchconfiguration"
name = "EC2KeyName" name = "EC2KeyName"
value = "${aws_key_pair.mykeypair.id}" value = aws_key_pair.mykeypair.id
} }
setting { setting {
namespace = "aws:autoscaling:launchconfiguration" namespace = "aws:autoscaling:launchconfiguration"
name = "InstanceType" name = "InstanceType"
value = "t2.micro" value = "t2.micro"
} }
setting { setting {
namespace = "aws:elasticbeanstalk:environment" namespace = "aws:elasticbeanstalk:environment"
name = "ServiceRole" name = "ServiceRole"
value = "${aws_iam_role.elasticbeanstalk-service-role.name}" value = aws_iam_role.elasticbeanstalk-service-role.name
} }
setting { setting {
namespace = "aws:ec2:vpc" namespace = "aws:ec2:vpc"
name = "ELBScheme" name = "ELBScheme"
value = "public" value = "public"
} }
setting { setting {
namespace = "aws:ec2:vpc" namespace = "aws:ec2:vpc"
name = "ELBSubnets" name = "ELBSubnets"
value = "${aws_subnet.main-public-1.id},${aws_subnet.main-public-2.id}" value = "${aws_subnet.main-public-1.id},${aws_subnet.main-public-2.id}"
} }
setting { setting {
namespace = "aws:elb:loadbalancer" namespace = "aws:elb:loadbalancer"
name = "CrossZone" name = "CrossZone"
value = "true" value = "true"
} }
setting { setting {
namespace = "aws:elasticbeanstalk:command" namespace = "aws:elasticbeanstalk:command"
name = "BatchSize" name = "BatchSize"
value = "30" value = "30"
} }
setting { setting {
namespace = "aws:elasticbeanstalk:command" namespace = "aws:elasticbeanstalk:command"
name = "BatchSizeType" name = "BatchSizeType"
value = "Percentage" value = "Percentage"
} }
setting { setting {
namespace = "aws:autoscaling:asg" namespace = "aws:autoscaling:asg"
name = "Availability Zones" name = "Availability Zones"
value = "Any 2" value = "Any 2"
} }
setting { setting {
namespace = "aws:autoscaling:asg" namespace = "aws:autoscaling:asg"
name = "MinSize" name = "MinSize"
value = "1" value = "1"
} }
setting { setting {
namespace = "aws:autoscaling:updatepolicy:rollingupdate" namespace = "aws:autoscaling:updatepolicy:rollingupdate"
name = "RollingUpdateType" name = "RollingUpdateType"
value = "Health" value = "Health"
} }
setting { setting {
namespace = "aws:elasticbeanstalk:application:environment" namespace = "aws:elasticbeanstalk:application:environment"
name = "RDS_USERNAME" name = "RDS_USERNAME"
value = "${aws_db_instance.mariadb.username}" value = aws_db_instance.mariadb.username
} }
setting { setting {
namespace = "aws:elasticbeanstalk:application:environment" namespace = "aws:elasticbeanstalk:application:environment"
name = "RDS_PASSWORD" name = "RDS_PASSWORD"
value = "${aws_db_instance.mariadb.password}" value = aws_db_instance.mariadb.password
} }
setting { setting {
namespace = "aws:elasticbeanstalk:application:environment" namespace = "aws:elasticbeanstalk:application:environment"
name = "RDS_DATABASE" name = "RDS_DATABASE"
value = "mydb" value = aws_db_instance.mariadb.name
value = "${aws_db_instance.mariadb.name}"
} }
setting { setting {
namespace = "aws:elasticbeanstalk:application:environment" namespace = "aws:elasticbeanstalk:application:environment"
name = "RDS_HOSTNAME" name = "RDS_HOSTNAME"
value = "${aws_db_instance.mariadb.endpoint}" value = aws_db_instance.mariadb.endpoint
} }
} }
+25 -18
View File
@@ -1,7 +1,7 @@
# iam roles # iam roles
resource "aws_iam_role" "app-ec2-role" { resource "aws_iam_role" "app-ec2-role" {
name = "app-ec2-role" name = "app-ec2-role"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
@@ -16,16 +16,18 @@ resource "aws_iam_role" "app-ec2-role" {
] ]
} }
EOF EOF
} }
resource "aws_iam_instance_profile" "app-ec2-role" { resource "aws_iam_instance_profile" "app-ec2-role" {
name = "app-ec2-role" name = "app-ec2-role"
role = "${aws_iam_role.app-ec2-role.name}" role = aws_iam_role.app-ec2-role.name
} }
# service # service
resource "aws_iam_role" "elasticbeanstalk-service-role" { resource "aws_iam_role" "elasticbeanstalk-service-role" {
name = "elasticbeanstalk-service-role" name = "elasticbeanstalk-service-role"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
@@ -40,26 +42,31 @@ resource "aws_iam_role" "elasticbeanstalk-service-role" {
] ]
} }
EOF EOF
} }
# policies # policies
resource "aws_iam_policy_attachment" "app-attach1" { resource "aws_iam_policy_attachment" "app-attach1" {
name = "app-attach1" name = "app-attach1"
roles = ["${aws_iam_role.app-ec2-role.name}"] roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier" policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier"
} }
resource "aws_iam_policy_attachment" "app-attach2" { resource "aws_iam_policy_attachment" "app-attach2" {
name = "app-attach2" name = "app-attach2"
roles = ["${aws_iam_role.app-ec2-role.name}"] roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker" policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker"
} }
resource "aws_iam_policy_attachment" "app-attach3" { resource "aws_iam_policy_attachment" "app-attach3" {
name = "app-attach3" name = "app-attach3"
roles = ["${aws_iam_role.app-ec2-role.name}"] roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier" policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
} }
resource "aws_iam_policy_attachment" "app-attach4" { resource "aws_iam_policy_attachment" "app-attach4" {
name = "app-attach4" name = "app-attach4"
roles = ["${aws_iam_role.elasticbeanstalk-service-role.name}"] roles = [aws_iam_role.elasticbeanstalk-service-role.name]
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth" policy_arn = "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
} }
+4 -3
View File
@@ -1,7 +1,8 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
lifecycle { lifecycle {
ignore_changes = ["public_key"] ignore_changes = [public_key]
} }
} }
+2 -1
View File
@@ -1,3 +1,4 @@
output "eb" { output "eb" {
value = "${aws_elastic_beanstalk_environment.app-prod.cname}" value = aws_elastic_beanstalk_environment.app-prod.cname
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+32 -33
View File
@@ -1,40 +1,39 @@
resource "aws_db_subnet_group" "mariadb-subnet" { resource "aws_db_subnet_group" "mariadb-subnet" {
name = "mariadb-subnet" name = "mariadb-subnet"
description = "RDS subnet group" description = "RDS subnet group"
subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"] subnet_ids = [aws_subnet.main-private-1.id, aws_subnet.main-private-2.id]
} }
resource "aws_db_parameter_group" "mariadb-parameters" { resource "aws_db_parameter_group" "mariadb-parameters" {
name = "mariadb-params" name = "mariadb-params"
family = "mariadb10.1" family = "mariadb10.1"
description = "MariaDB parameter group" description = "MariaDB parameter group"
parameter { parameter {
name = "max_allowed_packet" name = "max_allowed_packet"
value = "16777216" value = "16777216"
}
}
resource "aws_db_instance" "mariadb" {
allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number
engine = "mariadb"
engine_version = "10.1.14"
instance_class = "db.t2.small" # use micro if you want to use the free tier
identifier = "mariadb"
name = "mydatabase" # database name
username = "root" # username
password = "${var.RDS_PASSWORD}" # password
db_subnet_group_name = "${aws_db_subnet_group.mariadb-subnet.name}"
parameter_group_name = "${aws_db_parameter_group.mariadb-parameters.name}"
multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other
vpc_security_group_ids = ["${aws_security_group.allow-mariadb.id}"]
storage_type = "gp2"
backup_retention_period = 30 # how long youre going to keep your backups
availability_zone = "${aws_subnet.main-private-1.availability_zone}" # prefered AZ
final_snapshot_identifier = "mariadb-final-snapshot" # final snapshot when executing terraform destroy
tags {
Name = "mariadb-instance"
} }
} }
resource "aws_db_instance" "mariadb" {
allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number
engine = "mariadb"
engine_version = "10.1.14"
instance_class = "db.t2.small" # use micro if you want to use the free tier
identifier = "mariadb"
name = "mydatabase" # database name
username = "root" # username
password = var.RDS_PASSWORD # password
db_subnet_group_name = aws_db_subnet_group.mariadb-subnet.name
parameter_group_name = aws_db_parameter_group.mariadb-parameters.name
multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other
vpc_security_group_ids = [aws_security_group.allow-mariadb.id]
storage_type = "gp2"
backup_retention_period = 30 # how long youre going to keep your backups
availability_zone = aws_subnet.main-private-1.availability_zone # prefered AZ
final_snapshot_identifier = "mariadb-final-snapshot" # final snapshot when executing terraform destroy
tags = {
Name = "mariadb-instance"
}
}
+25 -24
View File
@@ -1,44 +1,45 @@
resource "aws_security_group" "app-prod" { resource "aws_security_group" "app-prod" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "application - production" name = "application - production"
description = "security group for my app" description = "security group for my app"
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags = {
tags {
Name = "myinstance" Name = "myinstance"
} }
} }
resource "aws_security_group" "allow-mariadb" { resource "aws_security_group" "allow-mariadb" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
name = "allow-mariadb" name = "allow-mariadb"
description = "allow-mariadb" description = "allow-mariadb"
ingress { ingress {
from_port = 3306 from_port = 3306
to_port = 3306 to_port = 3306
protocol = "tcp" protocol = "tcp"
security_groups = ["${aws_security_group.app-prod.id}"] # allowing access from our example instance security_groups = [aws_security_group.app-prod.id] # allowing access from our example instance
} }
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
self = true self = true
} }
tags { tags = {
Name = "allow-mariadb" Name = "allow-mariadb"
} }
} }
+6 -1
View File
@@ -1,10 +1,15 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "RDS_PASSWORD" {}
variable "RDS_PASSWORD" {
}
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+106 -93
View File
@@ -1,143 +1,156 @@
# Internet VPC # Internet VPC
resource "aws_vpc" "main" { resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" cidr_block = "10.0.0.0/16"
instance_tenancy = "default" instance_tenancy = "default"
enable_dns_support = "true" enable_dns_support = "true"
enable_dns_hostnames = "true" enable_dns_hostnames = "true"
enable_classiclink = "false" enable_classiclink = "false"
tags { tags = {
Name = "main" Name = "main"
} }
} }
# Subnets # Subnets
resource "aws_subnet" "main-public-1" { resource "aws_subnet" "main-public-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
resource "aws_subnet" "main-public-2" { resource "aws_subnet" "main-public-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24" cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-public-2" Name = "main-public-2"
} }
} }
resource "aws_subnet" "main-public-3" { resource "aws_subnet" "main-public-3" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24" cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true" map_public_ip_on_launch = "true"
availability_zone = "eu-west-1c" availability_zone = "eu-west-1c"
tags { tags = {
Name = "main-public-3" Name = "main-public-3"
} }
} }
resource "aws_subnet" "main-private-1" { resource "aws_subnet" "main-private-1" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.4.0/24" cidr_block = "10.0.4.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1a" availability_zone = "eu-west-1a"
tags { tags = {
Name = "main-private-1" Name = "main-private-1"
} }
} }
resource "aws_subnet" "main-private-2" { resource "aws_subnet" "main-private-2" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
cidr_block = "10.0.5.0/24" cidr_block = "10.0.5.0/24"
map_public_ip_on_launch = "false" map_public_ip_on_launch = "false"
availability_zone = "eu-west-1b" availability_zone = "eu-west-1b"
tags { tags = {
Name = "main-private-2" Name = "main-private-2"
} }
} }
resource "aws_subnet" "main-private-3" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags { resource "aws_subnet" "main-private-3" {
Name = "main-private-3" vpc_id = aws_vpc.main.id
} cidr_block = "10.0.6.0/24"
map_public_ip_on_launch = "false"
availability_zone = "eu-west-1c"
tags = {
Name = "main-private-3"
}
} }
# Internet GW # Internet GW
resource "aws_internet_gateway" "main-gw" { resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
tags { tags = {
Name = "main" Name = "main"
} }
} }
# route tables # route tables
resource "aws_route_table" "main-public" { resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.main.id}" vpc_id = aws_vpc.main.id
route { route {
cidr_block = "0.0.0.0/0" cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}" gateway_id = aws_internet_gateway.main-gw.id
} }
tags { tags = {
Name = "main-public-1" Name = "main-public-1"
} }
} }
# route associations public # route associations public
resource "aws_route_table_association" "main-public-1-a" { resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
route_table_id = "${aws_route_table.main-public.id}" route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-2-a" {
subnet_id = "${aws_subnet.main-public-2.id}"
route_table_id = "${aws_route_table.main-public.id}"
}
resource "aws_route_table_association" "main-public-3-a" {
subnet_id = "${aws_subnet.main-public-3.id}"
route_table_id = "${aws_route_table.main-public.id}"
}
resource "aws_route_table" "main-private" {
vpc_id = "${aws_vpc.main.id}"
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.nat-gw.id}"
}
tags { resource "aws_route_table_association" "main-public-2-a" {
Name = "main-private-1" subnet_id = aws_subnet.main-public-2.id
} route_table_id = aws_route_table.main-public.id
} }
resource "aws_route_table_association" "main-public-3-a" {
subnet_id = aws_subnet.main-public-3.id
route_table_id = aws_route_table.main-public.id
}
resource "aws_route_table" "main-private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat-gw.id
}
tags = {
Name = "main-private-1"
}
}
# route associations private # route associations private
resource "aws_route_table_association" "main-private-1-a" { resource "aws_route_table_association" "main-private-1-a" {
subnet_id = "${aws_subnet.main-private-1.id}" subnet_id = aws_subnet.main-private-1.id
route_table_id = "${aws_route_table.main-private.id}" route_table_id = aws_route_table.main-private.id
} }
resource "aws_route_table_association" "main-private-2-a" { resource "aws_route_table_association" "main-private-2-a" {
subnet_id = "${aws_subnet.main-private-2.id}" subnet_id = aws_subnet.main-private-2.id
route_table_id = "${aws_route_table.main-private.id}" route_table_id = aws_route_table.main-private.id
} }
resource "aws_route_table_association" "main-private-3-a" { resource "aws_route_table_association" "main-private-3-a" {
subnet_id = "${aws_subnet.main-private-3.id}" subnet_id = aws_subnet.main-private-3.id
route_table_id = "${aws_route_table.main-private.id}" route_table_id = aws_route_table.main-private.id
} }
# nat gw # nat gw
resource "aws_eip" "nat" { resource "aws_eip" "nat" {
vpc = true vpc = true
} }
resource "aws_nat_gateway" "nat-gw" { resource "aws_nat_gateway" "nat-gw" {
allocation_id = "${aws_eip.nat.id}" allocation_id = aws_eip.nat.id
subnet_id = "${aws_subnet.main-public-1.id}" subnet_id = aws_subnet.main-public-1.id
depends_on = ["aws_internet_gateway.main-gw"] depends_on = [aws_internet_gateway.main-gw]
} }
+5 -4
View File
@@ -15,15 +15,16 @@ data "aws_ami" "ubuntu" {
} }
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${data.aws_ami.ubuntu.id}" ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro" instance_type = "t2.micro"
# the VPC subnet # the VPC subnet
subnet_id = "${var.ENV == "prod" ? module.vpc-prod.public_subnets[0] : module.vpc-dev.public_subnets[0] }" subnet_id = var.ENV == "prod" ? module.vpc-prod.public_subnets[0] : module.vpc-dev.public_subnets[0]
# the security group # the security group
vpc_security_group_ids = ["${var.ENV == "prod" ? aws_security_group.allow-ssh-prod.id : aws_security_group.allow-ssh-dev.id }"] vpc_security_group_ids = [var.ENV == "prod" ? aws_security_group.allow-ssh-prod.id : aws_security_group.allow-ssh-dev.id]
# the public SSH key # the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
} }
+2 -1
View File
@@ -1,4 +1,5 @@
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair" key_name = "mykeypair"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+5 -4
View File
@@ -1,5 +1,5 @@
resource "aws_security_group" "allow-ssh-prod" { resource "aws_security_group" "allow-ssh-prod" {
vpc_id = "${module.vpc-prod.vpc_id}" vpc_id = module.vpc-prod.vpc_id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
@@ -17,13 +17,13 @@ resource "aws_security_group" "allow-ssh-prod" {
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "allow-ssh" Name = "allow-ssh"
} }
} }
resource "aws_security_group" "allow-ssh-dev" { resource "aws_security_group" "allow-ssh-dev" {
vpc_id = "${module.vpc-dev.vpc_id}" vpc_id = module.vpc-dev.vpc_id
name = "allow-ssh" name = "allow-ssh"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
@@ -41,7 +41,8 @@ resource "aws_security_group" "allow-ssh-dev" {
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "allow-ssh" Name = "allow-ssh"
} }
} }
+1
View File
@@ -13,3 +13,4 @@ variable "PATH_TO_PUBLIC_KEY" {
variable "ENV" { variable "ENV" {
default = "prod" default = "prod"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+5 -2
View File
@@ -1,5 +1,6 @@
module "vpc-prod" { module "vpc-prod" {
source = "terraform-aws-modules/vpc/aws" source = "terraform-aws-modules/vpc/aws"
version = "2.5.0"
name = "vpc-prod" name = "vpc-prod"
cidr = "10.0.0.0/16" cidr = "10.0.0.0/16"
@@ -18,7 +19,8 @@ module "vpc-prod" {
} }
module "vpc-dev" { module "vpc-dev" {
source = "terraform-aws-modules/vpc/aws" source = "terraform-aws-modules/vpc/aws"
version = "2.5.0"
name = "vpc-dev" name = "vpc-dev"
cidr = "10.0.0.0/16" cidr = "10.0.0.0/16"
@@ -35,3 +37,4 @@ module "vpc-dev" {
Environment = "dev" Environment = "dev"
} }
} }
+8 -7
View File
@@ -1,12 +1,13 @@
module "main-vpc" { module "main-vpc" {
source = "../modules/vpc" source = "../modules/vpc"
ENV = "dev" ENV = "dev"
AWS_REGION = "${var.AWS_REGION}" AWS_REGION = var.AWS_REGION
} }
module "instances" { module "instances" {
source = "../modules/instances" source = "../modules/instances"
ENV = "dev" ENV = "dev"
VPC_ID = "${module.main-vpc.vpc_id}" VPC_ID = module.main-vpc.vpc_id
PUBLIC_SUBNETS = ["${module.main-vpc.public_subnets}"] PUBLIC_SUBNETS = module.main-vpc.public_subnets
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+1
View File
@@ -1,3 +1,4 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+17 -14
View File
@@ -1,14 +1,16 @@
variable "ENV" {} variable "ENV" {
}
variable "INSTANCE_TYPE" { variable "INSTANCE_TYPE" {
default = "t2.micro" default = "t2.micro"
} }
variable "PUBLIC_SUBNETS" { variable "PUBLIC_SUBNETS" {
type = "list" type = list
} }
variable "VPC_ID" {} variable "VPC_ID" {
}
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
@@ -31,26 +33,26 @@ data "aws_ami" "ubuntu" {
} }
resource "aws_instance" "instance" { resource "aws_instance" "instance" {
ami = "${data.aws_ami.ubuntu.id}" ami = data.aws_ami.ubuntu.id
instance_type = "${var.INSTANCE_TYPE}" instance_type = var.INSTANCE_TYPE
# the VPC subnet # the VPC subnet
subnet_id = "${var.PUBLIC_SUBNETS[0]}" subnet_id = element(var.PUBLIC_SUBNETS, 0)
# the security group # the security group
vpc_security_group_ids = ["${aws_security_group.allow-ssh.id}"] vpc_security_group_ids = [aws_security_group.allow-ssh.id]
# the public SSH key # the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}" key_name = aws_key_pair.mykeypair.key_name
tags { tags = {
Name = "instance-${var.ENV}" Name = "instance-${var.ENV}"
Environmnent = "${var.ENV}" Environmnent = var.ENV
} }
} }
resource "aws_security_group" "allow-ssh" { resource "aws_security_group" "allow-ssh" {
vpc_id = "${var.VPC_ID}" vpc_id = var.VPC_ID
name = "allow-ssh-${var.ENV}" name = "allow-ssh-${var.ENV}"
description = "security group that allows ssh and all egress traffic" description = "security group that allows ssh and all egress traffic"
@@ -68,13 +70,14 @@ resource "aws_security_group" "allow-ssh" {
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags { tags = {
Name = "allow-ssh" Name = "allow-ssh"
Environmnent = "${var.ENV}" Environmnent = var.ENV
} }
} }
resource "aws_key_pair" "mykeypair" { resource "aws_key_pair" "mykeypair" {
key_name = "mykeypair-${var.ENV}" key_name = "mykeypair-${var.ENV}"
public_key = "${file("${path.root}/${var.PATH_TO_PUBLIC_KEY}")}" public_key = file("${path.root}/${var.PATH_TO_PUBLIC_KEY}")
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+10 -6
View File
@@ -1,5 +1,8 @@
variable "ENV" {} variable "ENV" {
variable "AWS_REGION" {} }
variable "AWS_REGION" {
}
module "main-vpc" { module "main-vpc" {
source = "terraform-aws-modules/vpc/aws" source = "terraform-aws-modules/vpc/aws"
@@ -16,21 +19,22 @@ module "main-vpc" {
tags = { tags = {
Terraform = "true" Terraform = "true"
Environment = "${var.ENV}" Environment = var.ENV
} }
} }
output "vpc_id" { output "vpc_id" {
description = "The ID of the VPC" description = "The ID of the VPC"
value = "${module.main-vpc.vpc_id}" value = module.main-vpc.vpc_id
} }
output "private_subnets" { output "private_subnets" {
description = "List of IDs of private subnets" description = "List of IDs of private subnets"
value = ["${module.main-vpc.private_subnets}"] value = module.main-vpc.private_subnets
} }
output "public_subnets" { output "public_subnets" {
description = "List of IDs of public subnets" description = "List of IDs of public subnets"
value = ["${module.main-vpc.public_subnets}"] value = module.main-vpc.public_subnets
} }
+8 -7
View File
@@ -1,12 +1,13 @@
module "main-vpc" { module "main-vpc" {
source = "../modules/vpc" source = "../modules/vpc"
ENV = "prod" ENV = "prod"
AWS_REGION = "${var.AWS_REGION}" AWS_REGION = var.AWS_REGION
} }
module "instances" { module "instances" {
source = "../modules/instances" source = "../modules/instances"
ENV = "prod" ENV = "prod"
VPC_ID = "${module.main-vpc.vpc_id}" VPC_ID = module.main-vpc.vpc_id
PUBLIC_SUBNETS = ["${module.main-vpc.public_subnets}"] PUBLIC_SUBNETS = module.main-vpc.public_subnets
} }
+2 -1
View File
@@ -1,3 +1,4 @@
provider "aws" { provider "aws" {
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+1
View File
@@ -1,3 +1,4 @@
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+11 -9
View File
@@ -1,26 +1,28 @@
resource "aws_key_pair" "mykey" { resource "aws_key_pair" "mykey" {
key_name = "mykey" key_name = "mykey"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
key_name = "${aws_key_pair.mykey.key_name}" key_name = aws_key_pair.mykey.key_name
provisioner "file" { provisioner "file" {
source = "script.sh" source = "script.sh"
destination = "/tmp/script.sh" destination = "/tmp/script.sh"
} }
provisioner "remote-exec" { provisioner "remote-exec" {
inline = [ inline = [
"chmod +x /tmp/script.sh", "chmod +x /tmp/script.sh",
"sudo /tmp/script.sh" "sudo /tmp/script.sh",
] ]
} }
connection { connection {
host = "${self.public_ip}" host = coalesce(self.public_ip, self.private_ip)
user = "${var.INSTANCE_USERNAME}" type = "ssh"
private_key = "${file("${var.PATH_TO_PRIVATE_KEY}")}" user = var.INSTANCE_USERNAME
private_key = file(var.PATH_TO_PRIVATE_KEY)
} }
} }
+3 -3
View File
@@ -1,6 +1,6 @@
provider "aws" { provider "aws" {
access_key = "${var.AWS_ACCESS_KEY}" access_key = var.AWS_ACCESS_KEY
secret_key = "${var.AWS_SECRET_KEY}" secret_key = var.AWS_SECRET_KEY
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+11 -3
View File
@@ -1,10 +1,15 @@
variable "AWS_ACCESS_KEY" {} variable "AWS_ACCESS_KEY" {
variable "AWS_SECRET_KEY" {} }
variable "AWS_SECRET_KEY" {
}
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
@@ -15,9 +20,12 @@ variable "AMIS" {
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "INSTANCE_USERNAME" { variable "INSTANCE_USERNAME" {
default = "ubuntu" default = "ubuntu"
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+3 -3
View File
@@ -1,6 +1,6 @@
provider "aws" { provider "aws" {
access_key = "${var.AWS_ACCESS_KEY}" access_key = var.AWS_ACCESS_KEY
secret_key = "${var.AWS_SECRET_KEY}" secret_key = var.AWS_SECRET_KEY
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+14 -4
View File
@@ -1,10 +1,15 @@
variable "AWS_ACCESS_KEY" {} variable "AWS_ACCESS_KEY" {
variable "AWS_SECRET_KEY" {} }
variable "AWS_SECRET_KEY" {
}
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "WIN_AMIS" { variable "WIN_AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-30540427" us-east-1 = "ami-30540427"
us-west-2 = "ami-9f5efbff" us-west-2 = "ami-9f5efbff"
@@ -15,10 +20,15 @@ variable "WIN_AMIS" {
variable "PATH_TO_PRIVATE_KEY" { variable "PATH_TO_PRIVATE_KEY" {
default = "mykey" default = "mykey"
} }
variable "PATH_TO_PUBLIC_KEY" { variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub" default = "mykey.pub"
} }
variable "INSTANCE_USERNAME" { variable "INSTANCE_USERNAME" {
default = "Terraform" default = "Terraform"
} }
variable "INSTANCE_PASSWORD" { }
variable "INSTANCE_PASSWORD" {
}
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+9 -7
View File
@@ -1,13 +1,13 @@
resource "aws_key_pair" "mykey" { resource "aws_key_pair" "mykey" {
key_name = "mykey" key_name = "mykey"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" public_key = file(var.PATH_TO_PUBLIC_KEY)
} }
resource "aws_instance" "win-example" { resource "aws_instance" "win-example" {
ami = "${lookup(var.WIN_AMIS, var.AWS_REGION)}" ami = var.WIN_AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
key_name = "${aws_key_pair.mykey.key_name}" key_name = aws_key_pair.mykey.key_name
user_data = <<EOF user_data = <<EOF
<powershell> <powershell>
net user ${var.INSTANCE_USERNAME} '${var.INSTANCE_PASSWORD}' /add /y net user ${var.INSTANCE_USERNAME} '${var.INSTANCE_PASSWORD}' /add /y
net localgroup administrators ${var.INSTANCE_USERNAME} /add net localgroup administrators ${var.INSTANCE_USERNAME} /add
@@ -27,6 +27,7 @@ net start winrm
</powershell> </powershell>
EOF EOF
provisioner "file" { provisioner "file" {
source = "test.txt" source = "test.txt"
destination = "C:/test.txt" destination = "C:/test.txt"
@@ -35,7 +36,8 @@ EOF
host = coalesce(self.public_ip, self.private_ip) host = coalesce(self.public_ip, self.private_ip)
type = "winrm" type = "winrm"
timeout = "10m" timeout = "10m"
user = "${var.INSTANCE_USERNAME}" user = var.INSTANCE_USERNAME
password = "${var.INSTANCE_PASSWORD}" password = var.INSTANCE_PASSWORD
} }
} }
+5 -3
View File
@@ -1,10 +1,12 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
provisioner "local-exec" { provisioner "local-exec" {
command = "echo ${aws_instance.example.private_ip} >> private_ips.txt" command = "echo ${aws_instance.example.private_ip} >> private_ips.txt"
} }
} }
output "ip" { output "ip" {
value = "${aws_instance.example.public_ip}" value = aws_instance.example.public_ip
} }
+3 -3
View File
@@ -1,6 +1,6 @@
provider "aws" { provider "aws" {
access_key = "${var.AWS_ACCESS_KEY}" access_key = var.AWS_ACCESS_KEY
secret_key = "${var.AWS_SECRET_KEY}" secret_key = var.AWS_SECRET_KEY
region = "${var.AWS_REGION}" region = var.AWS_REGION
} }
+9 -3
View File
@@ -1,13 +1,19 @@
variable "AWS_ACCESS_KEY" {} variable "AWS_ACCESS_KEY" {
variable "AWS_SECRET_KEY" {} }
variable "AWS_SECRET_KEY" {
}
variable "AWS_REGION" { variable "AWS_REGION" {
default = "eu-west-1" default = "eu-west-1"
} }
variable "AMIS" { variable "AMIS" {
type = "map" type = map(string)
default = { default = {
us-east-1 = "ami-13be557e" us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666" us-west-2 = "ami-06b94666"
eu-west-1 = "ami-844e0bf7" eu-west-1 = "ami-844e0bf7"
} }
} }
+4
View File
@@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}
+5 -3
View File
@@ -1,10 +1,12 @@
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}" ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro" instance_type = "t2.micro"
provisioner "local-exec" { provisioner "local-exec" {
command = "echo ${aws_instance.example.private_ip} >> private_ips.txt" command = "echo ${aws_instance.example.private_ip} >> private_ips.txt"
} }
} }
output "ip" { output "ip" {
value = "${aws_instance.example.public_ip}" value = aws_instance.example.public_ip
} }

Some files were not shown because too many files have changed in this diff Show More