func()

in pkg/ec2helper/ec2helper.go [750:806]


func (h *EC2Helper) CreateSecurityGroupForSsh(vpcId string) (*string, error) {
	fmt.Println("Creating new security group...")

	// Create a new security group
	creationInput := &ec2.CreateSecurityGroupInput{
		Description: aws.String("Created by simple-ec2 for SSH connection to instances"),
		GroupName:   aws.String("simple-ec2 SSH"),
		VpcId:       aws.String(vpcId),
	}

	creationOutput, err := h.Svc.CreateSecurityGroup(creationInput)
	if err != nil {
		return nil, err
	}

	// Add ingress rule for SSH
	groupId := *creationOutput.GroupId
	ingressInput := &ec2.AuthorizeSecurityGroupIngressInput{
		GroupId: aws.String(groupId),
		IpPermissions: []*ec2.IpPermission{
			{
				FromPort:   aws.Int64(22),
				IpProtocol: aws.String("tcp"),
				IpRanges: []*ec2.IpRange{
					{
						CidrIp: aws.String("0.0.0.0/0"),
					},
				},
				Ipv6Ranges: []*ec2.Ipv6Range{
					{
						CidrIpv6: aws.String("::/0"),
					},
				},
				ToPort: aws.Int64(22),
			},
		},
	}

	_, err = h.Svc.AuthorizeSecurityGroupIngress(ingressInput)
	if err != nil {
		return nil, err
	}

	// Create tags
	tags := append(getSimpleEc2Tags(), &ec2.Tag{
		Key:   aws.String("Name"),
		Value: aws.String("simple-ec2 SSH Security Group"),
	})
	err = h.createTags([]string{groupId}, tags)
	if err != nil {
		return nil, err
	}

	fmt.Println("New security group created successfully")

	return creationOutput.GroupId, nil
}