Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Centralize session creation
Browse files Browse the repository at this point in the history
Rather than creating a session in each service, centralize within the
root command pass session around as needed. This allows us to DRY up
region logic in anticipation of Fargate being available outside of
us-east-1 and reduces the amount of boilerplate per service.

* Don't alias packages unnecessarily
* Pass region to CreateTaskDefinition explicitly rather than plucking it
  from the session
  • Loading branch information
jpignata committed Dec 23, 2017
1 parent 7b7cdff commit 321d036
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 122 deletions.
9 changes: 1 addition & 8 deletions acm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ package acm
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/acm"
"github.com/jpignata/fargate/console"
)

type ACM struct {
svc *acm.ACM
}

func New() ACM {
sess, err := session.NewSession()

if err != nil {
console.ErrorExit(err, "Could not create ACM session")
}

func New(sess *session.Session) ACM {
return ACM{
svc: acm.New(sess),
}
Expand Down
9 changes: 1 addition & 8 deletions cloudwatchlogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ package cloudwatchlogs
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/jpignata/fargate/console"
)

type CloudWatchLogs struct {
svc *cloudwatchlogs.CloudWatchLogs
}

func New() CloudWatchLogs {
sess, err := session.NewSession()

if err != nil {
console.ErrorExit(err, "Error creating CloudWatch Logs session")
}

func New(sess *session.Session) CloudWatchLogs {
return CloudWatchLogs{
svc: cloudwatchlogs.New(sess),
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/certificate_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func init() {
func destroyCertificate(domainName string) {
console.Info("[%s] Destroying certificate", domainName)

acm := ACM.New()
acm := ACM.New(sess)
acm.DeleteCertificate(domainName)
}
2 changes: 1 addition & 1 deletion cmd/certificate_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func validateCertificateAndKeyFiles() {
func importCertificate() {
console.Info("Importing certificate")

acm := ACM.New()
acm := ACM.New(sess)

certificateData, err := ioutil.ReadFile(certificateFile)

Expand Down
2 changes: 1 addition & 1 deletion cmd/certificate_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
}

func infoCertificate(domainName string) {
acm := ACM.New()
acm := ACM.New(sess)
certificate := acm.DescribeCertificate(domainName)

console.KeyValue("Domain Name", "%s\n", certificate.DomainName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/certificate_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

func listCertificates() {
acm := ACM.New()
acm := ACM.New(sess)
certificates := acm.ListCertificates()

if len(certificates) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/certificate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
func createCertificate(domainName string) {
console.Info("Requesting certificate [%s]", domainName)

acm := ACM.New()
acm := ACM.New(sess)
acm.RequestCertificate(domainName, aliases)

console.Info("[%s] You must validate ownership of the domain name for the certificate to be issued", domainName)
Expand Down
4 changes: 2 additions & 2 deletions cmd/certificate_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func init() {
func validateCertificate(domainName string) {
console.Info("Validating certificate [%s]", domainName)

route53 := Route53.New()
acm := ACM.New()
route53 := Route53.New(sess)
acm := ACM.New(sess)

hostedZones := route53.ListHostedZones()
certificate := acm.DescribeCertificate(domainName)
Expand Down
6 changes: 3 additions & 3 deletions cmd/lb_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func normalizeFields() {
}

func getCertificateArns() {
acm := ACM.New()
acm := ACM.New(sess)

for _, certificateDomainName := range certificateDomainNames {
certificate := acm.DescribeCertificate(certificateDomainName)
Expand Down Expand Up @@ -115,8 +115,8 @@ func inferType() {
func createLb(lbName string) {
console.Info("Creating load balancer [%s]", lbName)

elbv2 := ELBV2.New()
ec2 := EC2.New()
elbv2 := ELBV2.New(sess)
ec2 := EC2.New(sess)

subnetIds := ec2.GetDefaultVpcSubnetIds()
vpcId := ec2.GetDefaultVpcId()
Expand Down
2 changes: 1 addition & 1 deletion cmd/lb_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
func destroyLoadBalancer(domainName string) {
console.Info("[%s] Destroying load balancer", domainName)

elbv2 := ELBV2.New()
elbv2 := ELBV2.New(sess)
elbv2.DeleteLoadBalancer(domainName)
elbv2.DeleteTargetGroup(domainName + "-" + "default")
}
2 changes: 1 addition & 1 deletion cmd/lb_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

func listLoadBalancers() {
elbv2 := ELBV2.New()
elbv2 := ELBV2.New(sess)
loadBalancers := elbv2.DescribeLoadBalancers([]string{})

if len(loadBalancers) > 0 {
Expand Down
36 changes: 34 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,66 @@
package cmd

import (
"os"
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/jpignata/fargate/console"
"github.com/spf13/cobra"
)

const version = "0.0.1"
const (
version = "0.0.1"
defaultRegion = "us-east-1"
)

type Port struct {
Port int64
Protocol string
}

var verbose bool
var (
region string
verbose bool
sess *session.Session
)

var rootCmd = &cobra.Command{
Use: "fargate",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
envAwsRegion := os.Getenv("AWS_REGION")
envAwsDefaultRegion := os.Getenv("AWS_DEFAULT_REGION")

if verbose {
verbose = true
console.Verbose = true
}

if region == "" {
if envAwsDefaultRegion != "" {
region = envAwsDefaultRegion
} else if envAwsRegion != "" {
region = envAwsDefaultRegion
} else {
region = defaultRegion
}
}

sess = session.Must(
session.NewSession(
&aws.Config{
Region: aws.String(region),
},
),
)
},
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
rootCmd.PersistentFlags().StringVarP(&region, "region", "r", "", "AWS Region (defaults to us-east-1)")
}

func Execute() {
Expand Down
15 changes: 8 additions & 7 deletions cmd/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func init() {
}

func validateLb() {
elbv2 := ELBV2.New()
elbv2 := ELBV2.New(sess)
loadBalancer := elbv2.DescribeLoadBalancer(lbName)

if loadBalancer.Type == "network" {
Expand Down Expand Up @@ -166,12 +166,12 @@ func extractEnvVars() {
func createService(serviceName string) {
console.Info("Creating %s", serviceName)

cwl := CWL.New()
ec2 := EC2.New()
ecr := ECR.New()
ecs := ECS.New()
elbv2 := ELBV2.New()
iam := IAM.New()
cwl := CWL.New(sess)
ec2 := EC2.New(sess)
ecr := ECR.New(sess)
ecs := ECS.New(sess)
elbv2 := ELBV2.New(sess)
iam := IAM.New(sess)

var (
targetGroupArn string
Expand Down Expand Up @@ -240,6 +240,7 @@ func createService(serviceName string) {
Name: serviceName,
Port: port.Port,
LogGroupName: logGroupName,
LogRegion: region,
},
)
ecs.CreateService(
Expand Down
4 changes: 2 additions & 2 deletions cmd/service_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func init() {
func deployService(serviceName string) {
console.Info("Deploying %s", serviceName)

ecs := ECS.New()
ecs := ECS.New(sess)
service := ecs.DescribeService(serviceName)

if image == "" {
var tag string

ecr := ECR.New()
ecr := ECR.New(sess)
repositoryUri := ecr.GetRepositoryUri(serviceName)
repository := docker.Repository{
Uri: repositoryUri,
Expand Down
2 changes: 1 addition & 1 deletion cmd/service_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func init() {
func destroyService(serviceName string) {
console.Info("[%s] Destroying service", serviceName)

ecs := ECS.New()
ecs := ECS.New(sess)
ecs.DestroyService(serviceName)
}
4 changes: 2 additions & 2 deletions cmd/service_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func init() {
func infoService(serviceName string) {
var eniIds []string

ecs := ECS.New()
ec2 := EC2.New()
ecs := ECS.New(sess)
ec2 := EC2.New(sess)
service := ecs.DescribeService(serviceName)
tasks := ecs.DescribeTasksForService(serviceName)

Expand Down
2 changes: 1 addition & 1 deletion cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
}

func listServices() {
ecs := ECS.New()
ecs := ECS.New(sess)
services := ecs.ListServices()

if len(services) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/service_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func init() {
func psService(serviceName string) {
var eniIds []string

ecs := ECS.New()
ec2 := EC2.New()
ecs := ECS.New(sess)
ec2 := EC2.New(sess)
tasks := ecs.DescribeTasksForService(serviceName)

for _, task := range tasks {
Expand Down
4 changes: 2 additions & 2 deletions cmd/service_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func validateScale(scale string) {
func setDesiredCount(serviceName, scale string) {
if scale[0] == '+' || scale[0] == '-' {
if s, err := strconv.ParseInt(scale[1:len(scale)], 10, 64); err == nil {
ecs := ECS.New()
ecs := ECS.New(sess)
desiredCount = ecs.GetDesiredCount(serviceName)

if scale[0] == '+' {
Expand Down Expand Up @@ -74,6 +74,6 @@ func setDesiredCount(serviceName, scale string) {
func scaleService(serviceName string) {
console.Info("Scaling service %s to %d", serviceName, desiredCount)

ecs := ECS.New()
ecs := ECS.New(sess)
ecs.SetDesiredCount(serviceName, desiredCount)
}
12 changes: 1 addition & 11 deletions ec2/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package ec2

import (
"os"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/fatih/color"
)

type EC2 struct {
svc *ec2.EC2
}

func New() EC2 {
sess, err := session.NewSession()

if err != nil {
color.Red("Error creating EC2 session: ", err)
os.Exit(1)
}

func New(sess *session.Session) EC2 {
return EC2{
svc: ec2.New(sess),
}
Expand Down
18 changes: 4 additions & 14 deletions ecr/main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
package ecr

import (
"os"

"github.com/aws/aws-sdk-go/aws/session"
awsecr "github.com/aws/aws-sdk-go/service/ecr"
"github.com/fatih/color"
"github.com/aws/aws-sdk-go/service/ecr"
)

type ECR struct {
svc *awsecr.ECR
svc *ecr.ECR
}

func New() ECR {
sess, err := session.NewSession()

if err != nil {
color.Red("Error creating ECR session: ", err)
os.Exit(1)
}

func New(sess *session.Session) ECR {
return ECR{
svc: awsecr.New(sess),
svc: ecr.New(sess),
}
}
Loading

0 comments on commit 321d036

Please sign in to comment.