Skip to content

Dev #1

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

Merged
merged 3 commits into from
Oct 9, 2024
Merged
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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Bala Prasanth Babu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
# terraform-aws-web-app
# Terraform AWS Web App

This repository contains Terraform code to set up AWS infrastructure for a web application, including a VPC, subnet, internet gateway, route table, security group, and an EC2 instance running a web server.

## Prerequisites

Before you begin, ensure you have the following installed:

- [Terraform](https://www.terraform.io/downloads.html)
- An [AWS account](https://aws.amazon.com/)
- AWS CLI configured with appropriate permissions

## Getting Started

### Clone the Repository

```bash
git clone https://github.com/prasanth624/terraform-aws-web-app.git
cd terraform-aws-web-app
```

### Initialize Terraform

Run the following command to initialize the Terraform configuration:

```bash
terraform init
```

### Plan the Infrastructure

To see what Terraform will create, run:

```bash
terraform plan
```

### Apply the Configuration

To create the infrastructure, run:

```bash
terraform apply
```

You will be prompted to confirm the action. Type `yes` to proceed.

### Outputs

After the infrastructure is created, you can see the outputs by running:

```bash
terraform output
```

## Cleanup

To remove all resources created by Terraform, run:

```bash
terraform destroy
```

You will be prompted to confirm the destruction. Type `yes` to proceed.

## Contributing
Feel free to fork the repository and submit pull requests for any improvements or features you'd like to add!

## License
This project is licensed under the MIT License. See the LICENSE file for more details.
20 changes: 20 additions & 0 deletions ec2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "var.instance_type"
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
associate_public_ip_address = true

user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
echo "Hello Terraform!" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
EOF

tags = {
Name = "MyWebServer"
}
}
53 changes: 53 additions & 0 deletions network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = var.availability_zone
}

resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}

resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}

resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.main.id
}

resource "aws_security_group" "ec2_sg" {
vpc_id = aws_vpc.main.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
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"]
}
}

34 changes: 34 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
output "vpc_id" {
description = "The ID of the created VPC"
value = aws_vpc.main.id
}

output "subnet_id" {
description = "The ID of the created Subnet"
value = aws_subnet.main.id
}

output "internet_gateway_id" {
description = "The ID of the created Internet Gateway"
value = aws_internet_gateway.main.id
}

output "route_table_id" {
description = "The ID of the created Route Table"
value = aws_route_table.main.id
}

output "security_group_id" {
description = "The ID of the security group"
value = aws_security_group.ec2_sg.id
}

output "ec2_instance_id" {
description = "The ID of the created EC2 Instance"
value = aws_instance.web.id
}

output "ec2_instance_public_ip" {
description = "The public IP address of the EC2 Instance"
value = aws_instance.web.public_ip
}
16 changes: 16 additions & 0 deletions providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = var.aws_region

default_tags {
tags = var.default_tags
}
}
11 changes: 11 additions & 0 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
aws_region = "us-east-1"
availability_zone = "us-east-1a"
instance_type = "t2.micro"
ami_id = "ami-00f251754ac5da7f0"

default_tags = {
Environment = "Prod"
Project = "web-app"
Name = "web-app"
}

25 changes: 25 additions & 0 deletions vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "default_tags" {
description = "Default tags to apply to all resources"
type = map(string)
}

variable "aws_region" {
description = "The AWS region to deploy in."
type = string
default = "us-east-1"
}

variable "availability_zone" {
type = string
default = "us-east-1a"
}

variable "instance_type" {
type = string
}

variable "ami_id" {
description = "The AMI ID to use for the EC2 instance."
type = string
}