github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/provider/ecs/ec2.go (about)

     1  package ecs
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/aws/session"
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/juju/loggo"
     8  
     9  	"fmt"
    10  )
    11  
    12  // logging
    13  var ec2Logger = loggo.GetLogger("ec2")
    14  
    15  // EC2 struct
    16  type EC2 struct {
    17  }
    18  
    19  /*
    20   * GetSecurityGroupID retrieves the id from the security group based on the name
    21   */
    22  func (e *EC2) GetSecurityGroupID(name string) (string, error) {
    23  	svc := ec2.New(session.New())
    24  
    25  	input := &ec2.DescribeSecurityGroupsInput{
    26  		Filters: []*ec2.Filter{
    27  			{
    28  				Name:   aws.String("group-name"),
    29  				Values: aws.StringSlice([]string{name}),
    30  			},
    31  		},
    32  	}
    33  
    34  	result, err := svc.DescribeSecurityGroups(input)
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	if len(result.SecurityGroups) == 0 {
    40  		return "", fmt.Errorf("No security groups returned")
    41  	}
    42  
    43  	return aws.StringValue(result.SecurityGroups[0].GroupId), nil
    44  }
    45  
    46  /*
    47   * GetSubnetId retrieves the id from the subnet based on the name
    48   */
    49  func (e *EC2) GetSubnetID(name string) (string, error) {
    50  	svc := ec2.New(session.New())
    51  
    52  	input := &ec2.DescribeSubnetsInput{
    53  		Filters: []*ec2.Filter{
    54  			{
    55  				Name:   aws.String("tag:Name"),
    56  				Values: aws.StringSlice([]string{name}),
    57  			},
    58  		},
    59  	}
    60  
    61  	result, err := svc.DescribeSubnets(input)
    62  	if err != nil {
    63  		return "", err
    64  	}
    65  
    66  	if len(result.Subnets) == 0 {
    67  		return "", fmt.Errorf("No subnets returned")
    68  	}
    69  
    70  	return aws.StringValue(result.Subnets[0].SubnetId), nil
    71  }