From c5b619e322665cf4072b4b519381bf1ec3f22686 Mon Sep 17 00:00:00 2001 From: Joseph Peter <90427049+joey1089@users.noreply.github.com> Date: Sat, 7 Jan 2023 19:37:53 -0500 Subject: [PATCH 1/6] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e27f058 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# terraformec2 +LUIT project - for Terraform From 822f2054f662f0ab5e8007d45d8287012616833c Mon Sep 17 00:00:00 2001 From: Joseph Peter Date: Mon, 9 Jan 2023 23:13:16 -0500 Subject: [PATCH 2/6] added .gitignore and variables.tf --- .gitignore | 29 +++++++++++++++++++++++++++++ ec2.tf | 4 +--- variables.tf | 0 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a3e2fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +# +# example.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* diff --git a/ec2.tf b/ec2.tf index f55bdd8..dcdbf06 100644 --- a/ec2.tf +++ b/ec2.tf @@ -5,7 +5,6 @@ terraform { version = "~> 3.27" } } - required_version = ">= 0.14.9" } @@ -17,8 +16,7 @@ provider "aws" { resource "aws_instance" "app_server" { ami = "ami-830c94e3" instance_type = "t2.micro" - tags = { - Name = "ExampleAppServerInstance" + Name = "AppServerInstance" } } diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e69de29 From a176dc3ecd9b4cbc0fce7f7671e64f2e05896b25 Mon Sep 17 00:00:00 2001 From: Joseph Peter Date: Tue, 10 Jan 2023 22:35:25 -0500 Subject: [PATCH 3/6] added and updated variables, main and providers --- .terraform.lock.hcl | 25 +++++++++++++++++++ ec2.tf | 15 +++++++----- module-ec2/main.tf | 53 +++++++++++++++++++++++++++++++++++++++++ module-ec2/providers.tf | 18 ++++++++++++++ module-ec2/variables.tf | 27 +++++++++++++++++++++ 5 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 .terraform.lock.hcl create mode 100644 module-ec2/main.tf create mode 100644 module-ec2/providers.tf create mode 100644 module-ec2/variables.tf diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..7864285 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.76.1" + constraints = "~> 3.27" + hashes = [ + "h1:5WSHHV9CgBvZ0rDDDxLnNHsjDfm4knb7ihJ2AIGB58A=", + "zh:1cf933104a641ffdb64d71a76806f4df35d19101b47e0eb02c9c36bd64bfdd2d", + "zh:273afaf908775ade6c9d32462938e7739ee8b00a0de2ef3cdddc5bc115bb1d4f", + "zh:2bc24ae989e38f575de034083082c69b41c54b8df69d35728853257c400ce0f4", + "zh:53ba88dbdaf9f818d35001c3d519a787f457283d9341f562dc3d0af51fd9606e", + "zh:5cdac7afea68bbd89d3bdb345d99470226482eff41f375f220fe338d2e5808da", + "zh:63127808890ac4be6cff6554985510b15ac715df698d550a3e722722dc56523c", + "zh:97a1237791f15373743189b078a0e0f2fa4dd7d7474077423376cd186312dc55", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:a4f625e97e5f25073c08080e4a619f959bc0149fc853a6b1b49ab41d58b59665", + "zh:b56cca54019237941f7614e8d2712586a6ab3092e8e9492c70f06563259171e9", + "zh:d4bc33bfd6ac78fb61e6d48a61c179907dfdbdf149b89fb97272c663989a7fcd", + "zh:e0089d73fa56d128c574601305634a774eebacf4a84babba71da10040cecf99a", + "zh:e957531f1d92a6474c9b02bd9200da91b99ba07a0ab761c8e3176400dd41721c", + "zh:eceb85818d57d8270db4df7564cf4ed51b5c650a361aaa017c42227158e1946b", + "zh:f565e5caa1b349ec404c6d03d01c68b02233f5485ed038d0aab810dd4023a880", + ] +} diff --git a/ec2.tf b/ec2.tf index dcdbf06..f072a25 100644 --- a/ec2.tf +++ b/ec2.tf @@ -13,10 +13,13 @@ provider "aws" { region = "us-west-2" } -resource "aws_instance" "app_server" { - ami = "ami-830c94e3" - instance_type = "t2.micro" - tags = { - Name = "AppServerInstance" - } +# resource "aws_instance" "app_server" { +# ami = "ami-830c94e3" +# instance_type = "t2.micro" +# tags = { +# Name = "AppServerInstance" +# } +# } +module "module-ec2" { + source = "./module-ec2" #--- module path } diff --git a/module-ec2/main.tf b/module-ec2/main.tf new file mode 100644 index 0000000..52c1fb9 --- /dev/null +++ b/module-ec2/main.tf @@ -0,0 +1,53 @@ +#--- module-ec2/main.tf + +# Create aws ec2 instance for the app server +resource "aws_instance" "my_app_server" { + ami = var.ami_id + instance_type = var.instance + vpc_security_group_ids = [aws_security_group.allow_http.id] + subnet_id = aws_subnet.public_subnet.id + tags = { + Name = "EC2-app-server" + } +} + +resource "aws_vpc" "myvpc_main" { + cidr_block = "10.0.0.0/16" + instance_tenancy = "default" + tags = { + Name = "myvpc-main" + } +} + +resource "aws_subnet" "public_subnet" { + vpc_id = aws_vpc.myvpc_main.id + cidr_block = var.cidr + availability_zone = var.az + tags = { + Name = "public-subnet" + } +} +resource "aws_security_group" "allow_http" { + name = "allow_tls" + description = "Allow http inbound traffic" + vpc_id = aws_vpc.myvpc_main.id + + ingress { + description = "http allowed" + from_port = 80 + to_port = 80 + 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"] + } + + tags = { + Name = "allow-http" + } +} diff --git a/module-ec2/providers.tf b/module-ec2/providers.tf new file mode 100644 index 0000000..e668b90 --- /dev/null +++ b/module-ec2/providers.tf @@ -0,0 +1,18 @@ +#--- module-ec2/providers.tf +#create variables for all of module-ec2 +#child module for ec2 variables.tf + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } +# required_version = ">= 0.14.9" +} + +# Configure the AWS Provider +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/module-ec2/variables.tf b/module-ec2/variables.tf new file mode 100644 index 0000000..fbbbfd5 --- /dev/null +++ b/module-ec2/variables.tf @@ -0,0 +1,27 @@ +#--- module-ec2/variables.tf +# Create variable for aws_region +variable "aws_region" { + description = "AWS region" + type = string + default = "us-west-2" +} + +variable "ami_id" { + type = string + default = "ami-0ceecbb0f30a902a6" #-- us-west-2 ami id +} + +variable "instance" { + type = string + default = "t2.micro" +} + +variable "cidr" { + type = string + default = "10.0.1.0/24" +} + +variable "az" { + type = string + default = "us-west-2a" +} \ No newline at end of file From d60039c13c75bac01afb1589d504053c42784608 Mon Sep 17 00:00:00 2001 From: Joseph Peter Date: Wed, 11 Jan 2023 10:40:49 -0500 Subject: [PATCH 4/6] updated code --- ec2.tf | 7 ------- module-ec2/main.tf | 5 +++-- module-ec2/providers.tf | 5 +---- module-ec2/variables.tf | 8 ++++---- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/ec2.tf b/ec2.tf index f072a25..5a6d467 100644 --- a/ec2.tf +++ b/ec2.tf @@ -13,13 +13,6 @@ provider "aws" { region = "us-west-2" } -# resource "aws_instance" "app_server" { -# ami = "ami-830c94e3" -# instance_type = "t2.micro" -# tags = { -# Name = "AppServerInstance" -# } -# } module "module-ec2" { source = "./module-ec2" #--- module path } diff --git a/module-ec2/main.tf b/module-ec2/main.tf index 52c1fb9..488b7b3 100644 --- a/module-ec2/main.tf +++ b/module-ec2/main.tf @@ -7,7 +7,7 @@ resource "aws_instance" "my_app_server" { vpc_security_group_ids = [aws_security_group.allow_http.id] subnet_id = aws_subnet.public_subnet.id tags = { - Name = "EC2-app-server" + Name = "EC2-App-Server" } } @@ -23,12 +23,13 @@ resource "aws_subnet" "public_subnet" { vpc_id = aws_vpc.myvpc_main.id cidr_block = var.cidr availability_zone = var.az + map_public_ip_on_launch = true tags = { Name = "public-subnet" } } resource "aws_security_group" "allow_http" { - name = "allow_tls" + name = "allow-http" description = "Allow http inbound traffic" vpc_id = aws_vpc.myvpc_main.id diff --git a/module-ec2/providers.tf b/module-ec2/providers.tf index e668b90..9cc16d8 100644 --- a/module-ec2/providers.tf +++ b/module-ec2/providers.tf @@ -1,7 +1,5 @@ #--- module-ec2/providers.tf -#create variables for all of module-ec2 -#child module for ec2 variables.tf - +# provider is not needed if you already declared it in root terraform { required_providers { aws = { @@ -9,7 +7,6 @@ terraform { version = "~> 3.27" } } -# required_version = ">= 0.14.9" } # Configure the AWS Provider diff --git a/module-ec2/variables.tf b/module-ec2/variables.tf index fbbbfd5..c700b7f 100644 --- a/module-ec2/variables.tf +++ b/module-ec2/variables.tf @@ -5,22 +5,22 @@ variable "aws_region" { type = string default = "us-west-2" } - +# Create variable for ami-id variable "ami_id" { type = string default = "ami-0ceecbb0f30a902a6" #-- us-west-2 ami id } - +# Create variable for instance type variable "instance" { type = string default = "t2.micro" } - +# Create variable for cidr block variable "cidr" { type = string default = "10.0.1.0/24" } - +# Create variable for availability zone variable "az" { type = string default = "us-west-2a" From 4706915eb7bacc6871721edcbe5a0b4b23442bff Mon Sep 17 00:00:00 2001 From: Joseph Peter Date: Wed, 11 Jan 2023 12:50:05 -0500 Subject: [PATCH 5/6] output file created --- ec2.tf | 2 +- module-ec2/.terraform.lock.hcl | 25 +++++++++++++++++++++++++ module-ec2/main.tf | 28 ++++++++++++++-------------- module-ec2/outputs.tf | 6 ++++++ module-ec2/variables.tf | 4 ++-- 5 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 module-ec2/.terraform.lock.hcl create mode 100644 module-ec2/outputs.tf diff --git a/ec2.tf b/ec2.tf index 5a6d467..a506a25 100644 --- a/ec2.tf +++ b/ec2.tf @@ -14,5 +14,5 @@ provider "aws" { } module "module-ec2" { - source = "./module-ec2" #--- module path + source = "./module-ec2" #--- module path } diff --git a/module-ec2/.terraform.lock.hcl b/module-ec2/.terraform.lock.hcl new file mode 100644 index 0000000..7864285 --- /dev/null +++ b/module-ec2/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.76.1" + constraints = "~> 3.27" + hashes = [ + "h1:5WSHHV9CgBvZ0rDDDxLnNHsjDfm4knb7ihJ2AIGB58A=", + "zh:1cf933104a641ffdb64d71a76806f4df35d19101b47e0eb02c9c36bd64bfdd2d", + "zh:273afaf908775ade6c9d32462938e7739ee8b00a0de2ef3cdddc5bc115bb1d4f", + "zh:2bc24ae989e38f575de034083082c69b41c54b8df69d35728853257c400ce0f4", + "zh:53ba88dbdaf9f818d35001c3d519a787f457283d9341f562dc3d0af51fd9606e", + "zh:5cdac7afea68bbd89d3bdb345d99470226482eff41f375f220fe338d2e5808da", + "zh:63127808890ac4be6cff6554985510b15ac715df698d550a3e722722dc56523c", + "zh:97a1237791f15373743189b078a0e0f2fa4dd7d7474077423376cd186312dc55", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:a4f625e97e5f25073c08080e4a619f959bc0149fc853a6b1b49ab41d58b59665", + "zh:b56cca54019237941f7614e8d2712586a6ab3092e8e9492c70f06563259171e9", + "zh:d4bc33bfd6ac78fb61e6d48a61c179907dfdbdf149b89fb97272c663989a7fcd", + "zh:e0089d73fa56d128c574601305634a774eebacf4a84babba71da10040cecf99a", + "zh:e957531f1d92a6474c9b02bd9200da91b99ba07a0ab761c8e3176400dd41721c", + "zh:eceb85818d57d8270db4df7564cf4ed51b5c650a361aaa017c42227158e1946b", + "zh:f565e5caa1b349ec404c6d03d01c68b02233f5485ed038d0aab810dd4023a880", + ] +} diff --git a/module-ec2/main.tf b/module-ec2/main.tf index 488b7b3..0f4cbd4 100644 --- a/module-ec2/main.tf +++ b/module-ec2/main.tf @@ -5,7 +5,7 @@ resource "aws_instance" "my_app_server" { ami = var.ami_id instance_type = var.instance vpc_security_group_ids = [aws_security_group.allow_http.id] - subnet_id = aws_subnet.public_subnet.id + subnet_id = aws_subnet.public_subnet.id tags = { Name = "EC2-App-Server" } @@ -20,9 +20,9 @@ resource "aws_vpc" "myvpc_main" { } resource "aws_subnet" "public_subnet" { - vpc_id = aws_vpc.myvpc_main.id - cidr_block = var.cidr - availability_zone = var.az + vpc_id = aws_vpc.myvpc_main.id + cidr_block = var.cidr + availability_zone = var.az map_public_ip_on_launch = true tags = { Name = "public-subnet" @@ -32,20 +32,20 @@ resource "aws_security_group" "allow_http" { name = "allow-http" description = "Allow http inbound traffic" vpc_id = aws_vpc.myvpc_main.id - + ingress { - description = "http allowed" - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] + description = "http allowed" + from_port = 80 + to_port = 80 + 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"] + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] } tags = { diff --git a/module-ec2/outputs.tf b/module-ec2/outputs.tf new file mode 100644 index 0000000..a5fb92d --- /dev/null +++ b/module-ec2/outputs.tf @@ -0,0 +1,6 @@ +#--- module-ec2/outputs.tf + +output "public_ip" { + value = aws_instance.my_app_server.public_ip + description = "Gives the public ip address of the created ec2 instance" +} \ No newline at end of file diff --git a/module-ec2/variables.tf b/module-ec2/variables.tf index c700b7f..134a18d 100644 --- a/module-ec2/variables.tf +++ b/module-ec2/variables.tf @@ -17,11 +17,11 @@ variable "instance" { } # Create variable for cidr block variable "cidr" { - type = string + type = string default = "10.0.1.0/24" } # Create variable for availability zone variable "az" { - type = string + type = string default = "us-west-2a" } \ No newline at end of file From 74b03af313d4f0b69c58faae4fa060bbe9f35d43 Mon Sep 17 00:00:00 2001 From: Joseph Peter Date: Wed, 11 Jan 2023 13:20:22 -0500 Subject: [PATCH 6/6] user-data added --- module-ec2/main.tf | 1 + module-ec2/outputs.tf | 2 +- module-ec2/user-install.sh | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 module-ec2/user-install.sh diff --git a/module-ec2/main.tf b/module-ec2/main.tf index 0f4cbd4..744ab4a 100644 --- a/module-ec2/main.tf +++ b/module-ec2/main.tf @@ -6,6 +6,7 @@ resource "aws_instance" "my_app_server" { instance_type = var.instance vpc_security_group_ids = [aws_security_group.allow_http.id] subnet_id = aws_subnet.public_subnet.id + user_data = file("user-install.sh") tags = { Name = "EC2-App-Server" } diff --git a/module-ec2/outputs.tf b/module-ec2/outputs.tf index a5fb92d..1163396 100644 --- a/module-ec2/outputs.tf +++ b/module-ec2/outputs.tf @@ -1,6 +1,6 @@ #--- module-ec2/outputs.tf output "public_ip" { - value = aws_instance.my_app_server.public_ip + value = aws_instance.my_app_server.public_ip description = "Gives the public ip address of the created ec2 instance" } \ No newline at end of file diff --git a/module-ec2/user-install.sh b/module-ec2/user-install.sh new file mode 100644 index 0000000..31677a0 --- /dev/null +++ b/module-ec2/user-install.sh @@ -0,0 +1,7 @@ +#--- module-ec2/user-install.sh + +#!/bin/bash +apt update -y && +apt install -y nginx +echo "This is nginx server here" > /var/www/html/index.html +systemctl reload nginx \ No newline at end of file