Skip to content

Commit

Permalink
try unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LamSut committed Nov 1, 2024
1 parent 4a94f64 commit d8e89e7
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 28 deletions.
8 changes: 4 additions & 4 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
terraform {
backend "s3" {
bucket = "b2111933-bucket"
key = "state"
region = "us-east-1"
bucket = "b2111933-bucket"
key = "state"
region = "us-east-1"
dynamodb_table = "b2111933-table"
}
}
}
8 changes: 4 additions & 4 deletions ec2/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resource "aws_instance" "server" {
ami = "ami-066784287e358dad1"
instance_type = "t2.micro"
subnet_id = var.subnet
ami = "ami-066784287e358dad1"
instance_type = "t2.micro"
subnet_id = var.subnet
security_groups = [var.security_group]

tags = {
Name = "myserver"
}
}
}
7 changes: 7 additions & 0 deletions ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "ami" {
value = aws_instance.server.ami
}

output "instance_type" {
value = aws_instance.server.instance_type
}
2 changes: 1 addition & 1 deletion ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
variable "security_group" {}

variable "subnet" {}
variable "subnet" {}
6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module "vpc" {
}

module "ec2" {
source = "./ec2"
subnet = module.vpc.public_subnet
source = "./ec2"
subnet = module.vpc.public_subnet
security_group = module.vpc.security_group
}
}
2 changes: 1 addition & 1 deletion provider.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
provider "aws" {
region = "us-east-1"
}
}
19 changes: 19 additions & 0 deletions tests/freeEC2.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variables {
ami = "ami-066784287e358dad1"
instance_type = "t2.micro"
}

run "ec2_tests" {
command = plan

assert {
condition = module.ec2.ami == var.ami
error_message = "Not a free AMI type!"
}

assert {
condition = module.ec2.instance_type == var.instance_type
error_message = "Not a free instance type!"
}
}

1 change: 1 addition & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

28 changes: 14 additions & 14 deletions vpc/main.tf
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
#1 virtual private cloud, 1 subnet, 1 security group
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
enable_dns_support = true

tags = {
"Name" = "myvpc"
}
}

resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.0.1.0/24"
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
availability_zone = "us-east-1a"

tags = {
"Name" = "public_subnet1"
}
}

resource "aws_security_group" "security_group" {
vpc_id = aws_vpc.myvpc.id
name = "my_security_group"
vpc_id = aws_vpc.myvpc.id
name = "my_security_group"
description = "Public Security Group"

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

egress {
from_port = 0
to_port = 0
protocol = "-1"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

}
}
2 changes: 1 addition & 1 deletion vpc/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ output "public_subnet" {

output "security_group" {
value = aws_security_group.security_group.id
}
}
1 change: 1 addition & 0 deletions vpc/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit d8e89e7

Please sign in to comment.