From 1b13b50ddbf0854539bcab5673dafad966ad7be2 Mon Sep 17 00:00:00 2001 From: Shrayansh <35774702+shrayanshjha@users.noreply.github.com> Date: Tue, 6 Aug 2024 19:52:35 +0530 Subject: [PATCH 1/2] Add files via upload --- vpc/main.tf | 68 ++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/vpc/main.tf b/vpc/main.tf index 134ba5d..a07366a 100644 --- a/vpc/main.tf +++ b/vpc/main.tf @@ -1,34 +1,34 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 3.27" - } - } - - required_version = ">= 0.14.9" -} - -provider "aws" { - profile = "default" - region = "us-west-2" -} - -module "vpc" { - source = "terraform-aws-modules/vpc/aws" - - name = "my-vpc" - cidr = "10.0.0.0/16" - - azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] - private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] - public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] - - enable_nat_gateway = true - enable_vpn_gateway = true - - tags = { - Terraform = "true" - Environment = "dev" - } -} +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.46.0" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = "us-west-2" +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "my-vpc" + cidr = "10.0.0.0/16" + + azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] + + enable_nat_gateway = true + enable_vpn_gateway = true + + tags = { + Terraform = "true" + Environment = "dev" + } +} From b9d9f29bdb58a8ac3da2d283023764c77c4134c1 Mon Sep 17 00:00:00 2001 From: Shrayansh <35774702+shrayanshjha@users.noreply.github.com> Date: Tue, 6 Aug 2024 19:53:56 +0530 Subject: [PATCH 2/2] Add files via upload --- EIP_association/main.tf | 70 ++++++++++++++++++++++++++++++++++++++ ElasticIP/deploy.sh | 18 ++++++++++ ElasticIP/main.tf | 24 ++++++++++++++ Security Group/README.md | 1 + Security Group/main.tf | 11 ++++++ rds/main.tf | 72 ++++++++++++++++++++++++++++++++++++++++ rout53/main.tf | 61 ++++++++++++++++++++++++++++++++++ 7 files changed, 257 insertions(+) create mode 100644 EIP_association/main.tf create mode 100644 ElasticIP/deploy.sh create mode 100644 ElasticIP/main.tf create mode 100644 Security Group/README.md create mode 100644 Security Group/main.tf create mode 100644 rds/main.tf create mode 100644 rout53/main.tf diff --git a/EIP_association/main.tf b/EIP_association/main.tf new file mode 100644 index 0000000..2eaa821 --- /dev/null +++ b/EIP_association/main.tf @@ -0,0 +1,70 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + region = "us-east-1" # Change the region as needed +} + +# Data source to get the default VPC +data "aws_vpc" "default" { + default = true +} + +# Data source to get the default subnet in the default VPC +data "aws_subnet" "default" { + filter { + name = "vpc-id" + values = [data.aws_vpc.default.id] + } + + filter { + name = "default-for-az" + values = ["true"] + } + + availability_zone = "us-east-1a" # Change the availability zone as needed +} + +# Data source to get the default security group in the default VPC +data "aws_security_group" "default" { + filter { + name = "vpc-id" + values = [data.aws_vpc.default.id] + } + + filter { + name = "group-name" + values = ["default"] + } +} + +# Create an EC2 instance in the default VPC and subnet +resource "aws_instance" "web" { + ami = "ami-0ba9883b710b05ac6" # Replace with a valid AMI ID for your region + instance_type = "t2.micro" + subnet_id = data.aws_subnet.default.id + vpc_security_group_ids = [data.aws_security_group.default.id] + + tags = { + Name = "WebServer" + } +} + +# Allocate an Elastic IP +resource "aws_eip" "web" { + vpc = true +} + +# Associate the Elastic IP with the instance +resource "aws_eip_association" "web" { + instance_id = aws_instance.web.id + allocation_id = aws_eip.web.id +} diff --git a/ElasticIP/deploy.sh b/ElasticIP/deploy.sh new file mode 100644 index 0000000..2205b40 --- /dev/null +++ b/ElasticIP/deploy.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status +set -e + +# Initialize Terraform +echo "Initializing Terraform..." +terraform init + +# Plan the Terraform deployment +echo "Planning Terraform deployment..." +terraform plan + +# Apply the Terraform configuration with auto-accept +echo "Applying Terraform configuration..." +terraform apply -auto-approve + +echo "Terraform deployment complete." diff --git a/ElasticIP/main.tf b/ElasticIP/main.tf new file mode 100644 index 0000000..bce549d --- /dev/null +++ b/ElasticIP/main.tf @@ -0,0 +1,24 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + region = "us-east-1" # Change the region as needed +} + +# Allocate an Elastic IP +resource "aws_eip" "example" { + # No additional configuration is needed for a basic Elastic IP +} + +# Optionally, you can output the allocated Elastic IP address +output "elastic_ip" { + value = aws_eip.example.public_ip +} diff --git a/Security Group/README.md b/Security Group/README.md new file mode 100644 index 0000000..ace42f1 --- /dev/null +++ b/Security Group/README.md @@ -0,0 +1 @@ +# terraformcode \ No newline at end of file diff --git a/Security Group/main.tf b/Security Group/main.tf new file mode 100644 index 0000000..74060ab --- /dev/null +++ b/Security Group/main.tf @@ -0,0 +1,11 @@ +provider "aws" { + access_key = "${var.aws_access_key}" + secret_key = "${var.aws_secret_key}" + region = "${var.region}" +} + +module "s3" { + source = "" + #bucket name should be unique + bucket_name = "" +} diff --git a/rds/main.tf b/rds/main.tf new file mode 100644 index 0000000..e1ac358 --- /dev/null +++ b/rds/main.tf @@ -0,0 +1,72 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + region = "us-east-1" # Change the region as needed +} + +# Create a VPC +resource "aws_vpc" "main" { + cidr_block = "192.0.0.0/16" +} + +# Create the first subnet +resource "aws_subnet" "subnet_a" { + vpc_id = aws_vpc.main.id + cidr_block = "192.0.1.0/24" + availability_zone = "us-east-1a" # Change the availability zone as needed +} + +# Create the second subnet +resource "aws_subnet" "subnet_b" { + vpc_id = aws_vpc.main.id + cidr_block = "192.0.2.0/24" + availability_zone = "us-east-1b" # Change the availability zone as needed +} + +# Create a security group for the RDS instance +resource "aws_security_group" "rds_sg" { + vpc_id = aws_vpc.main.id + + ingress { + from_port = 3306 + to_port = 3306 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +# Create a DB subnet group +resource "aws_db_subnet_group" "main" { + name = "main" + subnet_ids = [aws_subnet.subnet_a.id, aws_subnet.subnet_b.id] +} + +# Create an RDS instance +resource "aws_db_instance" "default" { + allocated_storage = 20 + engine = "mysql" + engine_version = "8.0" + instance_class = "db.t3.micro" + name = "exampledb" + username = "admin" + password = "password" # Change the password as needed + db_subnet_group_name = aws_db_subnet_group.main.name + vpc_security_group_ids = [aws_security_group.rds_sg.id] + skip_final_snapshot = true +} diff --git a/rout53/main.tf b/rout53/main.tf new file mode 100644 index 0000000..273d5c4 --- /dev/null +++ b/rout53/main.tf @@ -0,0 +1,61 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = "us-east-2" +} + +# Create a Route 53 hosted zone +resource "aws_route53_zone" "main" { + name = "example.com" +} + +resource "aws_route53_zone" "dev" { + name = "dev.example.com" + + tags = { + Environment = "dev" + } +} +resource "aws_route53_record" "dev-ns" { + zone_id = aws_route53_zone.main.zone_id + name = "dev.example.com" + type = "NS" + ttl = "30" + records = aws_route53_zone.dev.name_servers +} + + +# Create an A record in the hosted zone +resource "aws_route53_record" "www" { + zone_id = aws_route53_zone.primary.id + name = "www.example.com" + type = "A" + ttl = 300 + #records = [aws_eip.lb.public_ip] + + records = [ + "192.0.2.1" + ] +} + +# Create a CNAME record in the hosted zone +resource "aws_route53_record" "cname_record" { + zone_id = aws_route53_zone.primary.id + name = "blog.example.com" + type = "CNAME" + ttl = 300 + + records = [ + "www.ipexample.com" + ] +}