mirror of
				https://github.com/mr-vercetti/tf-aws-demo.git
				synced 2025-11-04 01:15:46 +01:00 
			
		
		
		
	Add dynamic resources naming and tfvars example
This commit is contained in:
		@@ -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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user