Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 17 terraform #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions 17-Terraform/dev/network-infra/dev.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vpc_cidr = "10.0.0.0/16"
subnet1_cidr = "10.0.1.0/24"
subnet2_cidr = "10.0.2.0/24"
destination_cidr = "0.0.0.0/0"
85 changes: 85 additions & 0 deletions 17-Terraform/dev/network-infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
time = {
source = "hashicorp/time"
version = "~> 0.8"
}
}
backend "s3" {
bucket = "desmond-stelligent-u-bucket"
key = "dev/network-infra/state/terraform.tfstate"
encrypt = true
region = "us-east-1"
profile = "labs"
dynamodb_table = "terraform-up-and-running-locks"
}
}

provider "aws" {
region = "us-east-1"
profile = "labs"
}

provider "archive" {}

data "aws_availability_zones" "available" {}

# Create a VPC
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
}

resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet1_cidr
availability_zone = data.aws_availability_zones.available.names[0]

tags = {
Name = "tf-subnet-1"
}
}

resource "aws_subnet" "subnet2" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet2_cidr
availability_zone = data.aws_availability_zones.available.names[1]

tags = {
Name = "tf-subnet-2"
}
}

resource "aws_internet_gateway" "ig" {
vpc_id = aws_vpc.main.id
}

resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
}

resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.public.id
destination_cidr_block = var.destination_cidr
gateway_id = aws_internet_gateway.ig.id
}

resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.subnet1.id
route_table_id = aws_route_table.public.id
}


data "archive_file" "zip" {
type = "zip"
source_file = "data_backup/tfplan"
output_path = "data_backup/data_backup.zip"
depends_on = [
aws_vpc.main,
aws_subnet.subnet1,
aws_subnet.subnet2
]
}
21 changes: 21 additions & 0 deletions 17-Terraform/dev/network-infra/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

WORKSPACE_NAME="${1}"
OPTION="${2}"

FILE="${WORKSPACE_NAME}.tfvars"
BACKUP_PATH="data_backup"

if [ -f "/$FILE" ]
then
echo "File '${FILE}' not found."
else
terraform ${OPTION} -out ${BACKUP_PATH}/tfplan -var-file=${FILE}
if [ -f "${BACKUP_PATH}/tfplan" ]
then
echo "Plan is available for review!"
time terraform apply ${BACKUP_PATH}/tfplan
else
echo "tfplan not found"
fi
fi
16 changes: 16 additions & 0 deletions 17-Terraform/dev/network-infra/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "vpc_cidr" {
type = string
}

variable "subnet1_cidr" {
type = string
}

variable "subnet2_cidr" {
type = string
}

variable "destination_cidr" {
type = string
}

85 changes: 85 additions & 0 deletions 17-Terraform/modules/vpc_with_public_subnets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
time = {
source = "hashicorp/time"
version = "~> 0.8"
}
}
backend "s3" {
bucket = "desmond-stelligent-u-bucket"
key = "dev/network-infra/state/terraform.tfstate"
encrypt = true
region = "us-east-1"
profile = "labs"
dynamodb_table = "terraform-up-and-running-locks"
}
}

provider "aws" {
region = "us-east-1"
profile = "labs"
}

provider "archive" {}

data "aws_availability_zones" "available" {}

# Create a VPC
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
}

resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet1_cidr
availability_zone = data.aws_availability_zones.available.names[0]

tags = {
Name = "tf-subnet-1"
}
}

resource "aws_subnet" "subnet2" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet2_cidr
availability_zone = data.aws_availability_zones.available.names[1]

tags = {
Name = "tf-subnet-2"
}
}

resource "aws_internet_gateway" "ig" {
vpc_id = aws_vpc.main.id
}

resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
}

resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.public.id
destination_cidr_block = var.destination_cidr
gateway_id = aws_internet_gateway.ig.id
}

resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.subnet1.id
route_table_id = aws_route_table.public.id
}


data "archive_file" "zip" {
type = "zip"
source_file = "data_backup/tfplan"
output_path = "data_backup/data_backup.zip"
depends_on = [
aws_vpc.main,
aws_subnet.subnet1,
aws_subnet.subnet2
]
}
11 changes: 11 additions & 0 deletions 17-Terraform/modules/vpc_with_public_subnets/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "vpc_id" {
value = aws_vpc.main.id
}

output "subnet_id" {
value = aws_subnet.subnet1.id
}

output "subnet_id_2" {
value = aws_subnet.subnet2.id
}
16 changes: 16 additions & 0 deletions 17-Terraform/modules/vpc_with_public_subnets/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "vpc_cidr" {
type = string
}

variable "subnet1_cidr" {
type = string
}

variable "subnet2_cidr" {
type = string
}

variable "destination_cidr" {
type = string
}

16 changes: 16 additions & 0 deletions 17-Terraform/prod/network-infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

module "my_vpc" {
source = "../../modules/vpc_with_public_subnets"
vpc_cidr = var.vpc_cidr
subnet1_cidr = var.subnet1_cidr
subnet2_cidr = var.subnet2_cidr
destination_cidr = var.destination_cidr
}
4 changes: 4 additions & 0 deletions 17-Terraform/prod/network-infra/prod.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vpc_cidr = "10.0.0.0/16"
subnet1_cidr = "10.0.1.0/24"
subnet2_cidr = "10.0.2.0/24"
destination_cidr = "0.0.0.0/0"
15 changes: 15 additions & 0 deletions 17-Terraform/prod/network-infra/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "vpc_cidr" {
type = string
}

variable "subnet1_cidr" {
type = string
}

variable "subnet2_cidr" {
type = string
}

variable "destination_cidr" {
type = string
}
68 changes: 68 additions & 0 deletions 17-Terraform/state-management/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
backend "s3" {
bucket = "desmond-stelligent-u-bucket"
key = "state_management/terraform.tfstate"
encrypt = true
region = "us-east-1"
profile = "labs"
dynamodb_table = "terraform-up-and-running-locks"
}
}

provider "aws" {
region = "us-east-1"
profile = "labs"
}

resource "aws_s3_bucket" "lab17_bucket" {
bucket = "desmond-stelligent-u-bucket"
}

resource "aws_s3_bucket_acl" "example-lab17" {
bucket = aws_s3_bucket.lab17_bucket.id
acl = "private"
}

resource "aws_s3_bucket_versioning" "example" {
bucket = aws_s3_bucket.lab17_bucket.id
versioning_configuration {
status = "Enabled"
}
}

resource "aws_s3_bucket_server_side_encryption_configuration" "example" {
bucket = aws_s3_bucket.lab17_bucket.bucket

rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}

resource "aws_s3_bucket_public_access_block" "s3_block_access" {
bucket = aws_s3_bucket.lab17_bucket.id

block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}


resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-up-and-running-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"

attribute {
name = "LockID"
type = "S"
}
}
11 changes: 11 additions & 0 deletions 17-Terraform/terraform-example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "aws" {
region = "us-east-1"
access_key = "AKIAUXAYGAARTYR6VMSI7C"
secret_key = "gEefvzATtW+YvP4sxNbEy8HSfgn55EoitOlRyoUf7wE3M"

}

resource "aws_instance" "my_ec2_server" {
ami = "ami-026b57f3c383c2eec"
instance_type = "t2.micro"
}