mirror of
https://github.com/farcasclaudiu/terraform-course.git
synced 2026-06-22 07:01:56 +03:00
demo-18b
This commit is contained in:
@@ -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}"]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
@@ -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}")}"
|
||||
}
|
||||
@@ -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}"]
|
||||
}
|
||||
@@ -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}"]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
Reference in New Issue
Block a user