Skip to content

Instantly share code, notes, and snippets.

# Backend Variables
variable "state_bucket_name" {
default = "2-tier-state-bucket"
}
variable "state_table_name" {
default = "2-tier-state-table"
}
@shreeegupta
shreeegupta / outputs.tf
Created May 26, 2023 16:42
two_tier_architecture_using_terraform/outputs.tf
#Outputs
output "web_server_public_ip" {
description = "Public IP of Web Servers"
value = module.create_two_tier_aws.web_server_public_ip
}
output "ec2_ssh_access" {
description = "Remote Access to EC2"
value = module.create_two_tier_aws.ec2_ssh_access
}
# Modules
module "create_two_tier_aws" {
source = "./modules"
env = var.env
aws_region = var.aws_region
vpc_cidr_block = var.vpc_cidr
public_subnet_cidr_block = [var.public_subnet1_az1_cidr, var.public_subnet2_az2_cidr]
private_subnet_cidr_block = [var.private_subnet1_az1_cidr, var.private_subnet2_az2_cidr]
@shreeegupta
shreeegupta / providers.tf
Last active May 26, 2023 16:50
two_tier_architecture_using_terraform/providers.tf
# Terraform and Provider Blocks
terraform {
required_providers {
aws = {
version = "~> 4.55"
source = "hashicorp/aws"
}
}
#####Uncomment this backend section after creating s3 bucket and dynamodb table####
@shreeegupta
shreeegupta / variables.tf
Created May 26, 2023 16:38
two_tier_architecture_using_terraform/variables.tf
# Variables
variable "env" {
description = "Environment Name"
type = string
}
variable "aws_region" {
description = "AWS deployment region"
type = string
# Outputs
output "web_server_public_ip" {
description = "Public IP of Web Servers"
value = [for i in aws_instance.web_server[*] : i.public_ip]
}
output "ec2_ssh_access" {
description = "SSH Remote Access to the first EC2 instance"
value = "ssh -i ${var.ssh_key}.pem ubuntu@${aws_instance.web_server[0].public_ip}"
}
# VPC
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr_block
enable_dns_hostnames = true
tags = {
Name = "${var.env}-vpc"
Environment = var.env
}
}
# Security Group
resource "aws_security_group" "db_security_group" {
name = "${var.env}-db-security-group"
description = "Security Group for RDS instance"
vpc_id = aws_vpc.vpc.id
ingress {
description = "MySQL traffic from Web Servers"
from_port = 3306
to_port = 3306
# Security Group
# Obtain User Local Public IP
data "external" "myipaddr" {
program = ["bash", "-c", "curl -s 'https://ipinfo.io/json'"]
}
resource "aws_security_group" "ec2_security_group" {
name = "${var.env}-ec2-security-group"
description = "Security Group for EC2 Web Servers"
# Security Group
resource "aws_security_group" "alb_security_group" {
name = "${var.env}-alb-security-group"
description = "ALB Security Group"
vpc_id = aws_vpc.vpc.id
ingress {
description = "HTTP from Internet"
from_port = 80
to_port = 80