Skip to content

Create README.md #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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*
25 changes: 25 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# terraformec2
LUIT project - for Terraform
10 changes: 2 additions & 8 deletions ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ terraform {
version = "~> 3.27"
}
}

required_version = ">= 0.14.9"
}

Expand All @@ -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
}
25 changes: 25 additions & 0 deletions module-ec2/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions module-ec2/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
6 changes: 6 additions & 0 deletions module-ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -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"
}
15 changes: 15 additions & 0 deletions module-ec2/providers.tf
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions module-ec2/user-install.sh
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions module-ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}
Empty file added variables.tf
Empty file.