Skip to content

Commit ca7b851

Browse files
author
Iliyan Vutoff
committed
Terraform practice - simple application
1 parent fd5ac91 commit ca7b851

File tree

10 files changed

+134
-0
lines changed

10 files changed

+134
-0
lines changed

terraform/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data "aws_ami" "aws_ami" {
2+
owners = ["amazon", "self"]
3+
most_recent = true
4+
5+
filter {
6+
name = "name"
7+
values = ["amzn-ami-hvm-*-x86_64-gp2"]
8+
}
9+
}
10+
11+
module "webserver" {
12+
source = "./modules/web"
13+
14+
instance_type = var.instance_type
15+
ami_id = data.aws_ami.aws_ami.id
16+
}
17+
18+
output "public_ip" {
19+
value = module.webserver.public_ip
20+
}

terraform/modules/web/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | 1.6.6 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | 5.31.0 |
8+
9+
## Providers
10+
11+
| Name | Version |
12+
|------|---------|
13+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.31.0 |
14+
15+
## Modules
16+
17+
No modules.
18+
19+
## Resources
20+
21+
| Name | Type |
22+
|------|------|
23+
| [aws_instance.amzn](https://registry.terraform.io/providers/hashicorp/aws/5.31.0/docs/resources/instance) | resource |
24+
| [aws_security_group.allow_ssh](https://registry.terraform.io/providers/hashicorp/aws/5.31.0/docs/resources/security_group) | resource |
25+
26+
## Inputs
27+
28+
| Name | Description | Type | Default | Required |
29+
|------|-------------|------|---------|:--------:|
30+
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | AMI ID | `string` | n/a | yes |
31+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Instance Type | `string` | n/a | yes |
32+
33+
## Outputs
34+
35+
| Name | Description |
36+
|------|-------------|
37+
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | Public IP of the EC2 instance |
38+
<!-- END_TF_DOCS -->

terraform/modules/web/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "aws_instance" "amzn" {
2+
ami = var.ami_id
3+
instance_type = var.instance_type
4+
security_groups = [aws_security_group.allow_ssh.name]
5+
}
6+
7+
resource "aws_security_group" "allow_ssh" {
8+
name = "allow_ssh"
9+
description = "Allow SSH inbound traffic"
10+
11+
ingress {
12+
description = "SSH from VPC"
13+
from_port = 22
14+
to_port = 22
15+
protocol = "tcp"
16+
cidr_blocks = ["0.0.0.0/0"]
17+
}
18+
19+
egress {
20+
description = "Allow all outbound traffic"
21+
from_port = 0
22+
to_port = 0
23+
protocol = "-1"
24+
cidr_blocks = ["0.0.0.0/0"]
25+
}
26+
}

terraform/modules/web/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "public_ip" {
2+
value = aws_instance.amzn.public_ip
3+
description = "Public IP of the EC2 instance"
4+
}

terraform/modules/web/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "instance_type" {
2+
type = string
3+
description = "Instance Type"
4+
}
5+
6+
variable "ami_id" {
7+
type = string
8+
description = "AMI ID"
9+
}

terraform/modules/web/versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = "1.6.6"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "5.31.0"
8+
}
9+
}
10+
}

terraform/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "aws_region" {
2+
type = string
3+
default = "eu-west-1"
4+
}
5+
6+
variable "instance_type" {
7+
type = string
8+
default = "t3.micro"
9+
}

terraform/vars/prod.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws_region = "us-east-1"
2+
instance_type = "m5.large"

terraform/vars/staging.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws_region = "eu-west-1"
2+
instance_type = "t3.micro"

terraform/versions.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = "1.6.6"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.0"
8+
}
9+
}
10+
}
11+
12+
provider "aws" {
13+
region = var.aws_region
14+
}

0 commit comments

Comments
 (0)