1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-08-08 22:36:41 +02:00

Load AWS region from meta data if not set for deploy

This commit is contained in:
Lee Brown
2019-07-14 00:41:58 -08:00
parent 1952c9d731
commit db07498192
7 changed files with 15 additions and 22 deletions

View File

@@ -73,7 +73,6 @@ func main() {
log := log.New(os.Stdout, service+" : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile) log := log.New(os.Stdout, service+" : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// ========================================================================= // =========================================================================
// Configuration // Configuration
var cfg struct { var cfg struct {
@@ -162,7 +161,6 @@ func main() {
return // We displayed help. return // We displayed help.
} }
// ========================================================================= // =========================================================================
// Config Validation & Defaults // Config Validation & Defaults
@@ -205,7 +203,6 @@ func main() {
cfg.Service.EnableHTTPS = true cfg.Service.EnableHTTPS = true
} }
// Determine the primary host by parsing host from the base app URL. // Determine the primary host by parsing host from the base app URL.
baseSiteUrl, err := url.Parse(cfg.Service.BaseUrl) baseSiteUrl, err := url.Parse(cfg.Service.BaseUrl)
if err != nil { if err != nil {
@@ -223,7 +220,6 @@ func main() {
primaryServiceHost = baseSiteUrl.Host primaryServiceHost = baseSiteUrl.Host
} }
// ========================================================================= // =========================================================================
// Log Service Info // Log Service Info
@@ -243,7 +239,6 @@ func main() {
log.Printf("main : Config : %v\n", string(cfgJSON)) log.Printf("main : Config : %v\n", string(cfgJSON))
} }
// ========================================================================= // =========================================================================
// Init AWS Session // Init AWS Session
var awsSession *session.Session var awsSession *session.Session
@@ -267,7 +262,6 @@ func main() {
awsSession = awstrace.WrapSession(awsSession) awsSession = awstrace.WrapSession(awsSession)
} }
// ========================================================================= // =========================================================================
// Start Redis // Start Redis
// Ensure the eviction policy on the redis cluster is set correctly. // Ensure the eviction policy on the redis cluster is set correctly.
@@ -301,7 +295,6 @@ func main() {
} }
} }
// ========================================================================= // =========================================================================
// Start Database // Start Database
var dbUrl url.URL var dbUrl url.URL
@@ -352,7 +345,6 @@ func main() {
log.Fatalf("main : Constructing authenticator : %+v", err) log.Fatalf("main : Constructing authenticator : %+v", err)
} }
// ========================================================================= // =========================================================================
// Load middlewares that need to be configured specific for the service. // Load middlewares that need to be configured specific for the service.
@@ -376,7 +368,6 @@ func main() {
serviceMiddlewares = append(serviceMiddlewares, redirect) serviceMiddlewares = append(serviceMiddlewares, redirect)
} }
// ========================================================================= // =========================================================================
// Start Tracing Support // Start Tracing Support
th := fmt.Sprintf("%s:%d", cfg.Trace.Host, cfg.Trace.Port) th := fmt.Sprintf("%s:%d", cfg.Trace.Host, cfg.Trace.Port)
@@ -398,7 +389,6 @@ func main() {
}() }()
} }
// ========================================================================= // =========================================================================
// ECS Task registration for services that don't use an AWS Elastic Load Balancer. // ECS Task registration for services that don't use an AWS Elastic Load Balancer.
err = devops.EcsServiceTaskInit(log, awsSession) err = devops.EcsServiceTaskInit(log, awsSession)
@@ -503,7 +493,6 @@ func main() {
}() }()
} }
// ========================================================================= // =========================================================================
// Shutdown // Shutdown

View File

@@ -118,7 +118,6 @@ func RespondErrorStatus(ctx context.Context, w http.ResponseWriter, er error, st
return nil return nil
} }
// RespondText sends text back to the client as plain text with the specified HTTP status code. // RespondText sends text back to the client as plain text with the specified HTTP status code.
func RespondText(ctx context.Context, w http.ResponseWriter, text string, statusCode int) error { func RespondText(ctx context.Context, w http.ResponseWriter, text string, statusCode int) error {
if err := Respond(ctx, w, []byte(text), statusCode, MIMETextPlainCharsetUTF8); err != nil { if err := Respond(ctx, w, []byte(text), statusCode, MIMETextPlainCharsetUTF8); err != nil {

View File

@@ -3,7 +3,6 @@ package deploy
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws/session"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -11,6 +10,8 @@ import (
"strings" "strings"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr" "github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecs" "github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/secretsmanager" "github.com/aws/aws-sdk-go/service/secretsmanager"
@@ -33,11 +34,17 @@ func GetAwsCredentials(targetEnv string) (awsCredentials, error) {
sess, err := session.NewSession() sess, err := session.NewSession()
if err != nil { if err != nil {
return creds, errors.Wrap(err, "failed to load aws credentials from instance") return creds, errors.Wrap(err, "Failed to load AWS credentials from instance")
} }
if sess.Config != nil && sess.Config.Region != nil { if sess.Config != nil && sess.Config.Region != nil {
creds.Region = *sess.Config.Region creds.Region = *sess.Config.Region
} else {
sm := ec2metadata.New(sess)
creds.Region, err = sm.Region()
if err != nil {
return creds, errors.Wrap(err, "Failed to get region from AWS session")
}
} }
return creds, nil return creds, nil

View File

@@ -1294,7 +1294,6 @@ func ServiceDeploy(log *log.Logger, req *serviceDeployRequest) error {
log.Printf("\t%s\tUsing Security Group '%s'.\n", tests.Success, req.Ec2SecurityGroupName) log.Printf("\t%s\tUsing Security Group '%s'.\n", tests.Success, req.Ec2SecurityGroupName)
} }
// This is only used when service uses Aurora via RDS for serverless Postgres and database cluster is defined. // This is only used when service uses Aurora via RDS for serverless Postgres and database cluster is defined.
// Aurora Postgres is limited to specific AWS regions and thus not used by default. // Aurora Postgres is limited to specific AWS regions and thus not used by default.
// If an Aurora Postgres cluster is defined, ensure it exists with RDS else create a new one. // If an Aurora Postgres cluster is defined, ensure it exists with RDS else create a new one.

View File

@@ -25,7 +25,6 @@ func main() {
log := log.New(os.Stdout, service+" : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile) log := log.New(os.Stdout, service+" : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// ========================================================================= // =========================================================================
// Log App Info // Log App Info