mirror of
https://github.com/farcasclaudiu/terraform-course.git
synced 2026-06-22 05:01:55 +03:00
@@ -3,6 +3,10 @@
|
||||
* 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
|
||||
|
||||
# Compatibility
|
||||
|
||||
* This is the >=terraform-0.12 branch. For compatibility with older versions, use the terraform-0.11 branch.
|
||||
|
||||
# Demo overview
|
||||
Demo Directory | Description
|
||||
------------ | -------------
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
provider "aws" {
|
||||
access_key = "${var.AWS_ACCESS_KEY}"
|
||||
secret_key = "${var.AWS_SECRET_KEY}"
|
||||
region = "${var.AWS_REGION}"
|
||||
access_key = var.AWS_ACCESS_KEY
|
||||
secret_key = var.AWS_SECRET_KEY
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -1,13 +1,19 @@
|
||||
variable "AWS_ACCESS_KEY" {}
|
||||
variable "AWS_SECRET_KEY" {}
|
||||
variable "AWS_ACCESS_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_SECRET_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-0d729a60"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+15
-14
@@ -1,29 +1,30 @@
|
||||
data "template_file" "init-script" {
|
||||
template = "${file("scripts/init.cfg")}"
|
||||
vars {
|
||||
REGION = "${var.AWS_REGION}"
|
||||
template = file("scripts/init.cfg")
|
||||
vars = {
|
||||
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
|
||||
|
||||
part {
|
||||
filename = "init.cfg"
|
||||
content_type = "text/cloud-config"
|
||||
content = "${data.template_file.init-script.rendered}"
|
||||
content = data.template_file.init-script.rendered
|
||||
}
|
||||
|
||||
part {
|
||||
content_type = "text/x-shellscript"
|
||||
content = "${data.template_file.shell-script.rendered}"
|
||||
content = data.template_file.shell-script.rendered
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+14
-15
@@ -1,33 +1,32 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
|
||||
# the VPC subnet
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
subnet_id = aws_subnet.main-public-1.id
|
||||
|
||||
# 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
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
|
||||
# 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" {
|
||||
availability_zone = "eu-west-1a"
|
||||
size = 20
|
||||
type = "gp2"
|
||||
tags {
|
||||
Name = "extra volume data"
|
||||
}
|
||||
availability_zone = "eu-west-1a"
|
||||
size = 20
|
||||
type = "gp2"
|
||||
tags = {
|
||||
Name = "extra volume data"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_volume_attachment" "ebs-volume-1-attachment" {
|
||||
device_name = "${var.INSTANCE_DEVICE_NAME}"
|
||||
volume_id = "${aws_ebs_volume.ebs-volume-1.id}"
|
||||
instance_id = "${aws_instance.example.id}"
|
||||
device_name = var.INSTANCE_DEVICE_NAME
|
||||
volume_id = aws_ebs_volume.ebs-volume-1.id
|
||||
instance_id = aws_instance.example.id
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,4 +1,5 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+13
-12
@@ -1,21 +1,22 @@
|
||||
resource "aws_security_group" "allow-ssh" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-ssh"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-ssh"
|
||||
description = "security group that allows ssh and all egress traffic"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags = {
|
||||
Name = "allow-ssh"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -1,20 +1,25 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
|
||||
variable "INSTANCE_DEVICE_NAME" {
|
||||
default = "/dev/xvdh"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+77
-70
@@ -1,110 +1,117 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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}"
|
||||
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}"
|
||||
subnet_id = aws_subnet.main-public-3.id
|
||||
route_table_id = aws_route_table.main-public.id
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+27
-23
@@ -1,34 +1,38 @@
|
||||
resource "aws_route53_zone" "newtech-academy" {
|
||||
name = "newtech.academy"
|
||||
name = "newtech.academy"
|
||||
}
|
||||
|
||||
resource "aws_route53_record" "server1-record" {
|
||||
zone_id = "${aws_route53_zone.newtech-academy.zone_id}"
|
||||
name = "server1.newtech.academy"
|
||||
type = "A"
|
||||
ttl = "300"
|
||||
records = ["104.236.247.8"]
|
||||
zone_id = aws_route53_zone.newtech-academy.zone_id
|
||||
name = "server1.newtech.academy"
|
||||
type = "A"
|
||||
ttl = "300"
|
||||
records = ["104.236.247.8"]
|
||||
}
|
||||
|
||||
resource "aws_route53_record" "www-record" {
|
||||
zone_id = "${aws_route53_zone.newtech-academy.zone_id}"
|
||||
name = "www.newtech.academy"
|
||||
type = "A"
|
||||
ttl = "300"
|
||||
records = ["104.236.247.8"]
|
||||
zone_id = aws_route53_zone.newtech-academy.zone_id
|
||||
name = "www.newtech.academy"
|
||||
type = "A"
|
||||
ttl = "300"
|
||||
records = ["104.236.247.8"]
|
||||
}
|
||||
|
||||
resource "aws_route53_record" "mail1-record" {
|
||||
zone_id = "${aws_route53_zone.newtech-academy.zone_id}"
|
||||
name = "newtech.academy"
|
||||
type = "MX"
|
||||
ttl = "300"
|
||||
records = [
|
||||
"1 aspmx.l.google.com.",
|
||||
"5 alt1.aspmx.l.google.com.",
|
||||
"5 alt2.aspmx.l.google.com.",
|
||||
"10 aspmx2.googlemail.com.",
|
||||
"10 aspmx3.googlemail.com."
|
||||
]
|
||||
zone_id = aws_route53_zone.newtech-academy.zone_id
|
||||
name = "newtech.academy"
|
||||
type = "MX"
|
||||
ttl = "300"
|
||||
records = [
|
||||
"1 aspmx.l.google.com.",
|
||||
"5 alt1.aspmx.l.google.com.",
|
||||
"5 alt2.aspmx.l.google.com.",
|
||||
"10 aspmx2.googlemail.com.",
|
||||
"10 aspmx3.googlemail.com.",
|
||||
]
|
||||
}
|
||||
|
||||
output "ns-servers" {
|
||||
value = "${aws_route53_zone.newtech-academy.name_servers}"
|
||||
value = aws_route53_zone.newtech-academy.name_servers
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+5
-5
@@ -1,14 +1,14 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
|
||||
# the VPC subnet
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
subnet_id = aws_subnet.main-public-1.id
|
||||
|
||||
# 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
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,4 +1,5 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -1,6 +1,8 @@
|
||||
output "instance" {
|
||||
value = "${aws_instance.example.public_ip}"
|
||||
value = aws_instance.example.public_ip
|
||||
}
|
||||
|
||||
output "rds" {
|
||||
value = "${aws_db_instance.mariadb.endpoint}"
|
||||
value = aws_db_instance.mariadb.endpoint
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+32
-33
@@ -1,40 +1,39 @@
|
||||
resource "aws_db_subnet_group" "mariadb-subnet" {
|
||||
name = "mariadb-subnet"
|
||||
description = "RDS subnet group"
|
||||
subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"]
|
||||
name = "mariadb-subnet"
|
||||
description = "RDS subnet group"
|
||||
subnet_ids = [aws_subnet.main-private-1.id, aws_subnet.main-private-2.id]
|
||||
}
|
||||
|
||||
resource "aws_db_parameter_group" "mariadb-parameters" {
|
||||
name = "mariadb-parameters"
|
||||
family = "mariadb10.1"
|
||||
description = "MariaDB parameter group"
|
||||
name = "mariadb-parameters"
|
||||
family = "mariadb10.1"
|
||||
description = "MariaDB parameter group"
|
||||
|
||||
parameter {
|
||||
name = "max_allowed_packet"
|
||||
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 you’re 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"
|
||||
parameter {
|
||||
name = "max_allowed_packet"
|
||||
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 you’re 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"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-24
@@ -1,43 +1,43 @@
|
||||
resource "aws_security_group" "example-instance" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-ssh"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-ssh"
|
||||
description = "security group that allows ssh and all egress traffic"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags = {
|
||||
Name = "example-instance"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_security_group" "allow-mariadb" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-mariadb"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-mariadb"
|
||||
description = "allow-mariadb"
|
||||
ingress {
|
||||
from_port = 3306
|
||||
to_port = 3306
|
||||
protocol = "tcp"
|
||||
security_groups = ["${aws_security_group.example-instance.id}"] # allowing access from our example instance
|
||||
from_port = 3306
|
||||
to_port = 3306
|
||||
protocol = "tcp"
|
||||
security_groups = [aws_security_group.example-instance.id] # allowing access from our example instance
|
||||
}
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
self = true
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
self = true
|
||||
}
|
||||
tags {
|
||||
tags = {
|
||||
Name = "allow-mariadb"
|
||||
}
|
||||
}
|
||||
|
||||
+8
-2
@@ -1,18 +1,24 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
variable "RDS_PASSWORD" { }
|
||||
|
||||
variable "RDS_PASSWORD" {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+77
-70
@@ -1,110 +1,117 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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}"
|
||||
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}"
|
||||
subnet_id = aws_subnet.main-public-3.id
|
||||
route_table_id = aws_route_table.main-public.id
|
||||
}
|
||||
|
||||
|
||||
+18
-13
@@ -1,28 +1,33 @@
|
||||
# group definition
|
||||
resource "aws_iam_group" "administrators" {
|
||||
name = "administrators"
|
||||
name = "administrators"
|
||||
}
|
||||
|
||||
resource "aws_iam_policy_attachment" "administrators-attach" {
|
||||
name = "administrators-attach"
|
||||
groups = ["${aws_iam_group.administrators.name}"]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
|
||||
name = "administrators-attach"
|
||||
groups = [aws_iam_group.administrators.name]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
|
||||
}
|
||||
|
||||
# user
|
||||
resource "aws_iam_user" "admin1" {
|
||||
name = "admin1"
|
||||
name = "admin1"
|
||||
}
|
||||
|
||||
resource "aws_iam_user" "admin2" {
|
||||
name = "admin2"
|
||||
name = "admin2"
|
||||
}
|
||||
|
||||
resource "aws_iam_group_membership" "administrators-users" {
|
||||
name = "administrators-users"
|
||||
users = [
|
||||
"${aws_iam_user.admin1.name}",
|
||||
"${aws_iam_user.admin2.name}",
|
||||
]
|
||||
group = "${aws_iam_group.administrators.name}"
|
||||
name = "administrators-users"
|
||||
users = [
|
||||
aws_iam_user.admin1.name,
|
||||
aws_iam_user.admin2.name,
|
||||
]
|
||||
group = aws_iam_group.administrators.name
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+9
-7
@@ -1,6 +1,6 @@
|
||||
resource "aws_iam_role" "s3-mybucket-role" {
|
||||
name = "s3-mybucket-role"
|
||||
assume_role_policy = <<EOF
|
||||
name = "s3-mybucket-role"
|
||||
assume_role_policy = <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
@@ -15,17 +15,18 @@ resource "aws_iam_role" "s3-mybucket-role" {
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
resource "aws_iam_instance_profile" "s3-mybucket-role-instanceprofile" {
|
||||
name = "s3-mybucket-role"
|
||||
role = "${aws_iam_role.s3-mybucket-role.name}"
|
||||
name = "s3-mybucket-role"
|
||||
role = aws_iam_role.s3-mybucket-role.name
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "s3-mybucket-role-policy" {
|
||||
name = "s3-mybucket-role-policy"
|
||||
role = "${aws_iam_role.s3-mybucket-role.id}"
|
||||
policy = <<EOF
|
||||
name = "s3-mybucket-role-policy"
|
||||
role = aws_iam_role.s3-mybucket-role.id
|
||||
policy = <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
@@ -42,5 +43,6 @@ resource "aws_iam_role_policy" "s3-mybucket-role-policy" {
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -1,17 +1,17 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
|
||||
# the VPC subnet
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
subnet_id = aws_subnet.main-public-1.id
|
||||
|
||||
# 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
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
|
||||
# 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
@@ -1,4 +1,5 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
output "instance" {
|
||||
value = "${aws_instance.example.public_ip}"
|
||||
value = aws_instance.example.public_ip
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -1,8 +1,9 @@
|
||||
resource "aws_s3_bucket" "b" {
|
||||
bucket = "mybucket-c29df1"
|
||||
acl = "private"
|
||||
bucket = "mybucket-c29df1"
|
||||
acl = "private"
|
||||
|
||||
tags {
|
||||
Name = "mybucket-c29df1"
|
||||
}
|
||||
tags = {
|
||||
Name = "mybucket-c29df1"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-12
@@ -1,21 +1,21 @@
|
||||
resource "aws_security_group" "example-instance" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-ssh"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-ssh"
|
||||
description = "security group that allows ssh and all egress traffic"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags = {
|
||||
Name = "example-instance"
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -1,17 +1,21 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+77
-70
@@ -1,110 +1,117 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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}"
|
||||
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}"
|
||||
subnet_id = aws_subnet.main-public-3.id
|
||||
route_table_id = aws_route_table.main-public.id
|
||||
}
|
||||
|
||||
|
||||
+15
-15
@@ -1,25 +1,25 @@
|
||||
resource "aws_launch_configuration" "example-launchconfig" {
|
||||
name_prefix = "example-launchconfig"
|
||||
image_id = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
instance_type = "t2.micro"
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
security_groups = ["${aws_security_group.allow-ssh.id}"]
|
||||
name_prefix = "example-launchconfig"
|
||||
image_id = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
security_groups = [aws_security_group.allow-ssh.id]
|
||||
}
|
||||
|
||||
resource "aws_autoscaling_group" "example-autoscaling" {
|
||||
name = "example-autoscaling"
|
||||
vpc_zone_identifier = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"]
|
||||
launch_configuration = "${aws_launch_configuration.example-launchconfig.name}"
|
||||
min_size = 1
|
||||
max_size = 2
|
||||
name = "example-autoscaling"
|
||||
vpc_zone_identifier = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
|
||||
launch_configuration = aws_launch_configuration.example-launchconfig.name
|
||||
min_size = 1
|
||||
max_size = 2
|
||||
health_check_grace_period = 300
|
||||
health_check_type = "EC2"
|
||||
force_delete = true
|
||||
health_check_type = "EC2"
|
||||
force_delete = true
|
||||
|
||||
tag {
|
||||
key = "Name"
|
||||
value = "ec2 instance"
|
||||
propagate_at_launch = true
|
||||
key = "Name"
|
||||
value = "ec2 instance"
|
||||
propagate_at_launch = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
resource "aws_autoscaling_policy" "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"
|
||||
scaling_adjustment = "1"
|
||||
cooldown = "300"
|
||||
@@ -21,17 +21,17 @@ resource "aws_cloudwatch_metric_alarm" "example-cpu-alarm" {
|
||||
threshold = "30"
|
||||
|
||||
dimensions = {
|
||||
"AutoScalingGroupName" = "${aws_autoscaling_group.example-autoscaling.name}"
|
||||
"AutoScalingGroupName" = aws_autoscaling_group.example-autoscaling.name
|
||||
}
|
||||
|
||||
actions_enabled = true
|
||||
alarm_actions = ["${aws_autoscaling_policy.example-cpu-policy.arn}"]
|
||||
alarm_actions = [aws_autoscaling_policy.example-cpu-policy.arn]
|
||||
}
|
||||
|
||||
# scale down alarm
|
||||
resource "aws_autoscaling_policy" "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"
|
||||
scaling_adjustment = "-1"
|
||||
cooldown = "300"
|
||||
@@ -50,9 +50,10 @@ resource "aws_cloudwatch_metric_alarm" "example-cpu-alarm-scaledown" {
|
||||
threshold = "5"
|
||||
|
||||
dimensions = {
|
||||
"AutoScalingGroupName" = "${aws_autoscaling_group.example-autoscaling.name}"
|
||||
"AutoScalingGroupName" = aws_autoscaling_group.example-autoscaling.name
|
||||
}
|
||||
|
||||
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
@@ -1,7 +1,8 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
lifecycle {
|
||||
ignore_changes = ["public_key"]
|
||||
ignore_changes = [public_key]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+13
-12
@@ -1,21 +1,22 @@
|
||||
resource "aws_security_group" "allow-ssh" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-ssh"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-ssh"
|
||||
description = "security group that allows ssh and all egress traffic"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags = {
|
||||
Name = "allow-ssh"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
# Uncomment if you want to have autoscaling notifications
|
||||
|
||||
#resource "aws_sns_topic" "example-sns" {
|
||||
# name = "sg-sns"
|
||||
# display_name = "example ASG SNS topic"
|
||||
|
||||
+5
-1
@@ -1,17 +1,21 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+77
-70
@@ -1,110 +1,117 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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}"
|
||||
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}"
|
||||
subnet_id = aws_subnet.main-public-3.id
|
||||
route_table_id = aws_route_table.main-public.id
|
||||
}
|
||||
|
||||
|
||||
+26
-24
@@ -1,28 +1,30 @@
|
||||
resource "aws_launch_configuration" "example-launchconfig" {
|
||||
name_prefix = "example-launchconfig"
|
||||
image_id = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
instance_type = "t2.micro"
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
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"
|
||||
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}"]
|
||||
launch_configuration = "${aws_launch_configuration.example-launchconfig.name}"
|
||||
min_size = 2
|
||||
max_size = 2
|
||||
health_check_grace_period = 300
|
||||
health_check_type = "ELB"
|
||||
load_balancers = ["${aws_elb.my-elb.name}"]
|
||||
force_delete = true
|
||||
|
||||
tag {
|
||||
key = "Name"
|
||||
value = "ec2 instance"
|
||||
propagate_at_launch = true
|
||||
name_prefix = "example-launchconfig"
|
||||
image_id = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
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"
|
||||
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]
|
||||
launch_configuration = aws_launch_configuration.example-launchconfig.name
|
||||
min_size = 2
|
||||
max_size = 2
|
||||
health_check_grace_period = 300
|
||||
health_check_type = "ELB"
|
||||
load_balancers = [aws_elb.my-elb.name]
|
||||
force_delete = true
|
||||
|
||||
tag {
|
||||
key = "Name"
|
||||
value = "ec2 instance"
|
||||
propagate_at_launch = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-14
@@ -1,25 +1,25 @@
|
||||
resource "aws_elb" "my-elb" {
|
||||
name = "my-elb"
|
||||
subnets = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"]
|
||||
security_groups = ["${aws_security_group.elb-securitygroup.id}"]
|
||||
listener {
|
||||
instance_port = 80
|
||||
name = "my-elb"
|
||||
subnets = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
|
||||
security_groups = [aws_security_group.elb-securitygroup.id]
|
||||
listener {
|
||||
instance_port = 80
|
||||
instance_protocol = "http"
|
||||
lb_port = 80
|
||||
lb_protocol = "http"
|
||||
lb_port = 80
|
||||
lb_protocol = "http"
|
||||
}
|
||||
health_check {
|
||||
healthy_threshold = 2
|
||||
healthy_threshold = 2
|
||||
unhealthy_threshold = 2
|
||||
timeout = 3
|
||||
target = "HTTP:80/"
|
||||
interval = 30
|
||||
timeout = 3
|
||||
target = "HTTP:80/"
|
||||
interval = 30
|
||||
}
|
||||
|
||||
cross_zone_load_balancing = true
|
||||
connection_draining = true
|
||||
cross_zone_load_balancing = true
|
||||
connection_draining = true
|
||||
connection_draining_timeout = 400
|
||||
tags {
|
||||
tags = {
|
||||
Name = "my-elb"
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -1,7 +1,8 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
lifecycle {
|
||||
ignore_changes = ["public_key"]
|
||||
ignore_changes = [public_key]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
output "ELB" {
|
||||
value = "${aws_elb.my-elb.dns_name}"
|
||||
value = aws_elb.my-elb.dns_name
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+32
-30
@@ -1,50 +1,52 @@
|
||||
resource "aws_security_group" "myinstance" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "myinstance"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "myinstance"
|
||||
description = "security group for my instance"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
protocol = "tcp"
|
||||
security_groups = ["${aws_security_group.elb-securitygroup.id}"]
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
tags {
|
||||
ingress {
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
protocol = "tcp"
|
||||
security_groups = [aws_security_group.elb-securitygroup.id]
|
||||
}
|
||||
|
||||
tags = {
|
||||
Name = "myinstance"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_security_group" "elb-securitygroup" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "elb"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "elb"
|
||||
description = "security group for load balancer"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags {
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
tags = {
|
||||
Name = "elb"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -1,17 +1,21 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+77
-70
@@ -1,110 +1,117 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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}"
|
||||
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}"
|
||||
subnet_id = aws_subnet.main-public-3.id
|
||||
route_table_id = aws_route_table.main-public.id
|
||||
}
|
||||
|
||||
|
||||
+42
-43
@@ -1,112 +1,111 @@
|
||||
resource "aws_elastic_beanstalk_application" "app" {
|
||||
name = "app"
|
||||
name = "app"
|
||||
description = "app"
|
||||
}
|
||||
|
||||
resource "aws_elastic_beanstalk_environment" "app-prod" {
|
||||
name = "app-prod"
|
||||
application = "${aws_elastic_beanstalk_application.app.name}"
|
||||
name = "app-prod"
|
||||
application = aws_elastic_beanstalk_application.app.name
|
||||
solution_stack_name = "64bit Amazon Linux 2016.09 v2.3.0 running PHP 7.0"
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "VPCId"
|
||||
value = "${aws_vpc.main.id}"
|
||||
value = aws_vpc.main.id
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "Subnets"
|
||||
value = "${aws_subnet.main-private-1.id},${aws_subnet.main-private-2.id}"
|
||||
name = "Subnets"
|
||||
value = "${aws_subnet.main-private-1.id},${aws_subnet.main-private-2.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "AssociatePublicIpAddress"
|
||||
value = "false"
|
||||
name = "AssociatePublicIpAddress"
|
||||
value = "false"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "IamInstanceProfile"
|
||||
value = "${aws_iam_instance_profile.app-ec2-role.name}"
|
||||
name = "IamInstanceProfile"
|
||||
value = aws_iam_instance_profile.app-ec2-role.name
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "SecurityGroups"
|
||||
value = "${aws_security_group.app-prod.id}"
|
||||
name = "SecurityGroups"
|
||||
value = aws_security_group.app-prod.id
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "EC2KeyName"
|
||||
value = "${aws_key_pair.mykeypair.id}"
|
||||
name = "EC2KeyName"
|
||||
value = aws_key_pair.mykeypair.id
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "InstanceType"
|
||||
value = "t2.micro"
|
||||
name = "InstanceType"
|
||||
value = "t2.micro"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:environment"
|
||||
name = "ServiceRole"
|
||||
value = "${aws_iam_role.elasticbeanstalk-service-role.name}"
|
||||
name = "ServiceRole"
|
||||
value = aws_iam_role.elasticbeanstalk-service-role.name
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "ELBScheme"
|
||||
value = "public"
|
||||
name = "ELBScheme"
|
||||
value = "public"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "ELBSubnets"
|
||||
value = "${aws_subnet.main-public-1.id},${aws_subnet.main-public-2.id}"
|
||||
name = "ELBSubnets"
|
||||
value = "${aws_subnet.main-public-1.id},${aws_subnet.main-public-2.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elb:loadbalancer"
|
||||
name = "CrossZone"
|
||||
value = "true"
|
||||
name = "CrossZone"
|
||||
value = "true"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:command"
|
||||
name = "BatchSize"
|
||||
value = "30"
|
||||
name = "BatchSize"
|
||||
value = "30"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:command"
|
||||
name = "BatchSizeType"
|
||||
value = "Percentage"
|
||||
name = "BatchSizeType"
|
||||
value = "Percentage"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:asg"
|
||||
name = "Availability Zones"
|
||||
value = "Any 2"
|
||||
name = "Availability Zones"
|
||||
value = "Any 2"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:asg"
|
||||
name = "MinSize"
|
||||
value = "1"
|
||||
name = "MinSize"
|
||||
value = "1"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:updatepolicy:rollingupdate"
|
||||
name = "RollingUpdateType"
|
||||
value = "Health"
|
||||
name = "RollingUpdateType"
|
||||
value = "Health"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_USERNAME"
|
||||
value = "${aws_db_instance.mariadb.username}"
|
||||
name = "RDS_USERNAME"
|
||||
value = aws_db_instance.mariadb.username
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_PASSWORD"
|
||||
value = "${aws_db_instance.mariadb.password}"
|
||||
name = "RDS_PASSWORD"
|
||||
value = aws_db_instance.mariadb.password
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_DATABASE"
|
||||
value = "mydb"
|
||||
value = "${aws_db_instance.mariadb.name}"
|
||||
name = "RDS_DATABASE"
|
||||
value = aws_db_instance.mariadb.name
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_HOSTNAME"
|
||||
value = "${aws_db_instance.mariadb.endpoint}"
|
||||
name = "RDS_HOSTNAME"
|
||||
value = aws_db_instance.mariadb.endpoint
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+25
-18
@@ -1,7 +1,7 @@
|
||||
# iam roles
|
||||
resource "aws_iam_role" "app-ec2-role" {
|
||||
name = "app-ec2-role"
|
||||
assume_role_policy = <<EOF
|
||||
name = "app-ec2-role"
|
||||
assume_role_policy = <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
@@ -16,16 +16,18 @@ resource "aws_iam_role" "app-ec2-role" {
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
resource "aws_iam_instance_profile" "app-ec2-role" {
|
||||
name = "app-ec2-role"
|
||||
role = "${aws_iam_role.app-ec2-role.name}"
|
||||
name = "app-ec2-role"
|
||||
role = aws_iam_role.app-ec2-role.name
|
||||
}
|
||||
|
||||
# service
|
||||
resource "aws_iam_role" "elasticbeanstalk-service-role" {
|
||||
name = "elasticbeanstalk-service-role"
|
||||
assume_role_policy = <<EOF
|
||||
name = "elasticbeanstalk-service-role"
|
||||
assume_role_policy = <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
@@ -40,26 +42,31 @@ resource "aws_iam_role" "elasticbeanstalk-service-role" {
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
}
|
||||
|
||||
# policies
|
||||
resource "aws_iam_policy_attachment" "app-attach1" {
|
||||
name = "app-attach1"
|
||||
roles = ["${aws_iam_role.app-ec2-role.name}"]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier"
|
||||
name = "app-attach1"
|
||||
roles = [aws_iam_role.app-ec2-role.name]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier"
|
||||
}
|
||||
|
||||
resource "aws_iam_policy_attachment" "app-attach2" {
|
||||
name = "app-attach2"
|
||||
roles = ["${aws_iam_role.app-ec2-role.name}"]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker"
|
||||
name = "app-attach2"
|
||||
roles = [aws_iam_role.app-ec2-role.name]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker"
|
||||
}
|
||||
|
||||
resource "aws_iam_policy_attachment" "app-attach3" {
|
||||
name = "app-attach3"
|
||||
roles = ["${aws_iam_role.app-ec2-role.name}"]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
|
||||
name = "app-attach3"
|
||||
roles = [aws_iam_role.app-ec2-role.name]
|
||||
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
|
||||
}
|
||||
|
||||
resource "aws_iam_policy_attachment" "app-attach4" {
|
||||
name = "app-attach4"
|
||||
roles = ["${aws_iam_role.elasticbeanstalk-service-role.name}"]
|
||||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
|
||||
name = "app-attach4"
|
||||
roles = [aws_iam_role.elasticbeanstalk-service-role.name]
|
||||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -1,7 +1,8 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykeypair"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
lifecycle {
|
||||
ignore_changes = ["public_key"]
|
||||
ignore_changes = [public_key]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
output "eb" {
|
||||
value = "${aws_elastic_beanstalk_environment.app-prod.cname}"
|
||||
value = aws_elastic_beanstalk_environment.app-prod.cname
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
provider "aws" {
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+32
-33
@@ -1,40 +1,39 @@
|
||||
resource "aws_db_subnet_group" "mariadb-subnet" {
|
||||
name = "mariadb-subnet"
|
||||
description = "RDS subnet group"
|
||||
subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"]
|
||||
name = "mariadb-subnet"
|
||||
description = "RDS subnet group"
|
||||
subnet_ids = [aws_subnet.main-private-1.id, aws_subnet.main-private-2.id]
|
||||
}
|
||||
|
||||
resource "aws_db_parameter_group" "mariadb-parameters" {
|
||||
name = "mariadb-params"
|
||||
family = "mariadb10.1"
|
||||
description = "MariaDB parameter group"
|
||||
name = "mariadb-params"
|
||||
family = "mariadb10.1"
|
||||
description = "MariaDB parameter group"
|
||||
|
||||
parameter {
|
||||
name = "max_allowed_packet"
|
||||
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 you’re 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"
|
||||
parameter {
|
||||
name = "max_allowed_packet"
|
||||
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 you’re 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"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-25
@@ -1,44 +1,45 @@
|
||||
resource "aws_security_group" "app-prod" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "application - production"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "application - production"
|
||||
description = "security group for my app"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
|
||||
tags {
|
||||
tags = {
|
||||
Name = "myinstance"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_security_group" "allow-mariadb" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-mariadb"
|
||||
vpc_id = aws_vpc.main.id
|
||||
name = "allow-mariadb"
|
||||
description = "allow-mariadb"
|
||||
ingress {
|
||||
from_port = 3306
|
||||
to_port = 3306
|
||||
protocol = "tcp"
|
||||
security_groups = ["${aws_security_group.app-prod.id}"] # allowing access from our example instance
|
||||
from_port = 3306
|
||||
to_port = 3306
|
||||
protocol = "tcp"
|
||||
security_groups = [aws_security_group.app-prod.id] # allowing access from our example instance
|
||||
}
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
self = true
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
self = true
|
||||
}
|
||||
tags {
|
||||
tags = {
|
||||
Name = "allow-mariadb"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -1,10 +1,15 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
variable "RDS_PASSWORD" {}
|
||||
|
||||
variable "RDS_PASSWORD" {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+106
-93
@@ -1,143 +1,156 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
vpc_id = aws_vpc.main.id
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
tags = {
|
||||
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 {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
vpc_id = aws_vpc.main.id
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
tags = {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
vpc_id = aws_vpc.main.id
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.main-gw.id
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
tags = {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
subnet_id = aws_subnet.main-public-1.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 {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
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 = {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations private
|
||||
resource "aws_route_table_association" "main-private-1-a" {
|
||||
subnet_id = "${aws_subnet.main-private-1.id}"
|
||||
route_table_id = "${aws_route_table.main-private.id}"
|
||||
subnet_id = aws_subnet.main-private-1.id
|
||||
route_table_id = aws_route_table.main-private.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "main-private-2-a" {
|
||||
subnet_id = "${aws_subnet.main-private-2.id}"
|
||||
route_table_id = "${aws_route_table.main-private.id}"
|
||||
subnet_id = aws_subnet.main-private-2.id
|
||||
route_table_id = aws_route_table.main-private.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "main-private-3-a" {
|
||||
subnet_id = "${aws_subnet.main-private-3.id}"
|
||||
route_table_id = "${aws_route_table.main-private.id}"
|
||||
subnet_id = aws_subnet.main-private-3.id
|
||||
route_table_id = aws_route_table.main-private.id
|
||||
}
|
||||
|
||||
# nat gw
|
||||
resource "aws_eip" "nat" {
|
||||
vpc = true
|
||||
vpc = true
|
||||
}
|
||||
|
||||
resource "aws_nat_gateway" "nat-gw" {
|
||||
allocation_id = "${aws_eip.nat.id}"
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
depends_on = ["aws_internet_gateway.main-gw"]
|
||||
allocation_id = aws_eip.nat.id
|
||||
subnet_id = aws_subnet.main-public-1.id
|
||||
depends_on = [aws_internet_gateway.main-gw]
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -15,15 +15,16 @@ data "aws_ami" "ubuntu" {
|
||||
}
|
||||
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${data.aws_ami.ubuntu.id}"
|
||||
ami = data.aws_ami.ubuntu.id
|
||||
instance_type = "t2.micro"
|
||||
|
||||
# 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
|
||||
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
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
resource "aws_security_group" "allow-ssh-prod" {
|
||||
vpc_id = "${module.vpc-prod.vpc_id}"
|
||||
vpc_id = module.vpc-prod.vpc_id
|
||||
name = "allow-ssh"
|
||||
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"]
|
||||
}
|
||||
|
||||
tags {
|
||||
tags = {
|
||||
Name = "allow-ssh"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_security_group" "allow-ssh-dev" {
|
||||
vpc_id = "${module.vpc-dev.vpc_id}"
|
||||
vpc_id = module.vpc-dev.vpc_id
|
||||
name = "allow-ssh"
|
||||
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"]
|
||||
}
|
||||
|
||||
tags {
|
||||
tags = {
|
||||
Name = "allow-ssh"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,3 +13,4 @@ variable "PATH_TO_PUBLIC_KEY" {
|
||||
variable "ENV" {
|
||||
default = "prod"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+5
-2
@@ -1,5 +1,6 @@
|
||||
module "vpc-prod" {
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
version = "2.5.0"
|
||||
|
||||
name = "vpc-prod"
|
||||
cidr = "10.0.0.0/16"
|
||||
@@ -18,7 +19,8 @@ module "vpc-prod" {
|
||||
}
|
||||
|
||||
module "vpc-dev" {
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
version = "2.5.0"
|
||||
|
||||
name = "vpc-dev"
|
||||
cidr = "10.0.0.0/16"
|
||||
@@ -35,3 +37,4 @@ module "vpc-dev" {
|
||||
Environment = "dev"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-7
@@ -1,12 +1,13 @@
|
||||
module "main-vpc" {
|
||||
source = "../modules/vpc"
|
||||
ENV = "dev"
|
||||
AWS_REGION = "${var.AWS_REGION}"
|
||||
source = "../modules/vpc"
|
||||
ENV = "dev"
|
||||
AWS_REGION = var.AWS_REGION
|
||||
}
|
||||
|
||||
module "instances" {
|
||||
source = "../modules/instances"
|
||||
ENV = "dev"
|
||||
VPC_ID = "${module.main-vpc.vpc_id}"
|
||||
PUBLIC_SUBNETS = ["${module.main-vpc.public_subnets}"]
|
||||
source = "../modules/instances"
|
||||
ENV = "dev"
|
||||
VPC_ID = module.main-vpc.vpc_id
|
||||
PUBLIC_SUBNETS = module.main-vpc.public_subnets
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
variable "ENV" {}
|
||||
variable "ENV" {
|
||||
}
|
||||
|
||||
variable "INSTANCE_TYPE" {
|
||||
default = "t2.micro"
|
||||
}
|
||||
|
||||
variable "PUBLIC_SUBNETS" {
|
||||
type = "list"
|
||||
type = list
|
||||
}
|
||||
|
||||
variable "VPC_ID" {}
|
||||
variable "VPC_ID" {
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
@@ -31,26 +33,26 @@ data "aws_ami" "ubuntu" {
|
||||
}
|
||||
|
||||
resource "aws_instance" "instance" {
|
||||
ami = "${data.aws_ami.ubuntu.id}"
|
||||
instance_type = "${var.INSTANCE_TYPE}"
|
||||
ami = data.aws_ami.ubuntu.id
|
||||
instance_type = var.INSTANCE_TYPE
|
||||
|
||||
# the VPC subnet
|
||||
subnet_id = "${var.PUBLIC_SUBNETS[0]}"
|
||||
subnet_id = element(var.PUBLIC_SUBNETS, 0)
|
||||
|
||||
# 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
|
||||
key_name = "${aws_key_pair.mykeypair.key_name}"
|
||||
key_name = aws_key_pair.mykeypair.key_name
|
||||
|
||||
tags {
|
||||
tags = {
|
||||
Name = "instance-${var.ENV}"
|
||||
Environmnent = "${var.ENV}"
|
||||
Environmnent = var.ENV
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_security_group" "allow-ssh" {
|
||||
vpc_id = "${var.VPC_ID}"
|
||||
vpc_id = var.VPC_ID
|
||||
name = "allow-ssh-${var.ENV}"
|
||||
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"]
|
||||
}
|
||||
|
||||
tags {
|
||||
tags = {
|
||||
Name = "allow-ssh"
|
||||
Environmnent = "${var.ENV}"
|
||||
Environmnent = var.ENV
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
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}")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
variable "ENV" {}
|
||||
variable "AWS_REGION" {}
|
||||
variable "ENV" {
|
||||
}
|
||||
|
||||
variable "AWS_REGION" {
|
||||
}
|
||||
|
||||
module "main-vpc" {
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
@@ -16,21 +19,22 @@ module "main-vpc" {
|
||||
|
||||
tags = {
|
||||
Terraform = "true"
|
||||
Environment = "${var.ENV}"
|
||||
Environment = var.ENV
|
||||
}
|
||||
}
|
||||
|
||||
output "vpc_id" {
|
||||
description = "The ID of the VPC"
|
||||
value = "${module.main-vpc.vpc_id}"
|
||||
value = module.main-vpc.vpc_id
|
||||
}
|
||||
|
||||
output "private_subnets" {
|
||||
description = "List of IDs of private subnets"
|
||||
value = ["${module.main-vpc.private_subnets}"]
|
||||
value = module.main-vpc.private_subnets
|
||||
}
|
||||
|
||||
output "public_subnets" {
|
||||
description = "List of IDs of public subnets"
|
||||
value = ["${module.main-vpc.public_subnets}"]
|
||||
value = module.main-vpc.public_subnets
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
module "main-vpc" {
|
||||
source = "../modules/vpc"
|
||||
ENV = "prod"
|
||||
AWS_REGION = "${var.AWS_REGION}"
|
||||
source = "../modules/vpc"
|
||||
ENV = "prod"
|
||||
AWS_REGION = var.AWS_REGION
|
||||
}
|
||||
|
||||
module "instances" {
|
||||
source = "../modules/instances"
|
||||
ENV = "prod"
|
||||
VPC_ID = "${module.main-vpc.vpc_id}"
|
||||
PUBLIC_SUBNETS = ["${module.main-vpc.public_subnets}"]
|
||||
source = "../modules/instances"
|
||||
ENV = "prod"
|
||||
VPC_ID = module.main-vpc.vpc_id
|
||||
PUBLIC_SUBNETS = module.main-vpc.public_subnets
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+11
-9
@@ -1,26 +1,28 @@
|
||||
resource "aws_key_pair" "mykey" {
|
||||
key_name = "mykey"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykey"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
key_name = "${aws_key_pair.mykey.key_name}"
|
||||
key_name = aws_key_pair.mykey.key_name
|
||||
|
||||
provisioner "file" {
|
||||
source = "script.sh"
|
||||
source = "script.sh"
|
||||
destination = "/tmp/script.sh"
|
||||
}
|
||||
provisioner "remote-exec" {
|
||||
inline = [
|
||||
"chmod +x /tmp/script.sh",
|
||||
"sudo /tmp/script.sh"
|
||||
"sudo /tmp/script.sh",
|
||||
]
|
||||
}
|
||||
connection {
|
||||
host = "${self.public_ip}"
|
||||
user = "${var.INSTANCE_USERNAME}"
|
||||
private_key = "${file("${var.PATH_TO_PRIVATE_KEY}")}"
|
||||
host = coalesce(self.public_ip, self.private_ip)
|
||||
type = "ssh"
|
||||
user = var.INSTANCE_USERNAME
|
||||
private_key = file(var.PATH_TO_PRIVATE_KEY)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
provider "aws" {
|
||||
access_key = "${var.AWS_ACCESS_KEY}"
|
||||
secret_key = "${var.AWS_SECRET_KEY}"
|
||||
region = "${var.AWS_REGION}"
|
||||
access_key = var.AWS_ACCESS_KEY
|
||||
secret_key = var.AWS_SECRET_KEY
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+11
-3
@@ -1,10 +1,15 @@
|
||||
variable "AWS_ACCESS_KEY" {}
|
||||
variable "AWS_SECRET_KEY" {}
|
||||
variable "AWS_ACCESS_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_SECRET_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
@@ -15,9 +20,12 @@ variable "AMIS" {
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "INSTANCE_USERNAME" {
|
||||
default = "ubuntu"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
provider "aws" {
|
||||
access_key = "${var.AWS_ACCESS_KEY}"
|
||||
secret_key = "${var.AWS_SECRET_KEY}"
|
||||
region = "${var.AWS_REGION}"
|
||||
access_key = var.AWS_ACCESS_KEY
|
||||
secret_key = var.AWS_SECRET_KEY
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+14
-4
@@ -1,10 +1,15 @@
|
||||
variable "AWS_ACCESS_KEY" {}
|
||||
variable "AWS_SECRET_KEY" {}
|
||||
variable "AWS_ACCESS_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_SECRET_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "WIN_AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-30540427"
|
||||
us-west-2 = "ami-9f5efbff"
|
||||
@@ -15,10 +20,15 @@ variable "WIN_AMIS" {
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
|
||||
variable "INSTANCE_USERNAME" {
|
||||
default = "Terraform"
|
||||
}
|
||||
variable "INSTANCE_PASSWORD" { }
|
||||
|
||||
variable "INSTANCE_PASSWORD" {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
resource "aws_key_pair" "mykey" {
|
||||
key_name = "mykey"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
key_name = "mykey"
|
||||
public_key = file(var.PATH_TO_PUBLIC_KEY)
|
||||
}
|
||||
|
||||
resource "aws_instance" "win-example" {
|
||||
ami = "${lookup(var.WIN_AMIS, var.AWS_REGION)}"
|
||||
ami = var.WIN_AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
key_name = "${aws_key_pair.mykey.key_name}"
|
||||
user_data = <<EOF
|
||||
key_name = aws_key_pair.mykey.key_name
|
||||
user_data = <<EOF
|
||||
<powershell>
|
||||
net user ${var.INSTANCE_USERNAME} '${var.INSTANCE_PASSWORD}' /add /y
|
||||
net localgroup administrators ${var.INSTANCE_USERNAME} /add
|
||||
@@ -27,6 +27,7 @@ net start winrm
|
||||
</powershell>
|
||||
EOF
|
||||
|
||||
|
||||
provisioner "file" {
|
||||
source = "test.txt"
|
||||
destination = "C:/test.txt"
|
||||
@@ -35,7 +36,8 @@ EOF
|
||||
host = coalesce(self.public_ip, self.private_ip)
|
||||
type = "winrm"
|
||||
timeout = "10m"
|
||||
user = "${var.INSTANCE_USERNAME}"
|
||||
password = "${var.INSTANCE_PASSWORD}"
|
||||
user = var.INSTANCE_USERNAME
|
||||
password = var.INSTANCE_PASSWORD
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -1,10 +1,12 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
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" {
|
||||
value = "${aws_instance.example.public_ip}"
|
||||
value = aws_instance.example.public_ip
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
provider "aws" {
|
||||
access_key = "${var.AWS_ACCESS_KEY}"
|
||||
secret_key = "${var.AWS_SECRET_KEY}"
|
||||
region = "${var.AWS_REGION}"
|
||||
access_key = var.AWS_ACCESS_KEY
|
||||
secret_key = var.AWS_SECRET_KEY
|
||||
region = var.AWS_REGION
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -1,13 +1,19 @@
|
||||
variable "AWS_ACCESS_KEY" {}
|
||||
variable "AWS_SECRET_KEY" {}
|
||||
variable "AWS_ACCESS_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_SECRET_KEY" {
|
||||
}
|
||||
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
|
||||
variable "AMIS" {
|
||||
type = "map"
|
||||
type = map(string)
|
||||
default = {
|
||||
us-east-1 = "ami-13be557e"
|
||||
us-west-2 = "ami-06b94666"
|
||||
eu-west-1 = "ami-844e0bf7"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
}
|
||||
+5
-3
@@ -1,10 +1,12 @@
|
||||
resource "aws_instance" "example" {
|
||||
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
|
||||
ami = var.AMIS[var.AWS_REGION]
|
||||
instance_type = "t2.micro"
|
||||
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" {
|
||||
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
Reference in New Issue
Block a user