diff --git a/README.md b/README.md new file mode 100644 index 0000000..6114317 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# maven-standalone-application +This barch contains the necessary terrafrorm codes for creating resources in aws diff --git a/terraform-eks/db/db.tf b/terraform-eks/db/db.tf new file mode 100644 index 0000000..dbfdd52 --- /dev/null +++ b/terraform-eks/db/db.tf @@ -0,0 +1,21 @@ +resource "aws_instance" "db" { + ami = "ami-03a0c45ebc70f98ea" + instance_type = "t2.micro" + # subnet_id = module.eip.aws_subnet.demo.id + tags = { + Name = "DB Server" + } +} + +# module "eip" { +# source = "../eip" // child module is called becoz subnet_id is referencing it as module.eip +# instance_id = aws_insatnce.db.id // declaring instance.id from the eip module +# } + +# module "sg" { +# source = "../sg" +# } + +output "PrivateIP" { + value = aws_instance.db.private_ip +} \ No newline at end of file diff --git a/terraform-eks/eip/eip.tf b/terraform-eks/eip/eip.tf new file mode 100644 index 0000000..e5c9625 --- /dev/null +++ b/terraform-eks/eip/eip.tf @@ -0,0 +1,44 @@ +variable "instance_id" { + type = string +} + +resource "aws_eip" "web_ip" { + instance = var.instance_id + # depends_on = [ + # aws_internet_gateway.demo + # ] +} + +# resource "aws_vpc" "demo" { +# cidr_block = "10.0.0.0/16" + +# tags = { +# Name = "myDemoVPC" +# } +# } + +# resource "aws_internet_gateway" "demo" { +# vpc_id = aws_vpc.demo.id // the .id is attached because vpc is being called inside igw + +# tags = { +# Name = "myDemoIGW" +# } +# } + +# resource "aws_subnet" "demo" { + +# availability_zone = us-east-2a +# cidr_block = "10.0.0.0/24" +# map_public_ip_on_launch = true +# vpc_id = aws_vpc.demo.id + +# // depends_on = [aws_internet_gateway.gw] + +# tags = { +# Name = "Public Subnet" +# } +# } + +output "PublicIP" { + value = aws_eip.web_ip.public_ip +} diff --git a/terraform-eks/main.tf b/terraform-eks/main.tf new file mode 100644 index 0000000..b280738 --- /dev/null +++ b/terraform-eks/main.tf @@ -0,0 +1,36 @@ +/* this is the main module block created in the current and parent directory. +From this module block, child blocks can be called in the main.tf module and should be +referenced properly. +*/ +//Terraform block +terraform { + required_version = "~> 1.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.0" # Optional but recommended in production + } + } +} + +//Provider block +provider "aws" { + region = "us-east-2" + profile = "terence" +} + +module "db" { //this is initializing the db module/directory + source = "./db" +} + +module "web" { // eip and sg were ran as child modules inside this module + source = "./web" +} + +output "PrivateIP" { + value = module.db.PrivateIP // this specified in the db module/directory +} + +output "PublicIP" { + value = module.web.pub_ip //calling the web eip +} \ No newline at end of file diff --git a/terraform-eks/sg/sg.tf b/terraform-eks/sg/sg.tf new file mode 100644 index 0000000..6f011dd --- /dev/null +++ b/terraform-eks/sg/sg.tf @@ -0,0 +1,45 @@ +variable "ingress" { + type = list(number) + default = [80, 443] +} + +variable "egress" { + type = list(number) + default = [0] +} + +resource "aws_security_group" "web_traffic" { + name = "Allow Web Traffic" + # vpc_id = module.eip.aws_vpc.demo.id + + dynamic "ingress" { + iterator = port + for_each = var.ingress + content { + from_port = port.value + to_port = port. value + protocol = "TCP" + cidr_blocks = ["0.0.0.0/0"] + } + } + + dynamic "egress" { + iterator = port + for_each = var.egress + content { + from_port = port.value + to_port = port. value + protocol = "TCP" + cidr_blocks = ["0.0.0.0/0"] + } + } +} + +# module "eip" { +# source = "../eip" +# instance_id = aws_vpc.demo.id +# } + +output "sg_name" { + value = aws_security_group.web_traffic.name +} diff --git a/terraform-eks/terraform.tfstate b/terraform-eks/terraform.tfstate new file mode 100644 index 0000000..975974f --- /dev/null +++ b/terraform-eks/terraform.tfstate @@ -0,0 +1,8 @@ +{ + "version": 4, + "terraform_version": "1.0.11", + "serial": 10, + "lineage": "f999bcf5-b749-4305-0dcd-f536b16a3751", + "outputs": {}, + "resources": [] +} diff --git a/terraform-eks/terraform.tfstate.backup b/terraform-eks/terraform.tfstate.backup new file mode 100644 index 0000000..e810e92 --- /dev/null +++ b/terraform-eks/terraform.tfstate.backup @@ -0,0 +1,344 @@ +{ + "version": 4, + "terraform_version": "1.0.11", + "serial": 5, + "lineage": "f999bcf5-b749-4305-0dcd-f536b16a3751", + "outputs": { + "PrivateIP": { + "value": "172.31.42.110", + "type": "string" + }, + "PublicIP": { + "value": "18.216.98.83", + "type": "string" + } + }, + "resources": [ + { + "module": "module.db", + "mode": "managed", + "type": "aws_instance", + "name": "db", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "ami": "ami-03a0c45ebc70f98ea", + "arn": "arn:aws:ec2:us-east-2:163492027646:instance/i-0054bd353edea6c80", + "associate_public_ip_address": true, + "availability_zone": "us-east-2c", + "capacity_reservation_specification": [ + { + "capacity_reservation_preference": "open", + "capacity_reservation_target": [] + } + ], + "cpu_core_count": 1, + "cpu_threads_per_core": 1, + "credit_specification": [ + { + "cpu_credits": "standard" + } + ], + "disable_api_termination": false, + "ebs_block_device": [], + "ebs_optimized": false, + "enclave_options": [ + { + "enabled": false + } + ], + "ephemeral_block_device": [], + "get_password_data": false, + "hibernation": false, + "host_id": null, + "iam_instance_profile": "", + "id": "i-0054bd353edea6c80", + "instance_initiated_shutdown_behavior": "stop", + "instance_state": "running", + "instance_type": "t2.micro", + "ipv6_address_count": 0, + "ipv6_addresses": [], + "key_name": "", + "launch_template": [], + "metadata_options": [ + { + "http_endpoint": "enabled", + "http_put_response_hop_limit": 1, + "http_tokens": "optional" + } + ], + "monitoring": false, + "network_interface": [], + "outpost_arn": "", + "password_data": "", + "placement_group": "", + "placement_partition_number": null, + "primary_network_interface_id": "eni-0b901b7dd9b6455e6", + "private_dns": "ip-172-31-42-110.us-east-2.compute.internal", + "private_ip": "172.31.42.110", + "public_dns": "ec2-18-220-172-154.us-east-2.compute.amazonaws.com", + "public_ip": "18.220.172.154", + "root_block_device": [ + { + "delete_on_termination": true, + "device_name": "/dev/sda1", + "encrypted": false, + "iops": 100, + "kms_key_id": "", + "tags": {}, + "throughput": 0, + "volume_id": "vol-0b6293245383abde8", + "volume_size": 8, + "volume_type": "gp2" + } + ], + "secondary_private_ips": [], + "security_groups": [ + "default" + ], + "source_dest_check": true, + "subnet_id": "subnet-0c2f0d40", + "tags": { + "Name": "DB Server" + }, + "tags_all": { + "Name": "DB Server" + }, + "tenancy": "default", + "timeouts": null, + "user_data": null, + "user_data_base64": null, + "volume_tags": null, + "vpc_security_group_ids": [ + "sg-bbfdb9f2" + ] + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMCwidXBkYXRlIjo2MDAwMDAwMDAwMDB9LCJzY2hlbWFfdmVyc2lvbiI6IjEifQ==" + } + ] + }, + { + "module": "module.web", + "mode": "managed", + "type": "aws_instance", + "name": "web", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "ami": "ami-03a0c45ebc70f98ea", + "arn": "arn:aws:ec2:us-east-2:163492027646:instance/i-0e078cbf4d9997fdd", + "associate_public_ip_address": true, + "availability_zone": "us-east-2c", + "capacity_reservation_specification": [ + { + "capacity_reservation_preference": "open", + "capacity_reservation_target": [] + } + ], + "cpu_core_count": 1, + "cpu_threads_per_core": 1, + "credit_specification": [ + { + "cpu_credits": "standard" + } + ], + "disable_api_termination": false, + "ebs_block_device": [], + "ebs_optimized": false, + "enclave_options": [ + { + "enabled": false + } + ], + "ephemeral_block_device": [], + "get_password_data": false, + "hibernation": false, + "host_id": null, + "iam_instance_profile": "", + "id": "i-0e078cbf4d9997fdd", + "instance_initiated_shutdown_behavior": "stop", + "instance_state": "running", + "instance_type": "t2.micro", + "ipv6_address_count": 0, + "ipv6_addresses": [], + "key_name": "", + "launch_template": [], + "metadata_options": [ + { + "http_endpoint": "enabled", + "http_put_response_hop_limit": 1, + "http_tokens": "optional" + } + ], + "monitoring": false, + "network_interface": [], + "outpost_arn": "", + "password_data": "", + "placement_group": "", + "placement_partition_number": null, + "primary_network_interface_id": "eni-0bf8afafecfe45b5f", + "private_dns": "ip-172-31-41-84.us-east-2.compute.internal", + "private_ip": "172.31.41.84", + "public_dns": "ec2-18-224-94-21.us-east-2.compute.amazonaws.com", + "public_ip": "18.224.94.21", + "root_block_device": [ + { + "delete_on_termination": true, + "device_name": "/dev/sda1", + "encrypted": false, + "iops": 100, + "kms_key_id": "", + "tags": {}, + "throughput": 0, + "volume_id": "vol-0e007eae9259665d6", + "volume_size": 8, + "volume_type": "gp2" + } + ], + "secondary_private_ips": [], + "security_groups": [ + "Allow Web Traffic" + ], + "source_dest_check": true, + "subnet_id": "subnet-0c2f0d40", + "tags": { + "Name": "Web Server" + }, + "tags_all": { + "Name": "Web Server" + }, + "tenancy": "default", + "timeouts": null, + "user_data": "0bf7bf9a50ed826cee41c91cbe8a7f18caf13e5c", + "user_data_base64": null, + "volume_tags": null, + "vpc_security_group_ids": [ + "sg-0137a4ebf02ebac2e" + ] + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMCwidXBkYXRlIjo2MDAwMDAwMDAwMDB9LCJzY2hlbWFfdmVyc2lvbiI6IjEifQ==", + "dependencies": [ + "module.web.module.sg.aws_security_group.web_traffic" + ] + } + ] + }, + { + "module": "module.web.module.eip", + "mode": "managed", + "type": "aws_eip", + "name": "web_ip", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "address": null, + "allocation_id": "eipalloc-0c09c83060b2d79d3", + "associate_with_private_ip": null, + "association_id": "eipassoc-09f45b82e13be48e8", + "carrier_ip": "", + "customer_owned_ip": "", + "customer_owned_ipv4_pool": "", + "domain": "vpc", + "id": "eipalloc-0c09c83060b2d79d3", + "instance": "i-0e078cbf4d9997fdd", + "network_border_group": "us-east-2", + "network_interface": "eni-0bf8afafecfe45b5f", + "private_dns": "ip-172-31-41-84.us-east-2.compute.internal", + "private_ip": "172.31.41.84", + "public_dns": "ec2-18-216-98-83.us-east-2.compute.amazonaws.com", + "public_ip": "18.216.98.83", + "public_ipv4_pool": "amazon", + "tags": null, + "tags_all": {}, + "timeouts": null, + "vpc": true + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiZGVsZXRlIjoxODAwMDAwMDAwMDAsInJlYWQiOjkwMDAwMDAwMDAwMCwidXBkYXRlIjozMDAwMDAwMDAwMDB9fQ==", + "dependencies": [ + "module.web.aws_instance.web", + "module.web.module.sg.aws_security_group.web_traffic" + ] + } + ] + }, + { + "module": "module.web.module.sg", + "mode": "managed", + "type": "aws_security_group", + "name": "web_traffic", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-2:163492027646:security-group/sg-0137a4ebf02ebac2e", + "description": "Managed by Terraform", + "egress": [ + { + "cidr_blocks": [ + "0.0.0.0/0" + ], + "description": "", + "from_port": 0, + "ipv6_cidr_blocks": [], + "prefix_list_ids": [], + "protocol": "tcp", + "security_groups": [], + "self": false, + "to_port": 0 + } + ], + "id": "sg-0137a4ebf02ebac2e", + "ingress": [ + { + "cidr_blocks": [ + "0.0.0.0/0" + ], + "description": "", + "from_port": 443, + "ipv6_cidr_blocks": [], + "prefix_list_ids": [], + "protocol": "tcp", + "security_groups": [], + "self": false, + "to_port": 443 + }, + { + "cidr_blocks": [ + "0.0.0.0/0" + ], + "description": "", + "from_port": 80, + "ipv6_cidr_blocks": [], + "prefix_list_ids": [], + "protocol": "tcp", + "security_groups": [], + "self": false, + "to_port": 80 + } + ], + "name": "Allow Web Traffic", + "name_prefix": "", + "owner_id": "163492027646", + "revoke_rules_on_delete": false, + "tags": null, + "tags_all": {}, + "timeouts": null, + "vpc_id": "vpc-fabfda91" + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6OTAwMDAwMDAwMDAwfSwic2NoZW1hX3ZlcnNpb24iOiIxIn0=" + } + ] + } + ] +} diff --git a/terraform-eks/web/server-script.sh b/terraform-eks/web/server-script.sh new file mode 100644 index 0000000..b2217ac --- /dev/null +++ b/terraform-eks/web/server-script.sh @@ -0,0 +1,11 @@ +#! /bin/bash +# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html +sudo apt update -y +sudo apt install httpd -y +sudo systemctl enable httpd +sudo service httpd start +sudo echo '
Terraform Demo
Application Version: V1
' | sudo tee /var/www/html/app1/index.html +sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html + diff --git a/terraform-eks/web/web.tf b/terraform-eks/web/web.tf new file mode 100644 index 0000000..a1b28a9 --- /dev/null +++ b/terraform-eks/web/web.tf @@ -0,0 +1,23 @@ +resource "aws_instance" "web" { + ami = "ami-03a0c45ebc70f98ea" + instance_type = "t2.micro" + # subnet_id = module.eip.aws_subnet.demo.id + security_groups = [module.sg.sg_name] #reference using module/directory name and output block name + user_data = file("./web/server-script.sh") + tags = { + Name = "Web Server" + } +} + +module "eip" { + source = "../eip" //this is a child module represented by the ../ + instance_id = aws_instance.web.id //associating the instance to the eip +} + +module "sg" { + source = "../sg" +} + +output "pub_ip" { + value = module.eip.PublicIP +} \ No newline at end of file