Skip to content

Commit

Permalink
- Create EC2 instances with rand ebs mappings
Browse files Browse the repository at this point in the history
- CreateS3 buckets with three level nested objects
- Create Key Pairs
- Alter regions in ec2
  • Loading branch information
Abrar-Ahmed7 committed Jan 24, 2023
1 parent f466843 commit c8b2ad8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
5 changes: 5 additions & 0 deletions cmd/localstackpopulator.go
Expand Up @@ -54,6 +54,11 @@ var lspop = &cobra.Command{
if errCEI != nil {
log.Fatal("err: ", errCEI)
}

errCKP := pop.CreateKeyPair(sessions)
if errCKP != nil {
log.Fatal("err: ", errCKP)
}
},
}

Expand Down
7 changes: 6 additions & 1 deletion internal/aws/ec2.go
Expand Up @@ -46,7 +46,12 @@ type Ec2Service interface {
}

func GetAllRegions() []string {
return []string{"ap-south-1", "us-east-2", "us-west-1", "us-west-2", "af-south-1", "ap-east-1", "ap-south-2", "ap-southeast-3", "us-east-1", "ap-northeast-3", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2", "eu-south-1", "eu-west-3", "eu-south-2", "eu-north-1", "eu-central-2", "me-south-1", "me-central-1", "sa-east-1", "us-gov-east-1", "us-gov-west-1"}
return []string{
"us-east-1", "us-east-2", "us-west-1", "us-west-2", "af-south-1", "ap-east-1",
"ap-southeast-3", "ap-south-1", "ap-northeast-3", "ap-northeast-2",
"ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "ca-central-1", "eu-central-1",
"eu-west-1", "eu-west-2", "eu-south-1", "eu-west-3", "eu-north-1",
"me-south-1", "me-central-1", "sa-east-1", "us-gov-east-1", "us-gov-west-1"}
}

func GetInstances(sess session.Session) ([]EC2Resp, error) {
Expand Down
128 changes: 72 additions & 56 deletions populator/populator.go
@@ -1,6 +1,7 @@
package pop

import (
"bytes"
"fmt"
"log"
"strconv"
Expand All @@ -14,76 +15,66 @@ import (

func CreateBuckets(sess *session.Session) error {
s3Service := s3.New(sess)
for i := 0; i < 10; i++ {
for i := 0; i < 20; i++ {
gofakeit.Seed(0)
rWord := gofakeit.Password(true, false, true, false, false, 5)
_, err := s3Service.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(rWord + "-bucket" + strconv.Itoa(gofakeit.Number(0, 999999999999999999))), CreateBucketConfiguration: &s3.CreateBucketConfiguration{LocationConstraint: aws.String("ap-south-1")}})
bName := aws.String(rWord + "-bucket" + strconv.Itoa(gofakeit.Number(0, 999999999999999999)))
_, err := s3Service.CreateBucket(&s3.CreateBucketInput{
Bucket: bName,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{LocationConstraint: aws.String("ap-south-1")},
})
if err != nil {
log.Println("S3 err here: ", rWord, err)
return err
}

key := []string{"Delhi", "Asia/Japan", "Asia/China/Beijing", "Jakarta", "Africa/Ghana", "North-America/Canada/Toronto",
"Africa", "Africa/Jamaice", "Europe/England/London", "Vietnam", "Asia/South-Korea", "Asia/India/Kolkata",
"Australia/Sydney", "Paris", "India/Kerala/Kochi", "Asia/Sri-Lanka", "Asia/Indonesia", "Europe/France", "Europe/Sweden",
"Africa/West-Indies/City1", "North-America/USA/New-York", "Asia/India/Bangalore", "Asia/Nepal", "Asia/Burma"}
for i := 0; i < len(key); i++ {

body := []byte(gofakeit.Name())
s3Service.PutObject(&s3.PutObjectInput{
Bucket: bName,
Key: aws.String(key[i]),
Body: bytes.NewReader(body),
})
}
}
return nil
}

func CreateEC2Instances(sess []*session.Session) error {

/*
- Create VPC and get VPC ID
- There are several type of EBS,
- GP2 (default) (optimize not available)
- GP3
- IO2
- IO1
- Standard (optimize not available)
- SC1
- ST1
- EBS: (create volume) use the above VPC ID
- Create Instance
- Based on the instance type you can attach 1 or more ebs to it
- Based on volume type, device name can be assigned (ex: dec/sda1)
- Volumes will be available based on instance type and zone (imp)
*/
insType := []string{"t2.micro", "t2.nano", "t2.small", "t2.medium", "t2.large",
"t3a.nano", "t3a.micro", "t3a.small", "t3a.medium", "t3a.large",
"t3.nano", "t3.micro", "t3.small", "t3.medium", "t3.large"}

// Consider 3-5 sessions
// Create VPC, Subnets, SG, Volumes
// Create 5-10 EC Instances for each Sessions.

for _, s := range sess {
ec2Service := ec2.New(s)
gofakeit.Seed(0)
vpc, err := createVpc(ec2Service)
if err != nil {
fmt.Println(err)
}

sn, err := createSubnet(ec2Service, vpc.VpcId)
if err != nil {
fmt.Println(err)
}

sg, err := createSecGrp(ec2Service, vpc.VpcId)
if err != nil {
fmt.Println(err)
}

// v, err := createVolume(ec2Service)
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println(v.VolumeId)
blockDeviceMapping := createEbsMapping()

for i := 0; i < 10; i++ {
ebs := ec2.EbsBlockDevice{
VolumeSize: aws.Int64(8),
VolumeType: aws.String("gp2"),
}

bdm := ec2.BlockDeviceMapping{
DeviceName: aws.String("dev/sdf"),
Ebs: &ebs,
}

ec2Tag := []*ec2.TagSpecification{
{
Expand All @@ -101,30 +92,37 @@ func CreateEC2Instances(sess []*session.Session) error {
},
}

ec2, err := ec2Service.RunInstances(&ec2.RunInstancesInput{
BlockDeviceMappings: []*ec2.BlockDeviceMapping{&bdm},
ElasticGpuSpecification: []*ec2.ElasticGpuSpecification{},
ImageId: aws.String("ami-" + strconv.Itoa(gofakeit.Number(0, 9999999))),
InstanceType: aws.String(insType[gofakeit.Number(0, len(insType)-1)]),
MaxCount: aws.Int64(2),
MinCount: aws.Int64(1),
SecurityGroupIds: []*string{sg.GroupId},
SubnetId: aws.String(*sn.SubnetId),
TagSpecifications: ec2Tag,
_, err := ec2Service.RunInstances(&ec2.RunInstancesInput{
BlockDeviceMappings: blockDeviceMapping,
ImageId: aws.String("ami-" + strconv.Itoa(gofakeit.Number(0, 9999999))),
InstanceType: aws.String(gofakeit.RandString(insType)),
MaxCount: aws.Int64(2),
MinCount: aws.Int64(1),
SecurityGroupIds: []*string{sg.GroupId},
SubnetId: aws.String(*sn.SubnetId),
TagSpecifications: ec2Tag,
})
if err != nil {
fmt.Println(err)
fmt.Println(fmt.Errorf("error in creating EC2 instance: %v", err))
}

ins := ec2.Instances
insId := ins[0].InstanceId
makeSubnetPublic(ec2Service, vpc.VpcId, sn.SubnetId, insId)

// fmt.Println("EC2 Instance: ", ec2)
}

}
return nil
}

func CreateKeyPair(sess []*session.Session) error {
for _, s := range sess {
service := ec2.New(s)
gofakeit.Seed(0)
_, err := service.CreateKeyPair(&ec2.CreateKeyPairInput{
KeyFormat: aws.String("PEM"),
KeyName: aws.String(gofakeit.FirstName()),
KeyType: aws.String("ssh-rsa"),
})
if err != nil {
return fmt.Errorf("error in creating Key Pairs: %v", err)
}
}
return nil
}

Expand Down Expand Up @@ -212,7 +210,28 @@ func createSecGrp(service *ec2.EC2, vpcId *string) (*ec2.CreateSecurityGroupOutp
return sg, nil
}

func createEbsMapping() []*ec2.BlockDeviceMapping {
volumeSize := []int64{1, 2, 4, 8, 32, 64, 128}
deviceName := []string{"dev/sdf", "dev/sdg", "dev/sdh", "dev/sdi", "dev/sdj", "dev/sdk", "dev/sdl", "dev/sdm", "dev/sdn", "dev/sdo", "dev/sdp"}
blockDeviceMapping := []*ec2.BlockDeviceMapping{}

for i := 0; i < gofakeit.Number(1, 3); i++ {
ebs := ec2.EbsBlockDevice{
VolumeSize: aws.Int64(volumeSize[gofakeit.Number(0, len(volumeSize)-1)]),
VolumeType: aws.String("gp2"), // for t-type instances gp2 alone is supported
}
bdm := ec2.BlockDeviceMapping{
DeviceName: aws.String(gofakeit.RandString(deviceName)),
Ebs: &ebs,
}
blockDeviceMapping = append(blockDeviceMapping, &bdm)
}

return blockDeviceMapping
}

func createVolume(service *ec2.EC2) (*ec2.Volume, error) {
volumeSize := []int64{1, 2, 4, 8, 32, 64, 128}
vTag := []*ec2.TagSpecification{
{
ResourceType: aws.String("instance"),
Expand All @@ -231,7 +250,7 @@ func createVolume(service *ec2.EC2) (*ec2.Volume, error) {

v, err := service.CreateVolume(&ec2.CreateVolumeInput{
AvailabilityZone: aws.String("us-east-1a"),
Size: aws.Int64(8),
Size: aws.Int64(volumeSize[gofakeit.Number(0, len(volumeSize)-1)]),
TagSpecifications: vTag,
VolumeType: aws.String("gp2"),
})
Expand All @@ -244,10 +263,7 @@ func createVolume(service *ec2.EC2) (*ec2.Volume, error) {

// working on it
func makeSubnetPublic(service *ec2.EC2, vpcId, snId, insId *string) {
igw, _ := service.CreateInternetGateway(&ec2.CreateInternetGatewayInput{
DryRun: new(bool),
TagSpecifications: []*ec2.TagSpecification{},
})
igw, _ := service.CreateInternetGateway(&ec2.CreateInternetGatewayInput{})

_, _ = service.AttachInternetGateway(&ec2.AttachInternetGatewayInput{
InternetGatewayId: igw.InternetGateway.InternetGatewayId,
Expand Down

0 comments on commit c8b2ad8

Please sign in to comment.