Skip to content
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 examples/specifyip/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# this is given for reference, in most cases you will want to set the region using environment variables
# provider "aws" {
# region = "us-west-1"
# }

# AWS reserves the first four IP addresses and the last IP address in any CIDR block for its own use (cumulatively)
module "TestBasic" {
source = "../../"
owner = "terraform-ci@suse.com"
vpc_name = "terraform-aws-access-test-basic"
vpc_cidr = "10.0.255.0/24" # gives 256 usable addresses from .1 to .254, but AWS reserves .1 to .4 and .255, leaving .5 to .254
subnet_name = "terraform-aws-access-test-basic"
subnet_cidr = "10.0.255.224/28" # gives 14 usable addresses from .225 to .238, but AWS reserves .225 to .227 and .238, leaving .227 to .237
availability_zone = "us-west-1b" # check what availability zones are available in your region before setting this
security_group_name = "terraform-aws-access-test-basic"
security_group_type = "egress"
security_group_ip = chomp(var.ip)
public_ssh_key = var.key # I don't normally recommend this, but it allows tests to supply their own key
ssh_key_name = var.key_name # A lot of troubleshooting during critical times can be saved by hard coding variables in root modules
# root modules should be secured properly (including the state), and should represent your running infrastructure
}
19 changes: 19 additions & 0 deletions examples/specifyip/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "vpc" {
value = module.TestBasic.vpc
}

output "subnet" {
value = module.TestBasic.subnet
}

output "cidr" {
value = module.TestBasic.cidr
}

output "security_group" {
value = module.TestBasic.security_group
}

output "ssh_key" {
value = module.TestBasic.ssh_key
}
9 changes: 9 additions & 0 deletions examples/specifyip/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "key" {
type = string
}
variable "key_name" {
type = string
}
variable "ip" {
type = string
}
17 changes: 17 additions & 0 deletions examples/specifyip/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
terraform {
required_version = ">= 1.2.0"
required_providers {
local = {
source = "hashicorp/local"
version = ">= 2.4"
}
aws = {
source = "hashicorp/aws"
version = ">= 5.11"
}
http = {
source = "hashicorp/http"
version = ">= 3.4"
}
}
}
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ locals {
ssh_key_name = var.ssh_key_name
public_ssh_key = var.public_ssh_key # create when public key is given, otherwise select with name

ipinfo_ip = chomp(data.http.my_public_ip[0].response_body)
ipinfo_ip = (can(chomp(data.http.my_public_ip[0].response_body)) ? chomp(data.http.my_public_ip[0].response_body) : "")
ip = (local.security_group_ip == "" ? local.ipinfo_ip : local.security_group_ip)
}

Expand Down
46 changes: 46 additions & 0 deletions tests/specifyip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package test

import (
"fmt"
"log"
"net"
"testing"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
)

// this test generates all objects, no overrides
func TestIp(t *testing.T) {
t.Parallel()
uniqueID := random.UniqueId()
directory := "specifyip"
region := "us-west-1"
ip := GetOutboundIP().String()

keyPair := ssh.GenerateRSAKeyPair(t, 2048)
keyPairName := fmt.Sprintf("terraform-aws-access-test-%s-%s", directory, uniqueID)
terraformVars := map[string]interface{}{
"key_name": keyPairName,
"key": keyPair.PublicKey,
"ip": ip,
}
terraformOptions := setup(t, directory, region, terraformVars)
defer teardown(t, directory)
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}

// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()

localAddr := conn.LocalAddr().(*net.UDPAddr)

return localAddr.IP
}