-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module in Shift to init a Frontend Infrastructure
- Loading branch information
1 parent
472c520
commit d326ee2
Showing
10 changed files
with
317 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package infrastructure | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/leapfrogtechnology/shift/infrastructure/templates/providers/aws/frontend-architecture" | ||
"github.com/leapfrogtechnology/shift/infrastructure/utils" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func InitializeFrontend() { | ||
credentialsJsonFile, err := os.Open("config.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer credentialsJsonFile.Close() | ||
byteValue, _ := ioutil.ReadAll(credentialsJsonFile) | ||
var frontendArgs utils.FrontendInfrastructureVariables | ||
err = json.Unmarshal(byteValue, &frontendArgs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
workspaceDir := filepath.Join("/tmp", frontendArgs.CLIENT_NAME) | ||
utils.GenerateFrontendTemplateFile(frontend_architecture.InfrastructureTemplate, frontendArgs, workspaceDir) | ||
utils.RunInfrastrucutreChanges(workspaceDir) | ||
} |
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
infrastructure/templates/providers/aws/backend-ha-architecture/backend-architecture.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package backend_ha_architecture | ||
|
||
const InfrastructureTemplate = ` | ||
// Terraform State Backend Initialization | ||
terraform { | ||
backend "remote" { | ||
organization = "lftechnology" | ||
token = "{{ info.TERRAFORM_TOKEN }}" | ||
workspaces { | ||
name = "{{ info.CLIENT_NAME }}-backend" | ||
} | ||
} | ||
} | ||
// Provider Initialization | ||
provider "aws" { | ||
region = "{{ info.AWS_REGION }}" | ||
access_key = "{{ info.AWS_ACCESS_KEY }}" | ||
secret_key = "{{ info.AWS_SECRET_KEY }}" | ||
} | ||
# Variables | ||
variable "az_count" { | ||
type = "string" | ||
default = "2" | ||
} | ||
variable "tags" { | ||
type = "map" | ||
default = { | ||
Name = "{{ info.RESOURCE_NAME }}" | ||
Project = "{{ info.PROJECT_NAME }}" | ||
} | ||
} | ||
# Fetch AZ in current Region | ||
data "aws_availability_zones" "available" {} | ||
resource "aws_vpc" "main" { | ||
cidr_block = "{{ info.CIDR_BLOCK }}" | ||
tags = var.tags | ||
} | ||
# Create a Private Subnet | ||
resource "aws_subnet" "private" { | ||
count = var.az_count | ||
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index) | ||
vpc_id = aws_vpc.main.id | ||
availability_zone = data.aws_availability_zones.available.names[count.index] | ||
} | ||
# Create a Public Subnet | ||
resource "aws_subnet" "public" { | ||
count = var.az_count | ||
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, var.az_count + count.index) | ||
vpc_id = aws_vpc.main.id | ||
availability_zone = data.aws_availability_zones.available.names[count.index] | ||
map_public_ip_on_launch = true | ||
} | ||
# Internet Gateway for Public Subnet | ||
resource "aws_internet_gateway" "gw" { | ||
vpc_id = aws_vpc.main.id | ||
} | ||
# Route the Public Subnet through IGW | ||
resource "aws_route" "internet_access" { | ||
route_table_id = aws_vpc.main.main_route_table_id | ||
destination_cidr_block = "0.0.0.0/0" | ||
gateway_id = aws_internet_gateway.gw.id | ||
} | ||
# Create a NAT gateway with an EIP for each private subnet to get internet connectivity | ||
resource "aws_eip" "gw" { | ||
count = var.az_count | ||
vpc = true | ||
depends_on = [ | ||
"aws_internet_gateway.gw" | ||
] | ||
} | ||
resource "aws_nat_gateway" "gw" { | ||
count = var.az_count | ||
subnet_id = element(aws_subnet.public.*.id, count.index) | ||
allocation_id = element(aws_eip.gw.*.id, count.index) | ||
} | ||
# Create a new route table for the private subnets, make it route non-local traffic through the NAT gateway to the internet | ||
resource "aws_route_table" "private" { | ||
count = var.az_count | ||
vpc_id = aws_vpc.main.id | ||
route { | ||
cidr_block = "0.0.0.0/0" | ||
nat_gateway_id = element(aws_nat_gateway.gw.*.id, count.index) | ||
} | ||
} | ||
# Explicitly associate the newly created route tables to the private subnets (so they don't default to the main route table) | ||
resource "aws_route_table_association" "private" { | ||
count = var.az_count | ||
subnet_id = element(aws_subnet.private.*.id, count.index) | ||
route_table_id = element(aws_route_table.private.*.id, count.index) | ||
} | ||
# Outputs | ||
output "vpc_id" { | ||
value = aws_vpc.main.id | ||
} | ||
output "vpc_cidr_block" { | ||
value = aws_vpc.main.cidr_block | ||
} | ||
output "private_subnets" { | ||
value = aws_subnet.private.*.id | ||
} | ||
output "public_subnets" { | ||
value = aws_subnet.public.*.id | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/flosch/pongo2" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type FrontendInfrastructureVariables struct { | ||
CLIENT_NAME string `json:"client_name"` | ||
AWS_REGION string `json:"aws_region"` | ||
AWS_ACCESS_KEY string `json:"aws_access_key"` | ||
AWS_SECRET_KEY string `json:"aws_secret_key"` | ||
AWS_S3_BUCKET_NAME string `json:"aws_s3_bucket_name"` | ||
TERRAFORM_TOKEN string `json:"terraform_token"` | ||
} | ||
|
||
func GenerateFrontendTemplateFile(template string, s3Args FrontendInfrastructureVariables, terraformPath string) { | ||
tpl, err := pongo2.FromString(template) | ||
if err != nil { | ||
panic(err) | ||
} | ||
out, err := tpl.Execute(pongo2.Context{"info": s3Args}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
terraformFileName := terraformPath + "/infrastructure.tf" | ||
err = os.MkdirAll(terraformPath, 0700) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = ioutil.WriteFile(terraformFileName, []byte(out), 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
Oops, something went wrong.