You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-06-17 00:17:59 +02:00
deveops load aws creds from session for deploy
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,7 +1,4 @@
|
|||||||
.idea
|
.idea
|
||||||
go.mod
|
|
||||||
aws.lee
|
aws.lee
|
||||||
aws.*
|
aws.*
|
||||||
.env_docker_compose
|
.env_docker_compose
|
||||||
|
|
||||||
!aws.go
|
|
@ -3,9 +3,11 @@ 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"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
@ -26,6 +28,21 @@ const (
|
|||||||
func GetAwsCredentials(targetEnv string) (awsCredentials, error) {
|
func GetAwsCredentials(targetEnv string) (awsCredentials, error) {
|
||||||
var creds awsCredentials
|
var creds awsCredentials
|
||||||
|
|
||||||
|
if v := getTargetEnv(targetEnv, "AWS_USE_ROLE"); v != "" {
|
||||||
|
creds.UseRole, _ = strconv.ParseBool(v)
|
||||||
|
|
||||||
|
sess, err := session.NewSession()
|
||||||
|
if err != nil {
|
||||||
|
return creds, errors.Wrap(err, "failed to load aws credentials from instance")
|
||||||
|
}
|
||||||
|
|
||||||
|
if sess.Config != nil && sess.Config.Region != nil {
|
||||||
|
creds.Region = *sess.Config.Region
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds, nil
|
||||||
|
}
|
||||||
|
|
||||||
creds.AccessKeyID = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_ACCESS_KEY_ID"))
|
creds.AccessKeyID = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_ACCESS_KEY_ID"))
|
||||||
creds.SecretAccessKey = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_SECRET_ACCESS_KEY"))
|
creds.SecretAccessKey = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_SECRET_ACCESS_KEY"))
|
||||||
creds.Region = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_REGION"))
|
creds.Region = strings.TrimSpace(getTargetEnv(targetEnv, "AWS_REGION"))
|
||||||
|
@ -170,13 +170,22 @@ func (r *serviceDeployRequest) awsSession() *session.Session {
|
|||||||
// AwsCredentials defines AWS credentials used for deployment. Unable to use roles when deploying
|
// AwsCredentials defines AWS credentials used for deployment. Unable to use roles when deploying
|
||||||
// using gitlab CI/CD pipeline.
|
// using gitlab CI/CD pipeline.
|
||||||
type awsCredentials struct {
|
type awsCredentials struct {
|
||||||
AccessKeyID string `validate:"required"`
|
AccessKeyID string `validate:"required_without=UseRole"`
|
||||||
SecretAccessKey string `validate:"required"`
|
SecretAccessKey string `validate:"required_without=UseRole"`
|
||||||
Region string `validate:"required"`
|
Region string `validate:"required_without=UseRole"`
|
||||||
|
UseRole bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Session returns a new AWS Session used to access AWS services.
|
// Session returns a new AWS Session used to access AWS services.
|
||||||
func (creds awsCredentials) Session() *session.Session {
|
func (creds awsCredentials) Session() *session.Session {
|
||||||
|
|
||||||
|
if creds.UseRole {
|
||||||
|
// 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.New(
|
return session.New(
|
||||||
&aws.Config{
|
&aws.Config{
|
||||||
Region: aws.String(creds.Region),
|
Region: aws.String(creds.Region),
|
||||||
|
@ -68,7 +68,12 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if awsCreds.UseRole {
|
||||||
|
log.Printf("\t\t\tUsing role")
|
||||||
|
} else {
|
||||||
log.Printf("\t\t\tAccessKeyID: '%s'", awsCreds.AccessKeyID)
|
log.Printf("\t\t\tAccessKeyID: '%s'", awsCreds.AccessKeyID)
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("\t\t\tRegion: '%s'", awsCreds.Region)
|
log.Printf("\t\t\tRegion: '%s'", awsCreds.Region)
|
||||||
log.Printf("\t%s\tAWS credentials valid.", tests.Success)
|
log.Printf("\t%s\tAWS credentials valid.", tests.Success)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user