Skip to content

Commit

Permalink
Add module in Shift to init a Frontend Infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshadotel committed Aug 28, 2019
1 parent 472c520 commit d326ee2
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 135 deletions.
23 changes: 16 additions & 7 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/leapfrogtechnology/shift/infrastructure"
"os"

"github.com/urfave/cli"
Expand All @@ -22,15 +23,23 @@ func Initialize(info *Info) error {
app.Usage = info.Description

app.Commands = []cli.Command{
cli.Command{
Name: "init",
Action: func(ctx *cli.Context) error {
print("Shift Shift Shift!")

return nil
{
Name: "init",
Description: "Initialize",
Aliases: nil,
Usage: "Initialize your Application",
Subcommands: []cli.Command{
{
Name: "frontend",
Aliases: nil,
Usage: "Initialize your frontend infrastructure",
Description: "Use this to initialize your frontend Infrastructure",
Action: func(c *cli.Context) {
infrastructure.InitializeFrontend()
},
},
},
},
}

return app.Run(os.Args)
}
6 changes: 3 additions & 3 deletions cli/shift.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main
package cli

import (
"fmt"

"github.com/lftechnology/shift/cli/cmd"
"github.com/leapfrogtechnology/shift/cli/cmd"
)

func main() {
func Initialize() {
info := &cmd.Info{
Name: "Shift",
Version: "0.0.1",
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions infrastructure/infrastructure.go
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)
}
122 changes: 0 additions & 122 deletions infrastructure/shift.go

This file was deleted.

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
}
`
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Backend Initialization
package frontend_architecture

const InfrastructureTemplate = `
// Terraform State Backend Initialization
terraform {
backend "remote" {
organization = "lftechnology"
token = "{{ info.TERRAFORM_TOKEN }}"
workspaces {
name = "{{ info.CLIENT_NAME }}"
name = "{{ info.CLIENT_NAME }}-frontend"
}
}
}
Expand Down Expand Up @@ -69,7 +72,7 @@ resource "aws_cloudfront_distribution" "www_distribution" {
compress = true
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
// This needs to match the `origin_id` above.
// This needs to match the origin above.
target_origin_id = "{{ info.AWS_S3_BUCKET_NAME }}"
min_ttl = 0
default_ttl = 86400
Expand Down Expand Up @@ -116,3 +119,4 @@ output "bucket_name" {
output "frontend_web_url" {
value = aws_cloudfront_distribution.www_distribution.domain_name
}
`
36 changes: 36 additions & 0 deletions infrastructure/utils/template.go
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)
}
}
Loading

0 comments on commit d326ee2

Please sign in to comment.