Skip to content

Commit 6bfc19a

Browse files
Optional vpc (#5)
* Make vpc optional
1 parent 988509e commit 6bfc19a

File tree

9 files changed

+43
-24
lines changed

9 files changed

+43
-24
lines changed

action.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ inputs:
4646
# create_hosted_zone:
4747
# description: "Bool, whether the hosted zone should be created in the action. Default is false"
4848
# default: false
49+
create_vpc:
50+
description: "Bool, whether a VPC should be created in the action. Default is false"
51+
default: false
52+
4953

5054
# ST2 config
5155
st2_auth_username:
@@ -87,6 +91,7 @@ runs:
8791
# CREATE_DOMAIN: ${{ inputs.CREATE_DOMAIN }}
8892
SUB_DOMAIN: ${{ inputs.sub_domain }}
8993
# CREATE_HOSTED_ZONE: ${{ inputs.CREATE_HOSTED_ZONE }}
94+
CREATE_VPC: ${{ inputs.create_vpc }}
9095
BITOPS_FAST_FAIL: true
9196

9297
# ST2 config

operations/_scripts/deploy/deploy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ docker run --rm --name bitops \
5555
-e TERRAFORM_DESTROY="${TERRAFORM_DESTROY}" \
5656
-e TF_STATE_BUCKET="${TF_STATE_BUCKET}" \
5757
-e DEFAULT_FOLDER_NAME="_default" \
58+
-e CREATE_VPC="${CREATE_VPC}" \
5859
-e BITOPS_FAST_FAIL="${BITOPS_FAST_FAIL}" \
5960
-e ST2_AUTH_USERNAME="${ST2_AUTH_USERNAME}" \
6061
-e ST2_AUTH_PASSWORD="${ST2_AUTH_PASSWORD}" \

operations/_scripts/generate/generate_tf_vars.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ aws_resource_identifier_supershort = \"${GITHUB_IDENTIFIER_SS}\"
7575
sub_domain_name = \"${SUB_DOMAIN}\"
7676
domain_name = \"${DOMAIN_NAME}\"
7777
78+
# VPC
79+
create_vpc = \"${CREATE_VPC}\"
80+
7881
# Region
7982
region = \"${AWS_DEFAULT_REGION}\"
8083

operations/deployment/terraform/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module "Stackstorm-Single-VM" {
1818
aws_resource_identifier_supershort=var.aws_resource_identifier_supershort
1919
sub_domain_name=var.sub_domain_name
2020
domain_name=var.domain_name
21+
create_vpc=var.create_vpc
2122
#create_domain=var.create_domain
2223
availability_zones=local.availability_zones
2324
route53_zone_id=var.route53_zone_id

operations/deployment/terraform/modules/02_networking.tf

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# TODO: optional based on flag: create_vpc
2-
resource "aws_vpc" "main" {
1+
resource "aws_vpc" "main" {
2+
count = var.create_vpc == "true" ? 1 : 0
33
cidr_block = var.vpc_cidr
44
tags = {
55
Name = "${var.aws_resource_identifier}"
66
}
77
}
8-
# TODO: optional based on flag: create_vpc
98
resource "aws_internet_gateway" "gw" {
10-
vpc_id = aws_vpc.main.id
9+
count = var.create_vpc == "true" ? 1 : 0
10+
vpc_id = aws_vpc.main[0].id
1111
}
1212

13-
# TODO: optional based on flag: create_vpc
1413
# resource "aws_subnet" "private" {
15-
# vpc_id = aws_vpc.main.id
16-
# count = length(var.private_subnets)
14+
# vpc_id = aws_vpc.main[0].id
15+
# count = var.create_vpc == "true" ? length(var.private_subnets) : 0
1716
# cidr_block = element(var.private_subnets, count.index)
1817
# availability_zone = element(var.availability_zones, count.index)
1918

@@ -23,12 +22,11 @@ resource "aws_internet_gateway" "gw" {
2322
# }
2423
# }
2524

26-
# TODO: optional based on flag: create_vpc
2725
resource "aws_subnet" "public" {
28-
vpc_id = aws_vpc.main.id
26+
count = var.create_vpc == "true" ? length(var.public_subnets) : 0
27+
vpc_id = aws_vpc.main[0].id
2928
cidr_block = element(var.public_subnets, count.index)
3029
availability_zone = element(var.availability_zones, count.index)
31-
count = length(var.public_subnets)
3230
map_public_ip_on_launch = true
3331

3432
tags = {
@@ -37,26 +35,27 @@ resource "aws_subnet" "public" {
3735
}
3836
}
3937

40-
# TODO: optional based on flag: create_vpc
4138
resource "aws_route_table" "public" {
42-
vpc_id = aws_vpc.main.id
39+
count = var.create_vpc == "true" ? 1 : 0
40+
vpc_id = aws_vpc.main[0].id
4341

4442
tags = {
4543
Name = "${var.aws_resource_identifier}"
4644
}
4745

4846
}
49-
# TODO: optional based on flag: create_vpc
47+
5048
resource "aws_route" "public" {
51-
route_table_id = aws_route_table.public.id
49+
count = var.create_vpc == "true" ? 1 : 0
50+
route_table_id = aws_route_table.public[0].id
5251
destination_cidr_block = "0.0.0.0/0"
53-
gateway_id = aws_internet_gateway.gw.id
52+
gateway_id = aws_internet_gateway.gw[0].id
5453
}
55-
# TODO: optional based on flag: create_vpc
54+
5655
resource "aws_route_table_association" "public" {
57-
count = length(var.public_subnets)
56+
count = var.create_vpc == "true" ? length(var.public_subnets) : 0
5857
subnet_id = element(aws_subnet.public.*.id, count.index)
59-
route_table_id = aws_route_table.public.id
58+
route_table_id = aws_route_table.public[0].id
6059
}
6160

6261

@@ -66,7 +65,7 @@ resource "aws_route_table_association" "public" {
6665
resource "aws_security_group" "allow_http" {
6766
name = "allow_http"
6867
description = "Allow HTTP traffic"
69-
vpc_id = aws_vpc.main.id
68+
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
7069
ingress {
7170
description = "HTTP"
7271
from_port = 80
@@ -85,7 +84,7 @@ resource "aws_security_group" "allow_http" {
8584
resource "aws_security_group" "allow_https" {
8685
name = "allow_https"
8786
description = "Allow HTTPS traffic"
88-
vpc_id = aws_vpc.main.id
87+
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
8988
ingress {
9089
description = "HTTPS"
9190
from_port = 443
@@ -104,7 +103,7 @@ resource "aws_security_group" "allow_https" {
104103
resource "aws_security_group" "allow_ssh" {
105104
name = "allow_ssh"
106105
description = "Allow SSH traffic"
107-
vpc_id = aws_vpc.main.id
106+
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
108107
ingress {
109108
description = "SSH"
110109
from_port = 22

operations/deployment/terraform/modules/03_ec2.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ resource "aws_instance" "server" {
44
key_name = aws_key_pair.deployer.key_name
55
associate_public_ip_address = true
66

7-
# TODO: handle no subnet_id (see 02_networking.tf) - flag create_vpc
8-
subnet_id = aws_subnet.public.*.id[0]
7+
subnet_id = var.create_vpc == "true" ? aws_subnet.public.*.id[0] : null
98
vpc_security_group_ids = [aws_security_group.allow_http.id, aws_security_group.allow_https.id, aws_security_group.allow_ssh.id]
109
user_data = <<EOF
1110
#!/bin/bash

operations/deployment/terraform/modules/04_elb.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
resource "aws_elb" "vm" {
22
name = "${var.aws_resource_identifier_supershort}"
3-
subnets = aws_subnet.public.*.id
3+
subnets = var.create_vpc == "true" ? aws_subnet.public.*.id : null
4+
availability_zones = var.create_vpc == "true" ? null : [aws_instance.server.availability_zone]
45

56
security_groups = [aws_security_group.allow_http.id, aws_security_group.allow_https.id]
67

operations/deployment/terraform/modules/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ variable "domain_name" {
8888
description = "root domain name without any subdomains"
8989
default = ""
9090
}
91+
variable "create_vpc" {
92+
type = string
93+
default = "false"
94+
description = "Attach public IP to the EC2 instance"
95+
}
9196

9297

9398

operations/deployment/terraform/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ variable "domain_name" {
8888
description = "root domain name without any subdomains"
8989
default = ""
9090
}
91+
variable "create_vpc" {
92+
type = string
93+
default = "false"
94+
description = "Attach public IP to the EC2 instance"
95+
}
9196

9297

9398
## NEW NEEDS REVIEW

0 commit comments

Comments
 (0)