Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module "vpc" {
- source = "/home/ec2-user/count/vpc-module"
- project_enviornment = var.project_env
- project_name = var.myproject_name
- vpc_cidr = var.myvpc_cidr
- natgw = true
- }
- # Creating Security group
- resource "aws_security_group" "bastion" {
- name_prefix = "${var.myproject_name}-${var.project_env}-bastion-"
- description = "Allow ssh from my IP only"
- vpc_id = module.vpc.vpc_id
- ingress {
- description = "22 from my IP"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.myproject_name}-${var.project_env}-bastion"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "${var.myproject_name}-${var.project_env}-frontend-"
- description = "Allow http,https and ssh"
- vpc_id = module.vpc.vpc_id
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "HTTPS port"
- from_port = 443
- to_port = 443
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "22 from my IP"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- security_groups = [aws_security_group.bastion.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.myproject_name}-${var.project_env}-frontend"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- resource "aws_security_group" "backend" {
- name_prefix = "${var.myproject_name}-${var.project_env}-backend-"
- description = "Allow mysql and ssh port only"
- vpc_id = module.vpc.vpc_id
- ingress {
- description = "22 from Bastion"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- security_groups = [aws_security_group.bastion.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "MYSQL IP"
- from_port = 3306
- to_port = 3306
- protocol = "tcp"
- security_groups = [aws_security_group.frontend.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.myproject_name}-${var.project_env}-backend"
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- # Creating ec2-instance
- resource "aws_instance" "bastion" {
- ami = var.ami
- subnet_id = module.vpc.public_subnet1
- instance_type = var.instance_type
- key_name = var.key_name
- vpc_security_group_ids = [aws_security_group.bastion.id]
- user_data = file("bastion.sh")
- tags = {
- "Name" = "${var.myproject_name}-${var.project_env}-bastion"
- }
- }
- resource "aws_instance" "frontend" {
- ami = var.ami
- subnet_id = module.vpc.public_subnet2
- instance_type = var.instance_type
- key_name = var.key_name
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- tags = {
- "Name" = "${var.myproject_name}-${var.project_env}-frontend"
- }
- }
- resource "aws_instance" "backend" {
- ami = var.ami
- subnet_id = module.vpc.private_subnet1
- instance_type = var.instance_type
- key_name = var.key_name
- vpc_security_group_ids = [aws_security_group.backend.id]
- user_data = file("backend.sh")
- tags = {
- "Name" = "${var.myproject_name}-${var.project_env}-backend"
- }
- }
- # Creating a private hosted zones
- resource "aws_route53_zone" "uber" {
- name = "${var.myproject_name}-${var.project_env}.local"
- vpc {
- vpc_id = module.vpc.vpc_id
- }
- }
- # Creatinng 3 records
- resource "aws_route53_record" "frontend" {
- zone_id = aws_route53_zone.uber.zone_id
- name = "frontend"
- type = "A"
- ttl = 5
- records = [aws_instance.frontend.private_ip]
- }
- resource "aws_route53_record" "backend" {
- zone_id = aws_route53_zone.uber.zone_id
- name = "backend"
- type = "A"
- ttl = 5
- records = [aws_instance.backend.private_ip]
- }
- resource "aws_route53_record" "bastiom" {
- zone_id = aws_route53_zone.uber.zone_id
- name = "bastion"
- type = "A"
- ttl = 5
- records = [aws_instance.bastion.private_ip]
- }
Advertisement
Add Comment
Please, Sign In to add comment