Skip to content

Commit

Permalink
[Terraform] // Missing Description, Add examples in module, Add valid…
Browse files Browse the repository at this point in the history
…ation (#99)
  • Loading branch information
Raghav2211 authored Jan 16, 2021
1 parent 52e8209 commit 09c798e
Show file tree
Hide file tree
Showing 24 changed files with 214 additions and 67 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ jobs:
- name: "Setup - Checkout"
uses: actions/[email protected]

# Static analysis of Terraform templates to spot potential security issues
# Marketplace: https://github.com/marketplace/actions/terraform-security-scan
- name: "Setup - Security Scan"
uses: triat/terraform-security-scan@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Downloads a specific version of Terraform CLI and adds it to PATH
# Marketplace: https://github.com/marketplace/actions/hashicorp-setup-terraform
- name: Setup Terraform
Expand Down
4 changes: 0 additions & 4 deletions todo-app/aws/3-tier-app/lab/bastion/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
provider "aws" {
region = var.region
}

module "bastion" {
source = "../../modules/bastion"
app = var.app
Expand Down
5 changes: 0 additions & 5 deletions todo-app/aws/3-tier-app/lab/database/mysql/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
provider "aws" {
region = var.region
}
module "mysql" {
source = "../../../modules/database/mysql"
app = var.app
instance_type = var.instance_type
master_user = var.master_user
master_password = var.master_password
multi_az = var.multi_az


}
4 changes: 4 additions & 0 deletions todo-app/aws/3-tier-app/lab/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Provider configuration
provider "aws" {
region = var.region
}
4 changes: 0 additions & 4 deletions todo-app/aws/3-tier-app/lab/security/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
provider "aws" {
region = var.region
}

data "aws_region" "current" {}

data "aws_vpc" "selected" {
Expand Down
4 changes: 0 additions & 4 deletions todo-app/aws/3-tier-app/lab/services/todo/app/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
provider "aws" {
region = var.region
}

module "todo_app" {
source = "../../../../modules/app-server"
app = var.app
Expand Down
4 changes: 0 additions & 4 deletions todo-app/aws/3-tier-app/lab/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
provider "aws" {
region = var.region
}

module "vpc" {
source = "../../modules/network"
app = var.app
Expand Down
2 changes: 1 addition & 1 deletion todo-app/aws/3-tier-app/modules/app-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ locals {
Version = var.app.version
Role = "app"
Environment = var.app.env
#Time = formatdate("YYYYMMDDhhmmss", timestamp())
LastScanned = formatdate("YYYYMMDDhh", timestamp())
}
}

Expand Down
3 changes: 2 additions & 1 deletion todo-app/aws/3-tier-app/modules/app-server/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ variable "instance_type" {


variable "scaling_capacity" {
type = map(string)
type = map(string)
description = "Scaling paramter for EC2 Auto scaling group"
default = {
min = 1
desired = 2
Expand Down
14 changes: 14 additions & 0 deletions todo-app/aws/3-tier-app/modules/app-server/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {

required_providers {
aws = {
source = "hashicorp/aws"
version = "= 3.22.0"
}
template = {
source = "hashicorp/template"
version = "= 2.2.0"
}
}
required_version = "= 0.13.5"
}
49 changes: 49 additions & 0 deletions todo-app/aws/3-tier-app/modules/bastion/example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
provider "aws" {
region = "us-west-2"
}

locals {
name_suffix = "us-west-2-l-psi"
tags = {
AppId = "psi"
Version = "1.0.0"
Role = "security"
Environment = "lab"
}
}

# Add VPC
module "vpc" {
source = "../../network/example"
}

# Add security group
module "bastion_sg" {
source = "terraform-aws-modules/security-group/aws//modules/ssh"
version = "3.17.0"
name = "security-group-${local.name_suffix}-bastion"
vpc_id = module.vpc.vpc_id
description = "Bastion security group"
ingress_cidr_blocks = ["0.0.0.0/0"]
use_name_prefix = false
auto_ingress_with_self = []
tags = merge(local.tags, {
App = "bastion"
})
}

module "bastion" {
source = "../"
app = {
id = "psi"
name = "todo"
version = "1.0.0"
env = "lab"
}
ssh_users = [
{
username = "bastionuser"
public_key = "######################[PLACE-YOUR-SSH-PUBLIC-KEY-HERE]######################"
}
]
}
48 changes: 48 additions & 0 deletions todo-app/aws/3-tier-app/modules/database/mysql/example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
provider "aws" {
region = "us-west-2"
}

locals {
name_suffix = "us-west-2-l-psi"
tags = {
AppId = "psi"
Version = "1.0.0"
Role = "security"
Environment = "lab"
}
}

# Add VPC
module "vpc" {
source = "../../network/example"
}

#Add security group
module "mysql_sg" {
source = "terraform-aws-modules/security-group/aws//modules/mysql"
version = "3.17.0"
name = "security-group-${local.name_suffix}-mysql"
vpc_id = module.vpc.vpc_id
description = "Test mysql database connection"
use_name_prefix = false
auto_ingress_with_self = []
# auto_ingress_rules = []
ingress_cidr_blocks = ["0.0.0.0/0"]

tags = merge(local.tags, {
App = "mysql"
})
}

module "mysql" {
source = "../"
app = {
id = "psi"
version = "1.0.0"
env = "lab"
}
instance_type = "db.t2.micro"
master_user = "masteruser"
master_password = "masterpassword"
multi_az = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "vpc_id" {
description = "The ID of the VPC"
value = module.vpc.vpc_id
}
2 changes: 1 addition & 1 deletion todo-app/aws/3-tier-app/modules/database/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ locals {
Version = var.app.version
Role = "db"
Environment = var.app.env
#Time = formatdate("YYYYMMDDhhmmss", timestamp())
LastScanned = formatdate("YYYYMMDDhh", timestamp())
}
}

Expand Down
16 changes: 10 additions & 6 deletions todo-app/aws/3-tier-app/modules/database/mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ variable "app" {
}

variable "instance_type" {
type = string
description = "RDS(Mysql) instance type"
type = string
}

variable "storage_size_in_gib" {
type = number
default = 5
description = "The allocated storage in gigabytes"
type = number
default = 5
}

variable "database_name" {
Expand All @@ -29,11 +31,13 @@ variable "create_database_name_as_appid" {
}

variable "master_user" {
type = string
type = string
description = "Mysql master user"
}

variable "master_password" {
type = string
type = string
description = "Mysql master password"
validation {
condition = length(var.master_password) >= 8
error_message = "Master password should not be of length less than 8 charactors."
Expand All @@ -42,7 +46,7 @@ variable "master_password" {

variable "subnet_group" {
default = ""
description = "Database subnet group"
description = "Database subnet group, if not provided it'll use default subnet group"
}


Expand Down
9 changes: 9 additions & 0 deletions todo-app/aws/3-tier-app/modules/database/mysql/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "= 3.22.0"
}
}
required_version = "= 0.13.5"
}
2 changes: 1 addition & 1 deletion todo-app/aws/3-tier-app/modules/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ locals {
Version = var.app.version
Role = var.app.role
Environment = var.app.env
#Time = formatdate("YYYYMMDDhhmmss", timestamp())
LastScanned = formatdate("YYYYMMDDhh", timestamp())
}
}

Expand Down
7 changes: 5 additions & 2 deletions todo-app/aws/3-tier-app/modules/ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ variable "instance_count" {
}

variable "associate_public_ip_address" {
type = bool
default = false
description = "Enable public IP address on ec2 instance(s)"
type = bool
default = false
}

variable "security_group_filters" {
description = "Security group filters to find security groups for ec2 instance(s)"
type = list(object({
name = string
values = list(string)
Expand All @@ -44,6 +46,7 @@ variable "security_group_filters" {
}

variable "subnet_filters" {
description = "Subnet filters to find subnet for ec2 instance(s)"
type = list(object({
name = string
values = list(string)
Expand Down
20 changes: 20 additions & 0 deletions todo-app/aws/3-tier-app/modules/network/example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
provider "aws" {
region = "us-west-2"
}

module "vpc" {
source = "../"
app = {
id = "psi"
version = "1.0.0"
env = "lab"
}
cidr = "172.31.0.0/24"
azs = ["us-west-2a", "us-west-2b"]
public_subnets = ["172.31.0.128/26", "172.31.0.192/26"]
private_subnets = ["172.31.0.0/27", "172.31.0.32/27"]
database_subnets = ["172.31.0.64/27", "172.31.0.96/27"]
create_internet_gateway = true
create_database_subnet_group = true
enable_nat_gateway_single = true
}
4 changes: 4 additions & 0 deletions todo-app/aws/3-tier-app/modules/network/example/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "vpc_id" {
description = "The ID of the VPC"
value = module.vpc.vpc_id
}
41 changes: 18 additions & 23 deletions todo-app/aws/3-tier-app/modules/network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,34 @@ data "aws_region" "current" {}
locals {
name_suffix = "${data.aws_region.current.name}-${substr(var.app.env, 0, 1)}-${var.app.id}"
enable_nat_gateway_per_subnet = var.enable_nat_gateway_per_subnet || var.enable_nat_gateway_single || var.enable_nat_gateway_per_az
single_nat_gateway = var.enable_nat_gateway_per_subnet ? false : var.enable_nat_gateway_single
enable_nat_gateway_per_az = var.enable_nat_gateway_per_subnet ? false : (var.enable_nat_gateway_per_az && var.enable_nat_gateway_single ? ! var.enable_nat_gateway_per_az : var.enable_nat_gateway_per_az)
single_nat_gateway = local.enable_nat_gateway_per_subnet && var.enable_nat_gateway_single
enable_nat_gateway_per_az = local.enable_nat_gateway_per_subnet && (var.enable_nat_gateway_per_az && var.enable_nat_gateway_single ? ! var.enable_nat_gateway_per_az : var.enable_nat_gateway_per_az)
database_subnet_group = var.create_database_subnet_group ? length(var.database_subnets) > 1 : ! var.create_database_subnet_group
}

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.64.0"

name = "vpc-${local.name_suffix}"
cidr = var.cidr
azs = var.azs

# subnets
public_subnets = var.public_subnets
private_subnets = var.private_subnets
database_subnets = var.database_subnets

# gateways
enable_nat_gateway = local.enable_nat_gateway_per_subnet
single_nat_gateway = local.single_nat_gateway
one_nat_gateway_per_az = local.enable_nat_gateway_per_az

create_database_subnet_group = var.create_database_subnet_group
# database
source = "terraform-aws-modules/vpc/aws"
version = "2.64.0"
name = "vpc-${local.name_suffix}"
cidr = var.cidr
azs = var.azs
public_subnets = var.public_subnets
private_subnets = var.private_subnets
database_subnets = var.database_subnets
create_igw = var.create_internet_gateway
enable_nat_gateway = local.enable_nat_gateway_per_subnet
single_nat_gateway = local.single_nat_gateway
one_nat_gateway_per_az = local.enable_nat_gateway_per_az
create_database_subnet_group = local.database_subnet_group
create_database_subnet_route_table = length(var.database_subnets) > 1

instance_tenancy = var.instance_tenancy

tags = {
AppId = var.app.id
Version = var.app.version
Role = "infra"
Environment = var.app.env
#Time = formatdate("YYYYMMDDhhmmss", timestamp())
LastScanned = formatdate("YYYYMMDDhh", timestamp())
}

public_subnet_tags = {
Expand Down
Loading

0 comments on commit 09c798e

Please sign in to comment.