mirror of
https://github.com/ghndrx/terraform.git
synced 2026-02-10 06:45:01 +00:00
mv AWS -> aws/aws_simple
This commit is contained in:
96
aws/aws_simple/ec2/ec2.tf
Normal file
96
aws/aws_simple/ec2/ec2.tf
Normal file
@@ -0,0 +1,96 @@
|
||||
# Define the VPC and subnets data sources
|
||||
data "aws_vpc" "vpc" {
|
||||
id = data.aws_subnet.subnet1.vpc_id
|
||||
}
|
||||
|
||||
module "vpc_subnets" {
|
||||
source = "../vpc/subnets"
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet1" {
|
||||
id = module.vpc_subnets.subnet_ids[0]
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet2" {
|
||||
id = module.vpc_subnets.subnet_ids[1]
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet3" {
|
||||
id = module.vpc_subnets.subnet_ids[2]
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet4" {
|
||||
id = module.vpc_subnets.subnet_ids[3]
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet5" {
|
||||
id = module.vpc_subnets.subnet_ids[4]
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnet6" {
|
||||
id = module.vpc_subnets.subnet_ids[5]
|
||||
}
|
||||
|
||||
# Create a security group for the EC2 instance
|
||||
resource "aws_security_group" "instance" {
|
||||
name_prefix = "instance-"
|
||||
vpc_id = data.aws_vpc.vpc.id
|
||||
|
||||
ingress {
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 443
|
||||
to_port = 443
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
tags = {
|
||||
Name = "instance-security-group"
|
||||
}
|
||||
}
|
||||
|
||||
# Define the Launch Configuration
|
||||
resource "aws_launch_configuration" "lc" {
|
||||
name_prefix = "lc-"
|
||||
image_id = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
|
||||
instance_type = "t3.small"
|
||||
security_groups = [
|
||||
aws_security_group.instance.id
|
||||
]
|
||||
user_data = file("${path.module}/user-data.sh")
|
||||
root_block_device {
|
||||
volume_size = 20
|
||||
}
|
||||
}
|
||||
|
||||
# Define the Auto Scaling Group
|
||||
resource "aws_autoscaling_group" "asg" {
|
||||
name_prefix = "asg-"
|
||||
launch_configuration = aws_launch_configuration.lc.id
|
||||
vpc_zone_identifier = [
|
||||
data.aws_subnet.subnet1.id,
|
||||
data.aws_subnet.subnet2.id
|
||||
]
|
||||
min_size = var.min_size
|
||||
max_size = var.max_size
|
||||
desired_capacity = var.min_size
|
||||
health_check_grace_period = 300
|
||||
health_check_type = "EC2"
|
||||
termination_policies = ["OldestInstance"]
|
||||
tag {
|
||||
key = "Name"
|
||||
value = "asg-instance"
|
||||
propagate_at_launch = true
|
||||
}
|
||||
}
|
||||
|
||||
# Output the instance public IP address
|
||||
output "public_ip" {
|
||||
value = aws_autoscaling_group.asg.instances[0].public_ip
|
||||
}
|
||||
2
aws/aws_simple/ec2/terraform.tfvars
Normal file
2
aws/aws_simple/ec2/terraform.tfvars
Normal file
@@ -0,0 +1,2 @@
|
||||
min_size = 1
|
||||
max_size = 10
|
||||
28
aws/aws_simple/ec2/user-data.sh
Normal file
28
aws/aws_simple/ec2/user-data.sh
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Install necessary packages
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y git amazon-efs-utils vim-nox neofetch htop tmux curl wget
|
||||
|
||||
# Create new user with sudo privileges
|
||||
sudo useradd -m -s /bin/bash greg
|
||||
sudo usermod -aG sudo greg
|
||||
|
||||
# Add authorized keys for your public key
|
||||
sudo mkdir -p /home/greg/.ssh
|
||||
sudo touch /home/greg/.ssh/authorized_keys
|
||||
sudo chmod 700 /home/greg/.ssh
|
||||
sudo chmod 600 /home/greg/.ssh/authorized_keys
|
||||
sudo chown -R greg:greg /home/greg/.ssh
|
||||
|
||||
# Add your public key to authorized_keys
|
||||
sudo echo "YOUR_PUBLIC_KEY" >> /home/greg/.ssh/authorized_keys
|
||||
|
||||
# Set hostname
|
||||
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
|
||||
AVAILABILITY_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
|
||||
HOSTNAME="$INSTANCE_ID-$AVAILABILITY_ZONE"
|
||||
sudo hostnamectl set-hostname $HOSTNAME
|
||||
|
||||
# Run cloud-init.sh script
|
||||
sudo sh /path/to/cloud-init.sh
|
||||
13
aws/aws_simple/ec2/variables.tf
Normal file
13
aws/aws_simple/ec2/variables.tf
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
# Define variables
|
||||
variable "min_size" {
|
||||
type = number
|
||||
description = "Minimum number of instances in the Auto Scaling Group"
|
||||
default = 1
|
||||
}
|
||||
|
||||
variable "max_size" {
|
||||
type = number
|
||||
description = "Maximum number of instances in the Auto Scaling Group"
|
||||
default = 1
|
||||
}
|
||||
0
aws/aws_simple/efs/main.tf
Normal file
0
aws/aws_simple/efs/main.tf
Normal file
0
aws/aws_simple/elb/main.tf
Normal file
0
aws/aws_simple/elb/main.tf
Normal file
40
aws/aws_simple/main.tf
Normal file
40
aws/aws_simple/main.tf
Normal file
@@ -0,0 +1,40 @@
|
||||
# Define provider
|
||||
provider "aws" {
|
||||
region = var.aws_region
|
||||
access_key = var.aws_access_key
|
||||
secret_key = var.aws_secret_key
|
||||
}
|
||||
|
||||
# Define modules
|
||||
module "vpc_us_west" {
|
||||
source = "./modules/vpc"
|
||||
}
|
||||
|
||||
module "subnet_us_west" {
|
||||
source = "./modules/subnet"
|
||||
vpc_id = module.vpc_us_west.vpc_id
|
||||
region = "us-west-2"
|
||||
}
|
||||
|
||||
module "vpc_us_east" {
|
||||
source = "./modules/vpc"
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
module "subnet_us_east" {
|
||||
source = "./modules/subnet"
|
||||
vpc_id = module.vpc_us_east.vpc_id
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
module "ec2" {
|
||||
source = "./modules/ec2"
|
||||
}
|
||||
|
||||
module "elb" {
|
||||
source = "./modules/elb"
|
||||
}
|
||||
|
||||
module "efs" {
|
||||
source = "./modules/efs"
|
||||
}
|
||||
15
aws/aws_simple/readme.md
Normal file
15
aws/aws_simple/readme.md
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
|
||||
my-terraform-project/
|
||||
├── main.tf
|
||||
└── modules/
|
||||
├── EFS/
|
||||
│ └── main.tf
|
||||
├── ELB/
|
||||
│ └── main.tf
|
||||
├── EC2/
|
||||
│ └── main.tf
|
||||
├── S3/
|
||||
│ └── main.tf
|
||||
└── VPC/
|
||||
└── main.tf
|
||||
3
aws/aws_simple/terraform.tfvars
Normal file
3
aws/aws_simple/terraform.tfvars
Normal file
@@ -0,0 +1,3 @@
|
||||
aws_secret_key = ""
|
||||
aws_access_key = ""
|
||||
aws_region = "us-west-2"
|
||||
16
aws/aws_simple/variables.tf
Normal file
16
aws/aws_simple/variables.tf
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
# Define variables
|
||||
variable "aws_region" {
|
||||
type = string
|
||||
default = "us-west-2"
|
||||
}
|
||||
|
||||
variable "aws_access_key" {
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "aws_secret_key" {
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
31
aws/aws_simple/vpc/subnets/us-east/subnets-east.tf
Normal file
31
aws/aws_simple/vpc/subnets/us-east/subnets-east.tf
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
provider "aws" {
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
resource "aws_subnet" "us_east_subnet_1" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_east_subnet_1_cidr_block
|
||||
availability_zone = var.us_east_subnet_1_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_east_subnet_1_az}_subnet"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "us_east_subnet_2" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_east_subnet_2_cidr_block
|
||||
availability_zone = var.us_east_subnet_2_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_east_subnet_2_az}_subnet"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "us_east_subnet_3" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_east_subnet_3_cidr_block
|
||||
availability_zone = var.us_east_subnet_3_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_east_subnet_3_az}_subnet"
|
||||
}
|
||||
}
|
||||
10
aws/aws_simple/vpc/subnets/us-east/terraform.tfvars
Normal file
10
aws/aws_simple/vpc/subnets/us-east/terraform.tfvars
Normal file
@@ -0,0 +1,10 @@
|
||||
us_east_subnet_1_az = "us-east-1a"
|
||||
us_east_subnet_1_cidr_block = "10.0.4.0/24"
|
||||
|
||||
us_east_subnet_2_az = "us-east-1b"
|
||||
us_east_subnet_2_cidr_block = "10.0.5.0/24"
|
||||
|
||||
us_east_subnet_3_az = "us-east-1c"
|
||||
us_east_subnet_3_cidr_block = "10.0.6.0/24"
|
||||
|
||||
|
||||
28
aws/aws_simple/vpc/subnets/us-east/variables.tf
Normal file
28
aws/aws_simple/vpc/subnets/us-east/variables.tf
Normal file
@@ -0,0 +1,28 @@
|
||||
variable "us_east_subnet_1_az" {
|
||||
default = "us-east-1a"
|
||||
}
|
||||
|
||||
variable "us_east_subnet_2_az" {
|
||||
default = "us-east-1b"
|
||||
}
|
||||
|
||||
variable "us_east_subnet_3_az" {
|
||||
default = "us-east-1c"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
default = "us-east-1"
|
||||
}
|
||||
|
||||
|
||||
variable "us_east_subnet_1_cidr_block" {
|
||||
default = "10.0.4.0/24"
|
||||
}
|
||||
|
||||
variable "us_east_subnet_2_cidr_block" {
|
||||
default = "10.0.5.0/24"
|
||||
}
|
||||
|
||||
variable "us_east_subnet_3_cidr_block" {
|
||||
default = "10.0.6.0/24"
|
||||
}
|
||||
27
aws/aws_simple/vpc/subnets/us-west/subnets-west.tf
Normal file
27
aws/aws_simple/vpc/subnets/us-west/subnets-west.tf
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
resource "aws_subnet" "us_west_subnet_1" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_west_subnet_1_cidr_block
|
||||
availability_zone = var.us_west_subnet_1_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_west_subnet_1_az}_subnet"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "us_west_subnet_2" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_west_subnet_2_cidr_block
|
||||
availability_zone = var.us_west_subnet_2_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_west_subnet_2_az}_subnet"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "us_west_subnet_3" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
cidr_block = var.us_west_subnet_3_cidr_block
|
||||
availability_zone = var.us_west_subnet_3_az
|
||||
tags = {
|
||||
Name = "${var.region}_${var.us_west_subnet_3_az}_subnet"
|
||||
}
|
||||
}
|
||||
10
aws/aws_simple/vpc/subnets/us-west/terraform.tfvars
Normal file
10
aws/aws_simple/vpc/subnets/us-west/terraform.tfvars
Normal file
@@ -0,0 +1,10 @@
|
||||
region = "us-west-2"
|
||||
us_west_subnet_1_cidr_block = "10.0.1.0/24"
|
||||
us_west_subnet_1_az = "us-west-2a"
|
||||
|
||||
us_west_subnet_2_cidr_block = "10.0.2.0/24"
|
||||
us_west_subnet_2_az = "us-west-2b"
|
||||
|
||||
us_west_subnet_3_cidr_block = "10.0.3.0/24"
|
||||
us_west_subnet_3_az = "us-west-2c"
|
||||
|
||||
28
aws/aws_simple/vpc/subnets/us-west/variables.tf
Normal file
28
aws/aws_simple/vpc/subnets/us-west/variables.tf
Normal file
@@ -0,0 +1,28 @@
|
||||
variable "us_west_subnet_1_cidr_block" {
|
||||
default = "10.0.1.0/24"
|
||||
}
|
||||
|
||||
variable "us_west_subnet_2_cidr_block" {
|
||||
default = "10.0.2.0/24"
|
||||
}
|
||||
|
||||
variable "us_west_subnet_3_cidr_block" {
|
||||
default = "10.0.3.0/24"
|
||||
}
|
||||
|
||||
|
||||
variable "us_west_subnet_1_az" {
|
||||
default = "us-west-2a"
|
||||
}
|
||||
|
||||
variable "us_west_subnet_2_az" {
|
||||
default = "us-west-2b"
|
||||
}
|
||||
|
||||
variable "us_west_subnet_3_az" {
|
||||
default = "us-west-2c"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
default = "us-west-2"
|
||||
}
|
||||
2
aws/aws_simple/vpc/terraform.tfvars
Normal file
2
aws/aws_simple/vpc/terraform.tfvars
Normal file
@@ -0,0 +1,2 @@
|
||||
vpc_cidr_block = "10.0.0.0/16"
|
||||
|
||||
4
aws/aws_simple/vpc/variables.tf
Normal file
4
aws/aws_simple/vpc/variables.tf
Normal file
@@ -0,0 +1,4 @@
|
||||
variable "vpc_cidr_block" {
|
||||
default = "10.0.0.0/16"
|
||||
}
|
||||
|
||||
65
aws/aws_simple/vpc/vpc.tf
Normal file
65
aws/aws_simple/vpc/vpc.tf
Normal file
@@ -0,0 +1,65 @@
|
||||
#Create aws vpc
|
||||
resource "aws_vpc" "my_vpc" {
|
||||
cidr_block = var.vpc_cidr_block
|
||||
tags = {
|
||||
Name = "production-vpc"
|
||||
Environment = "production"
|
||||
}
|
||||
}
|
||||
# Create aws internet gateway
|
||||
resource "aws_internet_gateway" "my_igw" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
}
|
||||
|
||||
# Create route table entries for the west subnets
|
||||
resource "aws_route_table" "us_west_route_table" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
}
|
||||
|
||||
# Create route table entries for the east subnets
|
||||
resource "aws_route_table" "us_east_route_table" {
|
||||
vpc_id = aws_vpc.my_vpc.id
|
||||
}
|
||||
|
||||
resource "aws_route" "us_west_route" {
|
||||
route_table_id = aws_route_table.us_west_route_table.id
|
||||
destination_cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.my_igw.id
|
||||
}
|
||||
|
||||
resource "aws_route" "us_east_route" {
|
||||
route_table_id = aws_route_table.us_east_route_table.id
|
||||
destination_cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.my_igw.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_west_subnet_1_association" {
|
||||
subnet_id = aws_subnet.us_west_subnet_1.id
|
||||
route_table_id = aws_route_table.us_west_route_table.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_west_subnet_2_association" {
|
||||
subnet_id = aws_subnet.us_west_subnet_2.id
|
||||
route_table_id = aws_route_table.us_west_route_table.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_west_subnet_3_association" {
|
||||
subnet_id = aws_subnet.us_west_subnet_3.id
|
||||
route_table_id = aws_route_table.us_west_route_table.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_east_subnet_1_association" {
|
||||
subnet_id = aws_subnet.us_east_subnet_1.id
|
||||
route_table_id = aws_route_table.us_east_route_table.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_east_subnet_2_association" {
|
||||
subnet_id = aws_subnet.us_east_subnet_2.id
|
||||
route_table_id = aws_route_table.us_east_route_table.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "us_east_subnet_3_association" {
|
||||
subnet_id = aws_subnet.us_east_subnet_3.id
|
||||
route_table_id = aws_route_table.us_east_route_table.id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user