github.com/kolanos/fargate@v0.2.3/cmd/lb_alias.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/jpignata/fargate/console"
     5  	ELBV2 "github.com/jpignata/fargate/elbv2"
     6  	Route53 "github.com/jpignata/fargate/route53"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  type LbAliasOperation struct {
    11  	LoadBalancerName string
    12  	AliasDomain      string
    13  }
    14  
    15  var lbAliasCmd = &cobra.Command{
    16  	Use:   "alias <load-balancer-name> <domain-name>",
    17  	Args:  cobra.ExactArgs(2),
    18  	Short: "Create a load balancer alias record",
    19  	Long: `Create a load balancer alias record
    20  
    21  Create an alias record to the load balancer for domains that are hosted within
    22  Amazon Route 53 and within the same AWS account. If you're using another DNS
    23  provider or host your domains in a differnt account, you will need to manually
    24  create this record.  `,
    25  	Run: func(cmd *cobra.Command, args []string) {
    26  		operation := &LbAliasOperation{
    27  			LoadBalancerName: args[0],
    28  			AliasDomain:      args[1],
    29  		}
    30  
    31  		createAliasRecord(operation)
    32  	},
    33  }
    34  
    35  func init() {
    36  	lbCmd.AddCommand(lbAliasCmd)
    37  }
    38  
    39  func createAliasRecord(operation *LbAliasOperation) {
    40  	route53 := Route53.New(sess)
    41  	elbv2 := ELBV2.New(sess)
    42  
    43  	hostedZones := route53.ListHostedZones()
    44  	loadBalancer := elbv2.DescribeLoadBalancer(operation.LoadBalancerName)
    45  
    46  	for _, hostedZone := range hostedZones {
    47  		if hostedZone.IsSuperDomainOf(operation.AliasDomain) {
    48  			route53.CreateAlias(hostedZone, "A", operation.AliasDomain, loadBalancer.DNSName, loadBalancer.HostedZoneId)
    49  			console.Info("Created alias record (%s -> %s)", operation.AliasDomain, loadBalancer.DNSName)
    50  
    51  			return
    52  		}
    53  	}
    54  
    55  	console.Issue("Could not find hosted zone for %s", operation.AliasDomain)
    56  	console.Info("If you're hosting this domain elsewhere or in another AWS account, please manually create the Alias record:")
    57  	console.Info("%s -> %s", operation.AliasDomain, loadBalancer.DNSName)
    58  }