diff --git a/demo-18b/dev/dev.tf b/demo-18b/dev/dev.tf new file mode 100644 index 0000000..728b1d0 --- /dev/null +++ b/demo-18b/dev/dev.tf @@ -0,0 +1,12 @@ +module "main-vpc" { + 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}"] +} diff --git a/demo-18b/dev/provider.tf b/demo-18b/dev/provider.tf new file mode 100644 index 0000000..5606109 --- /dev/null +++ b/demo-18b/dev/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.AWS_REGION}" +} diff --git a/demo-18b/dev/vars.tf b/demo-18b/dev/vars.tf new file mode 100644 index 0000000..7c29d8c --- /dev/null +++ b/demo-18b/dev/vars.tf @@ -0,0 +1,3 @@ +variable "AWS_REGION" { + default = "eu-west-1" +} diff --git a/demo-18b/modules/instances/instance.tf b/demo-18b/modules/instances/instance.tf new file mode 100644 index 0000000..646ab30 --- /dev/null +++ b/demo-18b/modules/instances/instance.tf @@ -0,0 +1,70 @@ +variable "ENV" {} +variable "INSTANCE_TYPE" { default = "t2.micro" } +variable "PUBLIC_SUBNETS" { type = "list" } +variable "VPC_ID" {} +variable "PATH_TO_PUBLIC_KEY" { default = "mykey.pub" } + +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["099720109477"] # Canonical +} + +resource "aws_instance" "instance" { + ami = "${data.aws_ami.ubuntu.id}" + instance_type = "${var.INSTANCE_TYPE}" + + # the VPC subnet + subnet_id = "${var.PUBLIC_SUBNETS[0]}" + + # the security group + vpc_security_group_ids = ["${aws_security_group.allow-ssh.id}"] + + # the public SSH key + key_name = "${aws_key_pair.mykeypair.key_name}" + + tags { + Name = "instance-${var.ENV}" + Environmnent = "${var.ENV}" + } +} + +resource "aws_security_group" "allow-ssh" { + vpc_id = "${var.VPC_ID}" + name = "allow-ssh-${var.ENV}" + 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"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + Name = "allow-ssh" + Environmnent = "${var.ENV}" + } +} + +resource "aws_key_pair" "mykeypair" { + key_name = "mykeypair-${var.ENV}" + public_key = "${file("${path.root}/${var.PATH_TO_PUBLIC_KEY}")}" +} diff --git a/demo-18b/modules/vpc/vpc.tf b/demo-18b/modules/vpc/vpc.tf new file mode 100644 index 0000000..0b435c6 --- /dev/null +++ b/demo-18b/modules/vpc/vpc.tf @@ -0,0 +1,35 @@ +variable "ENV" {} +variable "AWS_REGION" {} + +module "main-vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "vpc-prod-${var.ENV}" + cidr = "10.0.0.0/16" + + azs = ["${var.AWS_REGION}a", "${var.AWS_REGION}b", "${var.AWS_REGION}c"] + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] + + enable_nat_gateway = false + enable_vpn_gateway = false + + tags = { + Terraform = "true" + Environment = "${var.ENV}" + } +} + +output "vpc_id" { + description = "The ID of the VPC" + value = "${module.main-vpc.vpc_id}" +} +output "private_subnets" { + description = "List of IDs of private subnets" + value = ["${module.main-vpc.private_subnets}"] +} + +output "public_subnets" { + description = "List of IDs of public subnets" + value = ["${module.main-vpc.public_subnets}"] +} diff --git a/demo-18b/prod/prod.tf b/demo-18b/prod/prod.tf new file mode 100644 index 0000000..d12b5cf --- /dev/null +++ b/demo-18b/prod/prod.tf @@ -0,0 +1,12 @@ +module "main-vpc" { + 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}"] +} diff --git a/demo-18b/prod/provider.tf b/demo-18b/prod/provider.tf new file mode 100644 index 0000000..5606109 --- /dev/null +++ b/demo-18b/prod/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.AWS_REGION}" +} diff --git a/demo-18b/prod/vars.tf b/demo-18b/prod/vars.tf new file mode 100644 index 0000000..7c29d8c --- /dev/null +++ b/demo-18b/prod/vars.tf @@ -0,0 +1,3 @@ +variable "AWS_REGION" { + default = "eu-west-1" +}