mirror of
https://github.com/mr-vercetti/tf-aws-demo.git
synced 2025-01-18 11:25:35 +01:00
Add dynamic resources naming and tfvars example
This commit is contained in:
parent
ffdb539b4d
commit
86fa35e0a5
@ -1,5 +1,5 @@
|
|||||||
resource "aws_security_group" "this" {
|
resource "aws_security_group" "this" {
|
||||||
name = "demo-app-alb-sg"
|
name = "${var.PROJECT_NAME}-alb-sg"
|
||||||
|
|
||||||
vpc_id = var.VPC_ID
|
vpc_id = var.VPC_ID
|
||||||
|
|
||||||
@ -20,20 +20,16 @@ resource "aws_security_group" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_lb" "this" {
|
resource "aws_lb" "this" {
|
||||||
name = "demo-app-alb"
|
name = "${var.PROJECT_NAME}-alb"
|
||||||
|
|
||||||
internal = false
|
internal = false
|
||||||
load_balancer_type = "application"
|
load_balancer_type = "application"
|
||||||
security_groups = [aws_security_group.this.id]
|
security_groups = [aws_security_group.this.id]
|
||||||
subnets = var.ALB_SUBNETS_IDS
|
subnets = var.ALB_SUBNETS_IDS
|
||||||
|
|
||||||
tags = {
|
|
||||||
Project = var.PROJECT_TAG
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_lb_target_group" "this" {
|
resource "aws_lb_target_group" "this" {
|
||||||
name = "demo-app-alb-tg"
|
name = "${var.PROJECT_NAME}-alb-tg"
|
||||||
port = 80
|
port = 80
|
||||||
protocol = "HTTP"
|
protocol = "HTTP"
|
||||||
vpc_id = var.VPC_ID
|
vpc_id = var.VPC_ID
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
variable "VPC_ID" {
|
variable "PROJECT_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "PROJECT_TAG" {
|
variable "VPC_ID" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
resource "aws_security_group" "this" {
|
resource "aws_security_group" "this" {
|
||||||
name = "demo-app-autoscaling-group-sg"
|
name = "${var.PROJECT_NAME}-autoscaling-group-sg"
|
||||||
|
|
||||||
vpc_id = var.VPC_ID
|
vpc_id = var.VPC_ID
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ data "template_file" "bootstrap" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_launch_configuration" "this" {
|
resource "aws_launch_configuration" "this" {
|
||||||
name = "demo-app-launch-configuration"
|
name = "${var.PROJECT_NAME}-launch-configuration"
|
||||||
|
|
||||||
image_id = var.EC2_AMI
|
image_id = var.EC2_AMI
|
||||||
instance_type = var.EC2_TYPE
|
instance_type = var.EC2_TYPE
|
||||||
@ -55,7 +55,7 @@ resource "aws_launch_configuration" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_autoscaling_group" "this" {
|
resource "aws_autoscaling_group" "this" {
|
||||||
name = "demo-app-autoscaling-group"
|
name = "${var.PROJECT_NAME}-autoscaling-group"
|
||||||
|
|
||||||
min_size = var.ASG_MIN_SIZE
|
min_size = var.ASG_MIN_SIZE
|
||||||
max_size = var.ASG_MAX_SIZE
|
max_size = var.ASG_MAX_SIZE
|
||||||
@ -68,14 +68,14 @@ resource "aws_autoscaling_group" "this" {
|
|||||||
|
|
||||||
tag {
|
tag {
|
||||||
key = "Name"
|
key = "Name"
|
||||||
value = "demo-app-instance"
|
value = "${var.PROJECT_NAME}-instance"
|
||||||
propagate_at_launch = true
|
propagate_at_launch = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# asg scale up policy
|
# asg scale up policy
|
||||||
resource "aws_autoscaling_policy" "cpu-policy" {
|
resource "aws_autoscaling_policy" "cpu-policy" {
|
||||||
name = "demo-app-cpu-policy"
|
name = "${var.PROJECT_NAME}-cpu-policy"
|
||||||
|
|
||||||
autoscaling_group_name = aws_autoscaling_group.this.name
|
autoscaling_group_name = aws_autoscaling_group.this.name
|
||||||
adjustment_type = "ChangeInCapacity"
|
adjustment_type = "ChangeInCapacity"
|
||||||
@ -105,7 +105,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-alarm" {
|
|||||||
|
|
||||||
# asg scale down policy
|
# asg scale down policy
|
||||||
resource "aws_autoscaling_policy" "cpu-policy-scaledown" {
|
resource "aws_autoscaling_policy" "cpu-policy-scaledown" {
|
||||||
name = "demo-app-cpu-policy-scaledown"
|
name = "${var.PROJECT_NAME}-cpu-policy-scaledown"
|
||||||
|
|
||||||
autoscaling_group_name = aws_autoscaling_group.this.name
|
autoscaling_group_name = aws_autoscaling_group.this.name
|
||||||
adjustment_type = "ChangeInCapacity"
|
adjustment_type = "ChangeInCapacity"
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
variable "PROJECT_NAME" {
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
# VPC
|
# VPC
|
||||||
variable "VPC_ID" {
|
variable "VPC_ID" {
|
||||||
type = string
|
type = string
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
resource "aws_security_group" "this" {
|
resource "aws_security_group" "this" {
|
||||||
name = "bastion-host-sg"
|
name = "${var.PROJECT_NAME}-bastion-host-sg"
|
||||||
|
|
||||||
vpc_id = var.VPC_ID
|
vpc_id = var.VPC_ID
|
||||||
|
|
||||||
@ -20,18 +20,18 @@ resource "aws_security_group" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_network_interface" "this" {
|
resource "aws_network_interface" "this" {
|
||||||
subnet_id = var.SUBNET_ID
|
subnet_id = var.SUBNET_ID
|
||||||
security_groups = [aws_security_group.this.id]
|
security_groups = [aws_security_group.this.id]
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = "bastion-host-nic"
|
Name = "${var.PROJECT_NAME}-bastion-host-nic"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "this" {
|
resource "aws_instance" "this" {
|
||||||
ami = var.EC2_AMI
|
ami = var.EC2_AMI
|
||||||
instance_type = var.EC2_TYPE
|
instance_type = var.EC2_TYPE
|
||||||
key_name = var.EC2_KEY_NAME
|
key_name = var.EC2_KEY_NAME
|
||||||
|
|
||||||
network_interface {
|
network_interface {
|
||||||
network_interface_id = aws_network_interface.this.id
|
network_interface_id = aws_network_interface.this.id
|
||||||
@ -39,6 +39,6 @@ resource "aws_instance" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = var.EC2_INSTANCE_NAME
|
Name = "${var.PROJECT_NAME}-bastion-host"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,3 @@
|
|||||||
output "bastion_host_name" {
|
|
||||||
value = aws_instance.this.id
|
|
||||||
}
|
|
||||||
|
|
||||||
output "bastion_host_private_ip" {
|
output "bastion_host_private_ip" {
|
||||||
value = aws_instance.this.private_ip
|
value = aws_instance.this.private_ip
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
variable "PROJECT_NAME" {
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
variable "VPC_ID" {
|
variable "VPC_ID" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
@ -11,14 +15,10 @@ variable "EC2_AMI" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable "EC2_TYPE" {
|
variable "EC2_TYPE" {
|
||||||
type = string
|
type = string
|
||||||
default = "t2.micro"
|
default = "t2.micro"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "EC2_INSTANCE_NAME" {
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "EC2_KEY_NAME" {
|
variable "EC2_KEY_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
20
prod/main.tf
20
prod/main.tf
@ -8,7 +8,7 @@ module "vpc" {
|
|||||||
source = "terraform-aws-modules/vpc/aws"
|
source = "terraform-aws-modules/vpc/aws"
|
||||||
version = "3.14.2"
|
version = "3.14.2"
|
||||||
|
|
||||||
name = var.DEMO_VPC_NAME
|
name = "${var.PROJECT_NAME}-vpc"
|
||||||
cidr = var.DEMO_VPC_CIDR
|
cidr = var.DEMO_VPC_CIDR
|
||||||
|
|
||||||
azs = var.DEMO_VPC_AVAILABILITY_ZONES
|
azs = var.DEMO_VPC_AVAILABILITY_ZONES
|
||||||
@ -21,7 +21,6 @@ module "vpc" {
|
|||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Terraform = "true"
|
Terraform = "true"
|
||||||
Project = var.PROJECT_TAG
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ module "vpc_data" {
|
|||||||
source = "../modules/vpc_data"
|
source = "../modules/vpc_data"
|
||||||
depends_on = [module.vpc]
|
depends_on = [module.vpc]
|
||||||
|
|
||||||
VPC_NAME = var.DEMO_VPC_NAME
|
VPC_NAME = "${var.PROJECT_NAME}-vpc"
|
||||||
BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
|
BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,21 +42,24 @@ module "ami_data" {
|
|||||||
module "bastion_host" {
|
module "bastion_host" {
|
||||||
source = "../modules/bastion_host"
|
source = "../modules/bastion_host"
|
||||||
|
|
||||||
|
PROJECT_NAME = var.PROJECT_NAME
|
||||||
|
|
||||||
VPC_ID = module.vpc_data.vpc.id
|
VPC_ID = module.vpc_data.vpc.id
|
||||||
SUBNET_ID = module.vpc_data.bastion_host_subnet.id
|
SUBNET_ID = module.vpc_data.bastion_host_subnet.id
|
||||||
|
|
||||||
EC2_AMI = module.ami_data.amazon_linux.id
|
EC2_AMI = module.ami_data.amazon_linux.id
|
||||||
EC2_TYPE = var.DEMO_BASTION_HOST_TYPE
|
EC2_TYPE = var.DEMO_BASTION_HOST_TYPE
|
||||||
EC2_INSTANCE_NAME = var.DEMO_BASTION_HOST_NAME
|
EC2_KEY_NAME = var.DEMO_BASTION_HOST_KEY_NAME
|
||||||
EC2_KEY_NAME = var.DEMO_BASTION_HOST_KEY_NAME
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create ALB
|
# Create ALB
|
||||||
module "alb" {
|
module "alb" {
|
||||||
source = "../modules/alb"
|
source = "../modules/alb"
|
||||||
|
|
||||||
|
PROJECT_NAME = var.PROJECT_NAME
|
||||||
|
|
||||||
VPC_ID = module.vpc_data.vpc.id
|
VPC_ID = module.vpc_data.vpc.id
|
||||||
PROJECT_TAG = var.PROJECT_TAG
|
|
||||||
ALB_SUBNETS_IDS = module.vpc_data.public_subnets.ids
|
ALB_SUBNETS_IDS = module.vpc_data.public_subnets.ids
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +67,8 @@ module "alb" {
|
|||||||
module "app" {
|
module "app" {
|
||||||
source = "../modules/app"
|
source = "../modules/app"
|
||||||
|
|
||||||
|
PROJECT_NAME = var.PROJECT_NAME
|
||||||
|
|
||||||
VPC_ID = module.vpc_data.vpc.id
|
VPC_ID = module.vpc_data.vpc.id
|
||||||
VPC_SUBNETS_IDS = module.vpc_data.private_subnets.ids
|
VPC_SUBNETS_IDS = module.vpc_data.private_subnets.ids
|
||||||
|
|
||||||
|
20
prod/terraform.tfvars.example
Executable file
20
prod/terraform.tfvars.example
Executable file
@ -0,0 +1,20 @@
|
|||||||
|
# general
|
||||||
|
REGION = "eu-west-3"
|
||||||
|
PROJECT_NAME = "demo"
|
||||||
|
|
||||||
|
# vpc
|
||||||
|
DEMO_VPC_CIDR = "10.0.0.0/24"
|
||||||
|
DEMO_VPC_AVAILABILITY_ZONES = ["eu-west-3a", "eu-west-3b"]
|
||||||
|
DEMO_VPC_PRIVATE_SUBNETS_CIDRS = ["10.0.0.0/28", "10.0.0.16/28"]
|
||||||
|
DEMO_VPC_PUBLIC_SUBNETS_CIDRS = ["10.0.0.32/28", "10.0.0.48/28"]
|
||||||
|
|
||||||
|
# bastion host
|
||||||
|
DEMO_BASTION_HOST_TYPE = "t2.micro"
|
||||||
|
DEMO_BASTION_HOST_KEY_NAME = "aws-key"
|
||||||
|
DEMO_BASTION_HOST_AZ = "eu-west-3a"
|
||||||
|
|
||||||
|
# demo app
|
||||||
|
DEMO_APP_EC2_TYPE = "t2.micro"
|
||||||
|
DEMO_APP_EC2_KEY_NAME = "aws-key"
|
||||||
|
DEMO_APP_ASG_MIN_SIZE = 2
|
||||||
|
DEMO_APP_ASG_MAX_SIZE = 5
|
@ -4,15 +4,11 @@ variable "REGION" {
|
|||||||
default = "eu-west-3"
|
default = "eu-west-3"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "PROJECT_TAG" {
|
variable "PROJECT_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
# vpc
|
# vpc
|
||||||
variable "DEMO_VPC_NAME" {
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "DEMO_VPC_CIDR" {
|
variable "DEMO_VPC_CIDR" {
|
||||||
type = string
|
type = string
|
||||||
default = "10.0.0.0/24"
|
default = "10.0.0.0/24"
|
||||||
@ -36,10 +32,6 @@ variable "DEMO_BASTION_HOST_TYPE" {
|
|||||||
default = "t2.micro"
|
default = "t2.micro"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "DEMO_BASTION_HOST_NAME" {
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "DEMO_BASTION_HOST_KEY_NAME" {
|
variable "DEMO_BASTION_HOST_KEY_NAME" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user