Skip to content

Commit

Permalink
Terraform practice - simple application
Browse files Browse the repository at this point in the history
  • Loading branch information
vutoff committed Dec 19, 2023
1 parent fd5ac91 commit ca7b851
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
20 changes: 20 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
data "aws_ami" "aws_ami" {
owners = ["amazon", "self"]
most_recent = true

filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}

module "webserver" {
source = "./modules/web"

instance_type = var.instance_type
ami_id = data.aws_ami.aws_ami.id
}

output "public_ip" {
value = module.webserver.public_ip
}
38 changes: 38 additions & 0 deletions terraform/modules/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | 1.6.6 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | 5.31.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.31.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_instance.amzn](https://registry.terraform.io/providers/hashicorp/aws/5.31.0/docs/resources/instance) | resource |
| [aws_security_group.allow_ssh](https://registry.terraform.io/providers/hashicorp/aws/5.31.0/docs/resources/security_group) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | AMI ID | `string` | n/a | yes |
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Instance Type | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | Public IP of the EC2 instance |
<!-- END_TF_DOCS -->
26 changes: 26 additions & 0 deletions terraform/modules/web/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "aws_instance" "amzn" {
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.allow_ssh.name]
}

resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"

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

egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
4 changes: 4 additions & 0 deletions terraform/modules/web/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "public_ip" {
value = aws_instance.amzn.public_ip
description = "Public IP of the EC2 instance"
}
9 changes: 9 additions & 0 deletions terraform/modules/web/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "instance_type" {
type = string
description = "Instance Type"
}

variable "ami_id" {
type = string
description = "AMI ID"
}
10 changes: 10 additions & 0 deletions terraform/modules/web/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "1.6.6"

required_providers {
aws = {
source = "hashicorp/aws"
version = "5.31.0"
}
}
}
9 changes: 9 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "aws_region" {
type = string
default = "eu-west-1"
}

variable "instance_type" {
type = string
default = "t3.micro"
}
2 changes: 2 additions & 0 deletions terraform/vars/prod.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aws_region = "us-east-1"
instance_type = "m5.large"
2 changes: 2 additions & 0 deletions terraform/vars/staging.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aws_region = "eu-west-1"
instance_type = "t3.micro"
14 changes: 14 additions & 0 deletions terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = "1.6.6"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.0"
}
}
}

provider "aws" {
region = var.aws_region
}

0 comments on commit ca7b851

Please sign in to comment.