Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AWS] List AWS Security Group #1456

Open
dishantmehta03 opened this issue Oct 11, 2024 · 1 comment
Open

[AWS] List AWS Security Group #1456

dishantmehta03 opened this issue Oct 11, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@dishantmehta03
Copy link

Is there any functions in terratest to list the aws security group i do not see any existing functionality to do that, if there is one appreciate if more information can be provided for the same

@dishantmehta03 dishantmehta03 added the enhancement New feature or request label Oct 11, 2024
@dishantmehta03 dishantmehta03 changed the title List AWS Security Group [AWS] List AWS Security Group Oct 11, 2024
@LCO18371
Copy link

You can use AWS SDK functions in Go, combined with Terratest, to achieve this.
Import the necessary libraries, and create a function.
this function is getting data of security group info
package test

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/stretchr/testify/assert"

)

var regions = []string{"us-east-1", "us-west-2"} // Add all regions you want to query

func getSecurityGroupInfoAllRegions(t *testing.T) []map[string]string {
var securityGroupsInfo []map[string]string
resourceType := "Security Group"

for _, region := range regions {
	// Create AWS session for each region
	sess, err := session.NewSession(&aws.Config{
		Region: aws.String(region),
	})
	assert.NoError(t, err)

	// Create EC2 service client
	ec2Svc := ec2.New(sess)

	// Describe Security Groups
	resp, err := ec2Svc.DescribeSecurityGroups(nil)
	if err != nil {
		log.Printf("Error fetching security groups in region %s: %v", region, err)
		continue // Skip this region if there's an error
	}

	fmt.Printf("Region: %s | Number of Security Groups: %d\n", region, len(resp.SecurityGroups))
	for _, sg := range resp.SecurityGroups {
		sgID := *sg.GroupId
		groupName := *sg.GroupName
		vpcID := "NA"
		if sg.VpcId != nil {
			vpcID = *sg.VpcId
		}
		creationTime := "NA" // No direct creation time for security groups in AWS SDK

		// Get Security Group tags
		tags := getSecurityGroupTags(t, ec2Svc, sgID)

		securityGroupsInfo = append(securityGroupsInfo, map[string]string{
			"Identifier":     sgID,
			"AWS_Service":    resourceType,
			"Name":           groupName,
			"Region":         region,
			"Status":         "N/A", // Update if needed
			"Creation_Time":  creationTime,
			"Deletion_Time":  "NA",
			"Tags":           tags,
		})
	}
}

return securityGroupsInfo

}

func getSecurityGroupTags(t *testing.T, ec2Svc *ec2.EC2, sgID string) string {
// Describe Tags for the Security Group
resp, err := ec2Svc.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(sgID)},
},
},
})
if err != nil {
log.Printf("Error fetching tags for Security Group %s: %v", sgID, err)
return "Error fetching tags"
}

if len(resp.Tags) == 0 {
	return "NA"
}

var tagString string
for _, tag := range resp.Tags {
	tagString += fmt.Sprintf("%s=%s, ", *tag.Key, *tag.Value)
}
return tagString[:len(tagString)-2] // Remove the last comma and space

}

func TestSecurityGroupInfoAllRegions(t *testing.T) {
securityGroups := getSecurityGroupInfoAllRegions(t)
for _, sg := range securityGroups {
t.Logf("Security Group: %+v\n", sg)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants