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" {
|
||||
name = "demo-app-alb-sg"
|
||||
name = "${var.PROJECT_NAME}-alb-sg"
|
||||
|
||||
vpc_id = var.VPC_ID
|
||||
|
||||
@ -20,20 +20,16 @@ resource "aws_security_group" "this" {
|
||||
}
|
||||
|
||||
resource "aws_lb" "this" {
|
||||
name = "demo-app-alb"
|
||||
|
||||
name = "${var.PROJECT_NAME}-alb"
|
||||
|
||||
internal = false
|
||||
load_balancer_type = "application"
|
||||
security_groups = [aws_security_group.this.id]
|
||||
subnets = var.ALB_SUBNETS_IDS
|
||||
|
||||
tags = {
|
||||
Project = var.PROJECT_TAG
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_lb_target_group" "this" {
|
||||
name = "demo-app-alb-tg"
|
||||
name = "${var.PROJECT_NAME}-alb-tg"
|
||||
port = 80
|
||||
protocol = "HTTP"
|
||||
vpc_id = var.VPC_ID
|
||||
|
@ -1,8 +1,8 @@
|
||||
variable "VPC_ID" {
|
||||
variable "PROJECT_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "PROJECT_TAG" {
|
||||
variable "VPC_ID" {
|
||||
type = string
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
resource "aws_security_group" "this" {
|
||||
name = "demo-app-autoscaling-group-sg"
|
||||
name = "${var.PROJECT_NAME}-autoscaling-group-sg"
|
||||
|
||||
vpc_id = var.VPC_ID
|
||||
|
||||
@ -40,7 +40,7 @@ data "template_file" "bootstrap" {
|
||||
}
|
||||
|
||||
resource "aws_launch_configuration" "this" {
|
||||
name = "demo-app-launch-configuration"
|
||||
name = "${var.PROJECT_NAME}-launch-configuration"
|
||||
|
||||
image_id = var.EC2_AMI
|
||||
instance_type = var.EC2_TYPE
|
||||
@ -55,7 +55,7 @@ resource "aws_launch_configuration" "this" {
|
||||
}
|
||||
|
||||
resource "aws_autoscaling_group" "this" {
|
||||
name = "demo-app-autoscaling-group"
|
||||
name = "${var.PROJECT_NAME}-autoscaling-group"
|
||||
|
||||
min_size = var.ASG_MIN_SIZE
|
||||
max_size = var.ASG_MAX_SIZE
|
||||
@ -68,14 +68,14 @@ resource "aws_autoscaling_group" "this" {
|
||||
|
||||
tag {
|
||||
key = "Name"
|
||||
value = "demo-app-instance"
|
||||
value = "${var.PROJECT_NAME}-instance"
|
||||
propagate_at_launch = true
|
||||
}
|
||||
}
|
||||
|
||||
# asg scale up 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
|
||||
adjustment_type = "ChangeInCapacity"
|
||||
@ -105,7 +105,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-alarm" {
|
||||
|
||||
# asg scale down policy
|
||||
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
|
||||
adjustment_type = "ChangeInCapacity"
|
||||
|
@ -1,3 +1,7 @@
|
||||
variable "PROJECT_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
# VPC
|
||||
variable "VPC_ID" {
|
||||
type = string
|
||||
|
@ -1,5 +1,5 @@
|
||||
resource "aws_security_group" "this" {
|
||||
name = "bastion-host-sg"
|
||||
name = "${var.PROJECT_NAME}-bastion-host-sg"
|
||||
|
||||
vpc_id = var.VPC_ID
|
||||
|
||||
@ -20,18 +20,18 @@ resource "aws_security_group" "this" {
|
||||
}
|
||||
|
||||
resource "aws_network_interface" "this" {
|
||||
subnet_id = var.SUBNET_ID
|
||||
subnet_id = var.SUBNET_ID
|
||||
security_groups = [aws_security_group.this.id]
|
||||
|
||||
tags = {
|
||||
Name = "bastion-host-nic"
|
||||
Name = "${var.PROJECT_NAME}-bastion-host-nic"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_instance" "this" {
|
||||
ami = var.EC2_AMI
|
||||
instance_type = var.EC2_TYPE
|
||||
key_name = var.EC2_KEY_NAME
|
||||
key_name = var.EC2_KEY_NAME
|
||||
|
||||
network_interface {
|
||||
network_interface_id = aws_network_interface.this.id
|
||||
@ -39,6 +39,6 @@ resource "aws_instance" "this" {
|
||||
}
|
||||
|
||||
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" {
|
||||
value = aws_instance.this.private_ip
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
variable "PROJECT_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "VPC_ID" {
|
||||
type = string
|
||||
}
|
||||
@ -11,14 +15,10 @@ variable "EC2_AMI" {
|
||||
}
|
||||
|
||||
variable "EC2_TYPE" {
|
||||
type = string
|
||||
type = string
|
||||
default = "t2.micro"
|
||||
}
|
||||
|
||||
variable "EC2_INSTANCE_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "EC2_KEY_NAME" {
|
||||
type = string
|
||||
}
|
20
prod/main.tf
20
prod/main.tf
@ -8,7 +8,7 @@ module "vpc" {
|
||||
source = "terraform-aws-modules/vpc/aws"
|
||||
version = "3.14.2"
|
||||
|
||||
name = var.DEMO_VPC_NAME
|
||||
name = "${var.PROJECT_NAME}-vpc"
|
||||
cidr = var.DEMO_VPC_CIDR
|
||||
|
||||
azs = var.DEMO_VPC_AVAILABILITY_ZONES
|
||||
@ -21,7 +21,6 @@ module "vpc" {
|
||||
|
||||
tags = {
|
||||
Terraform = "true"
|
||||
Project = var.PROJECT_TAG
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +29,7 @@ module "vpc_data" {
|
||||
source = "../modules/vpc_data"
|
||||
depends_on = [module.vpc]
|
||||
|
||||
VPC_NAME = var.DEMO_VPC_NAME
|
||||
VPC_NAME = "${var.PROJECT_NAME}-vpc"
|
||||
BASTION_HOST_AZ = var.DEMO_BASTION_HOST_AZ
|
||||
}
|
||||
|
||||
@ -43,21 +42,24 @@ module "ami_data" {
|
||||
module "bastion_host" {
|
||||
source = "../modules/bastion_host"
|
||||
|
||||
PROJECT_NAME = var.PROJECT_NAME
|
||||
|
||||
VPC_ID = module.vpc_data.vpc.id
|
||||
SUBNET_ID = module.vpc_data.bastion_host_subnet.id
|
||||
|
||||
EC2_AMI = module.ami_data.amazon_linux.id
|
||||
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_AMI = module.ami_data.amazon_linux.id
|
||||
EC2_TYPE = var.DEMO_BASTION_HOST_TYPE
|
||||
EC2_KEY_NAME = var.DEMO_BASTION_HOST_KEY_NAME
|
||||
}
|
||||
|
||||
# Create ALB
|
||||
module "alb" {
|
||||
source = "../modules/alb"
|
||||
|
||||
PROJECT_NAME = var.PROJECT_NAME
|
||||
|
||||
VPC_ID = module.vpc_data.vpc.id
|
||||
PROJECT_TAG = var.PROJECT_TAG
|
||||
|
||||
ALB_SUBNETS_IDS = module.vpc_data.public_subnets.ids
|
||||
}
|
||||
|
||||
@ -65,6 +67,8 @@ module "alb" {
|
||||
module "app" {
|
||||
source = "../modules/app"
|
||||
|
||||
PROJECT_NAME = var.PROJECT_NAME
|
||||
|
||||
VPC_ID = module.vpc_data.vpc.id
|
||||
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"
|
||||
}
|
||||
|
||||
variable "PROJECT_TAG" {
|
||||
variable "PROJECT_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
# vpc
|
||||
variable "DEMO_VPC_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "DEMO_VPC_CIDR" {
|
||||
type = string
|
||||
default = "10.0.0.0/24"
|
||||
@ -36,10 +32,6 @@ variable "DEMO_BASTION_HOST_TYPE" {
|
||||
default = "t2.micro"
|
||||
}
|
||||
|
||||
variable "DEMO_BASTION_HOST_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "DEMO_BASTION_HOST_KEY_NAME" {
|
||||
type = string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user