diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3fa8c86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.terraform diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..2566c50 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,19 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.27.0" + hashes = [ + "h1:vfIXoqbQ7hUfnz0Rb6a5jVU6hxVXZg//ejHGrS59FwU=", + "zh:2986eb5a1ffbb0336c6390aad533b62efc832aa8aa5460d523e1f2daa4f42f79", + "zh:825317cdb80860833125a856c0befc877cba22d41c631c5a7ca22400693d4356", + "zh:a47aad668cc74058f508c56c5407cd715dbb9b6389aa68d37543e897895db43f", + "zh:c0011502d0eb4637918127c3987a8cc07a015ea00f74f4956fd111c736286a4d", + "zh:d5088ab51043bb2239132f4ed3760292b6aa4f7296232e4b8017f8c5c34f051a", + "zh:d893658e983eb17a23a8124c79a910cc729cb1d751d5509b8e756101c828ad91", + "zh:dcc4384ee79ea9492c87eb01e664f7f6b1f1d156471476f30b28336c9d9a4aec", + "zh:e4abfaf013f31791cd029af7b6f989f73e3efca28fe2917057b428d051c4085f", + "zh:f2a4d9446d23afe2a42421e7d5f902d34451fb31b7787b5e3aef95c08fec5ced", + "zh:f54a6af10b077db9dc11556c27f59ba5c60e1b2ba96fe3aa9cd90d8c67d980f6", + ] +} diff --git a/README.md b/README.md deleted file mode 100644 index 10649cf..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# git-github -git started diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..e292fdc --- /dev/null +++ b/main.tf @@ -0,0 +1,136 @@ +#This Terraform Code Deploys Basic VPC Infra. +provider "aws" { + access_key = "${var.aws_access_key}" + secret_key = "${var.aws_secret_key}" + region = "${var.aws_region}" +} + +resource "aws_vpc" "default" { + cidr_block = "${var.vpc_cidr}" + enable_dns_hostnames = true + tags = { + Name = "${var.vpc_name}" + Owner = "Sreeharsha Veerapalli" + environment = "${var.environment}" + } +} + +resource "aws_internet_gateway" "default" { + vpc_id = "${aws_vpc.default.id}" + tags = { + Name = "${var.IGW_name}" + } +} + +resource "aws_subnet" "subnet1-public" { + vpc_id = "${aws_vpc.default.id}" + cidr_block = "${var.public_subnet1_cidr}" + availability_zone = "us-east-1a" + + tags = { + Name = "${var.public_subnet1_name}" + } +} + +resource "aws_subnet" "subnet2-public" { + vpc_id = "${aws_vpc.default.id}" + cidr_block = "${var.public_subnet2_cidr}" + availability_zone = "us-east-1b" + + tags = { + Name = "${var.public_subnet2_name}" + } +} + +resource "aws_subnet" "subnet3-public" { + vpc_id = "${aws_vpc.default.id}" + cidr_block = "${var.public_subnet3_cidr}" + availability_zone = "us-east-1c" + + tags = { + Name = "${var.public_subnet3_name}" + } + +} + + +resource "aws_route_table" "terraform-public" { + vpc_id = "${aws_vpc.default.id}" + + route { + cidr_block = "0.0.0.0/0" + gateway_id = "${aws_internet_gateway.default.id}" + } + + tags = { + Name = "${var.Main_Routing_Table}" + } +} + +resource "aws_route_table_association" "terraform-public" { + subnet_id = "${aws_subnet.subnet1-public.id}" + route_table_id = "${aws_route_table.terraform-public.id}" +} + +resource "aws_security_group" "allow_all" { + name = "allow_all" + description = "Allow all inbound traffic" + vpc_id = "${aws_vpc.default.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +# data "aws_ami" "my_ami" { +# most_recent = true +# #name_regex = "^mavrick" +# owners = ["721834156908"] +# } + + +# resource "aws_instance" "web-1" { +# ami = var.imagename +# #ami = "ami-0d857ff0f5fc4e03b" +# #ami = "${data.aws_ami.my_ami.id}" +# availability_zone = "us-east-1a" +# instance_type = "t2.micro" +# key_name = "LaptopKey" +# subnet_id = "${aws_subnet.subnet1-public.id}" +# vpc_security_group_ids = ["${aws_security_group.allow_all.id}"] +# associate_public_ip_address = true +# tags = { +# Name = "Server-1" +# Env = "Prod" +# Owner = "Sree" +# CostCenter = "ABCD" +# } +# } + +##output "ami_id" { +# value = "${data.aws_ami.my_ami.id}" +#} +#!/bin/bash +# echo "Listing the files in the repo." +# ls -al +# echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++" +# echo "Running Packer Now...!!" +# packer build -var=aws_access_key=AAAAAAAAAAAAAAAAAA -var=aws_secret_key=BBBBBBBBBBBBB packer.json +#packer validate --var-file creds.json packer.json +#packer build --var-file creds.json packer.json +#packer.exe build --var-file creds.json -var=aws_access_key=AAAAAAAAAAAAAAAAAA -var=aws_secret_key=BBBBBBBBBBBBB packer.json +# echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++" +# echo "Running Terraform Now...!!" +# terraform init +# terraform apply --var-file terraform.tfvars -var="aws_access_key=AAAAAAAAAAAAAAAAAA" -var="aws_secret_key=BBBBBBBBBBBBB" --auto-approve +#https://discuss.devopscube.com/t/how-to-get-the-ami-id-after-a-packer-build/36 diff --git a/outputs.tfx b/outputs.tfx new file mode 100644 index 0000000..dac0fed --- /dev/null +++ b/outputs.tfx @@ -0,0 +1,70 @@ +output "id" { + description = "List of IDs of instances" + value = ["${aws_instance.web-1.*.id}"] +} + +output "availability_zone" { + description = "List of availability zones of instances" + value = ["${aws_instance.web-1.*.availability_zone}"] +} + +// GH issue: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/issues/8 +//output "placement_group" { +// description = "List of placement groups of instances" +// value = ["${element(concat(aws_instance.web-1.*.placement_group, list("")), 0)}"] +//} +// +output "key_name" { + description = "List of key names of instances" + value = ["${aws_instance.web-1.*.key_name}"] +} + +output "public_dns" { + description = "List of public DNS names assigned to the instances. For EC2-VPC, web-1 is only available if you've enabled DNS hostnames for your VPC" + value = ["${aws_instance.web-1.*.public_dns}"] +} + +output "public_ip" { + description = "List of public IP addresses assigned to the instances, if applicable" + value = ["${aws_instance.web-1.*.public_ip}"] +} + +output "network_interface_id" { + description = "List of IDs of the network interface of instances" + value = ["${aws_instance.web-1.*.network_interface_id}"] +} + +output "primary_network_interface_id" { + description = "List of IDs of the primary network interface of instances" + value = ["${aws_instance.web-1.*.primary_network_interface_id}"] +} + +output "private_dns" { + description = "List of private DNS names assigned to the instances. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC" + value = ["${aws_instance.web-1.*.private_dns}"] +} + +output "private_ip" { + description = "List of private IP addresses assigned to the instances" + value = ["${aws_instance.web-1.*.private_ip}"] +} + +output "security_groups" { + description = "List of associated security groups of instances" + value = ["${aws_instance.web-1.*.security_groups}"] +} + +output "vpc_security_group_ids" { + description = "List of associated security groups of instances, if running in non-default VPC" + value = ["${aws_instance.web-1.*.vpc_security_group_ids}"] +} + +output "subnet_id" { + description = "List of IDs of VPC subnets of instances" + value = ["${aws_instance.web-1.*.subnet_id}"] +} + +output "tags" { + description = "List of tags of instances" + value = ["${aws_instance.web-1.*.tags}"] +} diff --git a/terraform.exe b/terraform.exe new file mode 100644 index 0000000..81ced75 Binary files /dev/null and b/terraform.exe differ diff --git a/terraform.tfstate b/terraform.tfstate new file mode 100644 index 0000000..73b66a8 --- /dev/null +++ b/terraform.tfstate @@ -0,0 +1,303 @@ +{ + "version": 4, + "terraform_version": "0.14.6", + "serial": 10, + "lineage": "63442dce-6cb5-2dbb-7e84-c15eef27d8d6", + "outputs": {}, + "resources": [ + { + "mode": "managed", + "type": "aws_internet_gateway", + "name": "default", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:internet-gateway/igw-0fd542585f9114fe4", + "id": "igw-0fd542585f9114fe4", + "owner_id": "897760560717", + "tags": { + "Name": "terraform-aws-igw" + }, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "bnVsbA==", + "dependencies": [ + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_route_table", + "name": "terraform-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "id": "rtb-01fb3e6093c1e259a", + "owner_id": "897760560717", + "propagating_vgws": [], + "route": [ + { + "cidr_block": "0.0.0.0/0", + "egress_only_gateway_id": "", + "gateway_id": "igw-0fd542585f9114fe4", + "instance_id": "", + "ipv6_cidr_block": "", + "local_gateway_id": "", + "nat_gateway_id": "", + "network_interface_id": "", + "transit_gateway_id": "", + "vpc_endpoint_id": "", + "vpc_peering_connection_id": "" + } + ], + "tags": { + "Name": "Terraform_Main_table-testing" + }, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "bnVsbA==", + "dependencies": [ + "aws_internet_gateway.default", + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_route_table_association", + "name": "terraform-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "gateway_id": null, + "id": "rtbassoc-03285c672a51e465b", + "route_table_id": "rtb-01fb3e6093c1e259a", + "subnet_id": "subnet-085fa2a4a218268d5" + }, + "sensitive_attributes": [], + "private": "bnVsbA==", + "dependencies": [ + "aws_internet_gateway.default", + "aws_route_table.terraform-public", + "aws_subnet.subnet1-public", + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_security_group", + "name": "allow_all", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:security-group/sg-0c6cb2d110fdac0e4", + "description": "Allow all inbound traffic", + "egress": [ + { + "cidr_blocks": [ + "0.0.0.0/0" + ], + "description": "", + "from_port": 0, + "ipv6_cidr_blocks": [], + "prefix_list_ids": [], + "protocol": "-1", + "security_groups": [], + "self": false, + "to_port": 0 + } + ], + "id": "sg-0c6cb2d110fdac0e4", + "ingress": [ + { + "cidr_blocks": [ + "0.0.0.0/0" + ], + "description": "", + "from_port": 0, + "ipv6_cidr_blocks": [], + "prefix_list_ids": [], + "protocol": "-1", + "security_groups": [], + "self": false, + "to_port": 0 + } + ], + "name": "allow_all", + "name_prefix": "", + "owner_id": "897760560717", + "revoke_rules_on_delete": false, + "tags": null, + "timeouts": null, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6NjAwMDAwMDAwMDAwfSwic2NoZW1hX3ZlcnNpb24iOiIxIn0=", + "dependencies": [ + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet1-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:subnet/subnet-085fa2a4a218268d5", + "assign_ipv6_address_on_creation": false, + "availability_zone": "us-east-1a", + "availability_zone_id": "use1-az1", + "cidr_block": "10.1.1.0/24", + "customer_owned_ipv4_pool": "", + "id": "subnet-085fa2a4a218268d5", + "ipv6_cidr_block": "", + "ipv6_cidr_block_association_id": "", + "map_customer_owned_ip_on_launch": false, + "map_public_ip_on_launch": false, + "outpost_arn": "", + "owner_id": "897760560717", + "tags": { + "Name": "Terraform_Public_Subnet1-testing" + }, + "timeouts": null, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMSJ9", + "dependencies": [ + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet2-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:subnet/subnet-022aff1f41a31d63d", + "assign_ipv6_address_on_creation": false, + "availability_zone": "us-east-1b", + "availability_zone_id": "use1-az2", + "cidr_block": "10.1.2.0/24", + "customer_owned_ipv4_pool": "", + "id": "subnet-022aff1f41a31d63d", + "ipv6_cidr_block": "", + "ipv6_cidr_block_association_id": "", + "map_customer_owned_ip_on_launch": false, + "map_public_ip_on_launch": false, + "outpost_arn": "", + "owner_id": "897760560717", + "tags": { + "Name": "Terraform_Public_Subnet2-testing" + }, + "timeouts": null, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMSJ9", + "dependencies": [ + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet3-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:subnet/subnet-016b7dfdba4e5715a", + "assign_ipv6_address_on_creation": false, + "availability_zone": "us-east-1c", + "availability_zone_id": "use1-az4", + "cidr_block": "10.1.3.0/24", + "customer_owned_ipv4_pool": "", + "id": "subnet-016b7dfdba4e5715a", + "ipv6_cidr_block": "", + "ipv6_cidr_block_association_id": "", + "map_customer_owned_ip_on_launch": false, + "map_public_ip_on_launch": false, + "outpost_arn": "", + "owner_id": "897760560717", + "tags": { + "Name": "Terraform_Public_Subnet3-testing" + }, + "timeouts": null, + "vpc_id": "vpc-0bf0c471b819e3bdb" + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMSJ9", + "dependencies": [ + "aws_vpc.default" + ] + } + ] + }, + { + "mode": "managed", + "type": "aws_vpc", + "name": "default", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "arn": "arn:aws:ec2:us-east-1:897760560717:vpc/vpc-0bf0c471b819e3bdb", + "assign_generated_ipv6_cidr_block": false, + "cidr_block": "10.1.0.0/16", + "default_network_acl_id": "acl-0d3cf52a0779b917a", + "default_route_table_id": "rtb-011c7fbf2db9c7927", + "default_security_group_id": "sg-07eb83fb8b9228823", + "dhcp_options_id": "dopt-c34207b9", + "enable_classiclink": false, + "enable_classiclink_dns_support": false, + "enable_dns_hostnames": true, + "enable_dns_support": true, + "id": "vpc-0bf0c471b819e3bdb", + "instance_tenancy": "default", + "ipv6_association_id": "", + "ipv6_cidr_block": "", + "main_route_table_id": "rtb-011c7fbf2db9c7927", + "owner_id": "897760560717", + "tags": { + "Name": "terraform-aws-testing", + "Owner": "Sreeharsha Veerapalli", + "environment": "dev" + } + }, + "sensitive_attributes": [], + "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjEifQ==" + } + ] + } + ] +} diff --git a/terraform.tfstate.backup b/terraform.tfstate.backup new file mode 100644 index 0000000..de9c491 --- /dev/null +++ b/terraform.tfstate.backup @@ -0,0 +1,58 @@ +{ + "version": 4, + "terraform_version": "0.14.6", + "serial": 1, + "lineage": "63442dce-6cb5-2dbb-7e84-c15eef27d8d6", + "outputs": {}, + "resources": [ + { + "mode": "managed", + "type": "aws_internet_gateway", + "name": "default", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_route_table", + "name": "terraform-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_route_table_association", + "name": "terraform-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_security_group", + "name": "allow_all", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet1-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet2-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + }, + { + "mode": "managed", + "type": "aws_subnet", + "name": "subnet3-public", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [] + } + ] +} diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..50ccad0 --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,17 @@ +aws_access_key = "AKIA5CBVTAZG5Z7F4OKT" +aws_secret_key = "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" +aws_region = "us-east-1" +vpc_cidr = "10.1.0.0/16" +public_subnet1_cidr = "10.1.1.0/24" +public_subnet2_cidr = "10.1.2.0/24" +public_subnet3_cidr = "10.1.3.0/24" +private_subnet_cidr = "10.1.20.0/24" +vpc_name = "terraform-aws-testing" +IGW_name = "terraform-aws-igw" +public_subnet1_name = "Terraform_Public_Subnet1-testing" +public_subnet2_name = "Terraform_Public_Subnet2-testing" +public_subnet3_name = "Terraform_Public_Subnet3-testing" +private_subnet_name = "Terraform_Private_Subnet-testing" +Main_Routing_Table = "Terraform_Main_table-testing" +key_name = "LaptopKey" +environment = "dev" diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..c3634d8 --- /dev/null +++ b/variables.tf @@ -0,0 +1,40 @@ +variable "aws_access_key" {} +variable "aws_secret_key" {} +variable "aws_region" {} +variable "amis" { + description = "AMIs by region" + default = { + us-east-1 = "ami-97785bed" # ubuntu 14.04 LTS + us-east-2 = "ami-f63b1193" # ubuntu 14.04 LTS + us-west-1 = "ami-824c4ee2" # ubuntu 14.04 LTS + us-west-2 = "ami-f2d3638a" # ubuntu 14.04 LTS + } +} +variable "vpc_cidr" {} +variable "vpc_name" {} +variable "IGW_name" {} +variable "key_name" {} +variable "public_subnet1_cidr" {} +variable "public_subnet2_cidr" {} +variable "public_subnet3_cidr" {} +variable "private_subnet_cidr" {} +variable "public_subnet1_name" {} +variable "public_subnet2_name" {} +variable "public_subnet3_name" {} +variable "private_subnet_name" {} +variable Main_Routing_Table {} +variable "azs" { + description = "Run the EC2 Instances in these Availability Zones" + type = "list" + default = ["us-east-1a", "us-east-1b", "us-east-1c"] +} +variable "environment" { default = "dev" } +variable "instance_type" { + type = "map" + default = { + dev = "t2.nano" + test = "t2.micro" + prod = "t2.medium" + } +} + diff --git a/vol.code b/vol.code deleted file mode 100644 index cd354b5..0000000 --- a/vol.code +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -#test pooling test -#test -for X in {1..1} -do -echo "the value of x is $X" -aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone us-east-1a -sleep 1 -done