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/.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/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 diff --git a/ec2.tf b/ec2.tf index f55bdd8..a506a25 100644 --- a/ec2.tf +++ b/ec2.tf @@ -5,7 +5,6 @@ terraform { version = "~> 3.27" } } - required_version = ">= 0.14.9" } @@ -14,11 +13,6 @@ provider "aws" { region = "us-west-2" } -resource "aws_instance" "app_server" { - ami = "ami-830c94e3" - instance_type = "t2.micro" - - tags = { - Name = "ExampleAppServerInstance" - } +module "module-ec2" { + 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 new file mode 100644 index 0000000..744ab4a --- /dev/null +++ b/module-ec2/main.tf @@ -0,0 +1,55 @@ +#--- 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 + user_data = file("user-install.sh") + 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 + map_public_ip_on_launch = true + tags = { + Name = "public-subnet" + } +} +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"] + } + + 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/outputs.tf b/module-ec2/outputs.tf new file mode 100644 index 0000000..1163396 --- /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/providers.tf b/module-ec2/providers.tf new file mode 100644 index 0000000..9cc16d8 --- /dev/null +++ b/module-ec2/providers.tf @@ -0,0 +1,15 @@ +#--- module-ec2/providers.tf +# provider is not needed if you already declared it in root +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } +} + +# Configure the AWS Provider +provider "aws" { + region = var.aws_region +} \ 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 diff --git a/module-ec2/variables.tf b/module-ec2/variables.tf new file mode 100644 index 0000000..134a18d --- /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" +} +# 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" +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e69de29