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

@ -110,7 +110,7 @@ cache:
S3_BUCKET_PRIVATE: 'saas-starter-kit-private'
S3_BUCKET_PUBLIC: 'saas-starter-kit-public'
NO_DEPLOY: 1
AWS_USE_ROLE: 1
AWS_USE_ROLE: 1
webapi:build:dev:
<<: *deploy_dev_webapi_tmpl
stage: build:dev

View File

@ -73,7 +73,6 @@ func main() {
log := log.New(os.Stdout, service+" : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// =========================================================================
// Configuration
var cfg struct {
@ -94,7 +93,7 @@ func main() {
Project string `default:"" envconfig:"PROJECT"`
BaseUrl string `default:"" envconfig:"BASE_URL" example:"http://api.eproc.tech"`
HostNames []string `envconfig:"HOST_NAMES" example:"alternative-subdomain.eproc.tech"`
EnableHTTPS bool `default:"false" envconfig:"ENABLE_HTTPS"`
EnableHTTPS bool `default:"false" envconfig:"ENABLE_HTTPS"`
TemplateDir string `default:"./templates" envconfig:"TEMPLATE_DIR"`
DebugHost string `default:"0.0.0.0:4000" envconfig:"DEBUG_HOST"`
ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"`
@ -162,7 +161,6 @@ func main() {
return // We displayed help.
}
// =========================================================================
// Config Validation & Defaults
@ -205,7 +203,6 @@ func main() {
cfg.Service.EnableHTTPS = true
}
// Determine the primary host by parsing host from the base app URL.
baseSiteUrl, err := url.Parse(cfg.Service.BaseUrl)
if err != nil {
@ -223,7 +220,6 @@ func main() {
primaryServiceHost = baseSiteUrl.Host
}
// =========================================================================
// Log Service Info
@ -243,7 +239,6 @@ func main() {
log.Printf("main : Config : %v\n", string(cfgJSON))
}
// =========================================================================
// Init AWS Session
var awsSession *session.Session
@ -267,7 +262,6 @@ func main() {
awsSession = awstrace.WrapSession(awsSession)
}
// =========================================================================
// Start Redis
// Ensure the eviction policy on the redis cluster is set correctly.
@ -301,7 +295,6 @@ func main() {
}
}
// =========================================================================
// Start Database
var dbUrl url.URL
@ -352,7 +345,6 @@ func main() {
log.Fatalf("main : Constructing authenticator : %+v", err)
}
// =========================================================================
// Load middlewares that need to be configured specific for the service.
@ -376,7 +368,6 @@ func main() {
serviceMiddlewares = append(serviceMiddlewares, redirect)
}
// =========================================================================
// Start Tracing Support
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.
err = devops.EcsServiceTaskInit(log, awsSession)
@ -503,7 +493,6 @@ func main() {
}()
}
// =========================================================================
// Shutdown

View File

@ -118,7 +118,6 @@ func RespondErrorStatus(ctx context.Context, w http.ResponseWriter, er error, st
return nil
}
// 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 {
if err := Respond(ctx, w, []byte(text), statusCode, MIMETextPlainCharsetUTF8); err != nil {

View File

@ -3,7 +3,6 @@ package deploy
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"io/ioutil"
"path/filepath"
"sort"
@ -11,6 +10,8 @@ import (
"strings"
"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/ecs"
"github.com/aws/aws-sdk-go/service/secretsmanager"
@ -33,11 +34,17 @@ func GetAwsCredentials(targetEnv string) (awsCredentials, error) {
sess, err := session.NewSession()
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
} 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

View File

@ -173,7 +173,7 @@ type awsCredentials struct {
AccessKeyID string `validate:"required_without=UseRole"`
SecretAccessKey string `validate:"required_without=UseRole"`
Region string `validate:"required_without=UseRole"`
UseRole bool
UseRole bool
}
// Session returns a new AWS Session used to access AWS services.
@ -183,7 +183,7 @@ func (creds awsCredentials) Session() *session.Session {
// Get an AWS session from an implicit source if no explicit
// configuration is provided. This is useful for taking advantage of
// EC2/ECS instance roles.
return session.Must(session.NewSession())
return session.Must(session.NewSession())
}
return session.New(

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)
}
// 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.
// If an Aurora Postgres cluster is defined, ensure it exists with RDS else create a new one.
@ -2460,7 +2459,7 @@ func ServiceDeploy(log *log.Logger, req *serviceDeployRequest) error {
placeholders["{HTTPS_ENABLED}"] = "true"
// When there is no Elastic Load Balancer, we need to terminate HTTPS on the app.
if !req.EnableEcsElb {
if !req.EnableEcsElb {
placeholders["{HTTPS_HOST}"] = "0.0.0.0:443"
}
}

View File

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