Skip to content

Commit 2d4e10f

Browse files
committed
adding create server module
1 parent f7c7633 commit 2d4e10f

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

enos/enos-descriptions.hcl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ globals {
2525
for Vault target nodes to access it the AWSKMS key are handled in the target modules.
2626
EOF
2727

28+
create_test_servers = <<-EOF
29+
Provision test servers required for development and QA activities. Additional servers will be
30+
deployed as necessary to support testing requirements (e.g., OpenLDAP).
31+
EOF
32+
2833
create_vault_cluster = <<-EOF
2934
Create the the Vault cluster. In this module we'll install, configure, start, initialize and
3035
unseal all the nodes in the Vault. After initialization it also enables various audit engines.

enos/enos-modules.hcl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ module "create_vpc" {
4848
common_tags = var.tags
4949
}
5050

51+
module "create_test_servers" {
52+
source = "./modules/create_test_servers"
53+
54+
ssh_keypair = var.aws_ssh_keypair_name
55+
}
56+
5157
module "choose_follower_host" {
5258
source = "./modules/choose_follower_host"
5359
}

enos/enos-scenario-smoke.hcl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ scenario "smoke" {
317317
}
318318
}
319319

320+
step "create_test_servers" {
321+
description = global.description.create_test_servers
322+
module = module.create_test_servers
323+
depends_on = [step.create_vpc]
324+
325+
providers = {
326+
enos = local.enos_provider[matrix.distro]
327+
}
328+
329+
variables {
330+
vpc_id = step.create_vpc.id
331+
vpc_security_group_ids = [step.create_vpc.security_group_id]
332+
}
333+
}
334+
320335
step "get_local_metadata" {
321336
description = global.description.get_local_metadata
322337
skip_step = matrix.artifact_source != "local"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
locals {
5+
// Variables
6+
instance_types = {
7+
"arm64" = var.instance_types["arm64"]
8+
"x86_64" = var.instance_types["amd64"]
9+
}
10+
instance_type = local.instance_types[data.aws_ami.amazon_linux.architecture]
11+
name_prefix = "enos-test-server-${random_id.unique_suffix.hex}"
12+
13+
}
14+
15+
data "aws_region" "current" {}
16+
17+
# Generates a unique suffix for the EC2 tag
18+
resource "random_id" "unique_suffix" {
19+
byte_length = 4
20+
}
21+
22+
# Lookup latest Amazon Linux 2 AMI dynamically
23+
data "aws_ami" "amazon_linux" {
24+
most_recent = true
25+
owners = ["amazon"]
26+
27+
filter {
28+
name = "${local.name_prefix}-ami"
29+
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
30+
}
31+
32+
filter {
33+
name = "virtualization-type"
34+
values = ["hvm"]
35+
}
36+
}
37+
38+
data "aws_ec2_instance_type_offerings" "instance" {
39+
filter {
40+
name = "instance-type"
41+
values = [local.instance_type]
42+
}
43+
44+
location_type = "availability-zone"
45+
}
46+
47+
data "aws_availability_zones" "available" {
48+
state = "available"
49+
50+
filter {
51+
name = "${local.name_prefix}-zone"
52+
values = data.aws_ec2_instance_type_offerings.instance.locations
53+
}
54+
}
55+
56+
# Get subnets in the given VPC across available AZs
57+
data "aws_subnets" "vpc_subnets" {
58+
filter {
59+
name = "${local.name_prefix}-subnets"
60+
values = data.aws_availability_zones.available.names
61+
}
62+
63+
filter {
64+
name = "${local.name_prefix}-vpc-id"
65+
values = [var.vpc_id]
66+
}
67+
}
68+
69+
# Create the EC2 instance
70+
resource "aws_instance" "enos_test_server" {
71+
ami = data.aws_ami.amazon_linux.id
72+
key_name = var.ssh_keypair
73+
instance_initiated_shutdown_behavior = "terminate"
74+
instance_type = local.instance_type
75+
subnet_id = element(data.aws_subnets.vpc_subnets.ids, 0)
76+
vpc_security_group_ids = var.vpc_security_group_ids
77+
78+
# Install Docker using user_data script
79+
user_data = <<-EOF
80+
#!/bin/bash
81+
yum update -y
82+
amazon-linux-extras install docker -y
83+
systemctl start docker
84+
systemctl enable docker
85+
usermod -aG docker ec2-user
86+
EOF
87+
88+
tags = {
89+
Name = "${local.name_prefix}-ec2"
90+
}
91+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
output "instance_id" {
5+
description = "ID of the EC2 instance"
6+
value = aws_instance.enos_test_server.id
7+
}
8+
9+
output "instance_public_ip" {
10+
description = "Public IP address of the EC2 instance"
11+
value = aws_instance.enos_test_server.public_ip
12+
}
13+
14+
output "instance_private_ip" {
15+
description = "Private IP address of the EC2 instance"
16+
value = aws_instance.enos_test_server.private_ip
17+
}
18+
19+
output "instance_ami_id" {
20+
description = "AMI ID used for the EC2 instance"
21+
value = data.aws_ami.amazon_linux.id
22+
}
23+
24+
output "instance_type" {
25+
description = "Instance type used for the EC2 instance"
26+
value = local.instance_type
27+
}
28+
29+
output "aws_region" {
30+
description = "AWS region where resources are deployed"
31+
value = data.aws_region.current.name
32+
}
33+
34+
output "subnet_id" {
35+
description = "Subnet ID where the EC2 instance is launched"
36+
value = aws_instance.enos_test_server.subnet_id
37+
}
38+
39+
output "availability_zone" {
40+
description = "Availability Zone of the EC2 instance"
41+
value = aws_instance.enos_test_server.availability_zone
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
variable "instance_types" {
5+
description = "The instance types to use depending on architecture"
6+
type = object({
7+
amd64 = string
8+
arm64 = string
9+
})
10+
default = {
11+
amd64 = "t3a.medium"
12+
arm64 = "t4g.medium"
13+
}
14+
}
15+
16+
variable "ssh_keypair" {
17+
description = "SSH keypair used to connect to EC2 instances"
18+
type = string
19+
}
20+
21+
variable "vpc_id" {
22+
description = "The identifier of the VPC where the target instances will be created"
23+
type = string
24+
}
25+
26+
variable "vpc_security_group_ids" {
27+
description = "The identifier of the VPC Security Group IDs where the server instance will be created"
28+
type = string
29+
}

enos/modules/create_vpc/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ output "cluster_id" {
2020
description = "A unique string associated with the VPC"
2121
value = random_string.cluster_id.result
2222
}
23+
24+
output "security_group_id" {
25+
description = "The VPC security group"
26+
value = aws_security_group.default.id
27+
}

0 commit comments

Comments
 (0)