Example #1
0
// AllSecurityGroups describes every instance in the requested regions
// *SecurityGroups are created for each *ec2.SecurityGroup
// and are passed to a channel
func AllSecurityGroups() chan *SecurityGroup {
	ch := make(chan *SecurityGroup, len(config.Regions))
	// waitgroup for all regions
	wg := sync.WaitGroup{}
	for _, region := range config.Regions {
		wg.Add(1)
		go func(region string) {
			defer wg.Done()
			// add region to waitgroup
			api := ec2.New(sess, aws.NewConfig().WithRegion(region))
			resp, err := api.DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{})
			for _, sg := range resp.SecurityGroups {
				ch <- NewSecurityGroup(region, sg)
			}
			if err != nil {
				// probably should do something here...
				log.Error(err.Error())
			}
		}(region)
	}
	go func() {
		// in a separate goroutine, wait for all regions to finish
		// when they finish, close the chan
		wg.Wait()
		close(ch)

	}()
	return ch
}
Example #2
0
func handleDescribeInstances(req *cwRequest, c *middleware.Context) {
	svc := ec2.New(&aws.Config{Region: aws.String(req.Region)})

	reqParam := &struct {
		Parameters struct {
			Filters     []*ec2.Filter `json:"filters"`
			InstanceIds []*string     `json:"instanceIds"`
		} `json:"parameters"`
	}{}
	json.Unmarshal(req.Body, reqParam)

	params := &ec2.DescribeInstancesInput{}
	if len(reqParam.Parameters.Filters) > 0 {
		params.Filters = reqParam.Parameters.Filters
	}
	if len(reqParam.Parameters.InstanceIds) > 0 {
		params.InstanceIds = reqParam.Parameters.InstanceIds
	}

	resp, err := svc.DescribeInstances(params)
	if err != nil {
		c.JsonApiErr(500, "Unable to call AWS API", err)
		return
	}

	c.JSON(200, resp)
}
Example #3
0
// clientEC2 creates a client to interact with AWS EC2 API.
func (b *backend) clientEC2(s logical.Storage, region string) (*ec2.EC2, error) {
	b.configMutex.RLock()
	if b.EC2ClientsMap[region] != nil {
		defer b.configMutex.RUnlock()
		// If the client object was already created, return it.
		return b.EC2ClientsMap[region], nil
	}

	// Release the read lock and acquire the write lock.
	b.configMutex.RUnlock()
	b.configMutex.Lock()
	defer b.configMutex.Unlock()

	// If the client gets created while switching the locks, return it.
	if b.EC2ClientsMap[region] != nil {
		return b.EC2ClientsMap[region], nil
	}

	// Create a AWS config object using a chain of providers.
	awsConfig, err := b.getClientConfig(s, region)
	if err != nil {
		return nil, err
	}

	// Create a new EC2 client object, cache it and return the same.
	b.EC2ClientsMap[region] = ec2.New(session.New(awsConfig))
	return b.EC2ClientsMap[region], nil
}
Example #4
0
func NewEbsVolumeDriver() (VolumeDriver, error) {
	d := &ebsVolumeDriver{
		volumes: make(map[string]*ebsVolume),
	}

	ec2sess := session.New()
	d.ec2meta = ec2metadata.New(ec2sess)

	// Fetch AWS information, validating along the way.
	if !d.ec2meta.Available() {
		return nil, errors.New("Not running on an EC2 instance.")
	}
	var err error
	if d.awsInstanceId, err = d.ec2meta.GetMetadata("instance-id"); err != nil {
		return nil, err
	}
	if d.awsRegion, err = d.ec2meta.Region(); err != nil {
		return nil, err
	}
	if d.awsAvailabilityZone, err =
		d.ec2meta.GetMetadata("placement/availability-zone"); err != nil {
		return nil, err
	}

	d.ec2 = ec2.New(ec2sess, &aws.Config{Region: aws.String(d.awsRegion)})

	// Print some diagnostic information and then return the driver.
	log("Auto-detected EC2 information:\n")
	log("\tInstanceId        : %v\n", d.awsInstanceId)
	log("\tRegion            : %v\n", d.awsRegion)
	log("\tAvailability Zone : %v\n", d.awsAvailabilityZone)
	return d, nil
}
Example #5
0
func getTags(region *string, instanceId *string) []string {
	output := []string{}

	service := ec2.New(&aws.Config{
		Region: region,
	})

	instances, err := service.DescribeInstances(&ec2.DescribeInstancesInput{
		InstanceIds: []*string{instanceId},
	})
	if err != nil {
		log.Fatal(err)
	}

	if len(instances.Reservations) > 1 {
		// more than one reservation isn't expected
		log.Fatal("Not Expected: Reservations > 1")
	}
	for _, res := range instances.Reservations {
		if len(res.Instances) > 1 {
			// more than one instance isn't expected
			log.Fatal("Not Expected: Instances > 1")
		}
		for _, instance := range res.Instances {
			for _, tag := range instance.Tags {
				output = append(output, *tag.Key+"="+*tag.Value)
			}
		}
	}
	return output
}
Example #6
0
func New(config Config) *Client {
	credentials := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
	sdkConfig := &aws.Config{
		Credentials: credentials,
		Region:      aws.String(config.RegionName),
	}

	endpointOverrides := config.EndpointOverrides
	if endpointOverrides == nil {
		endpointOverrides = &Endpoints{}
	}

	route53Client := route53.New(sdkConfig.Merge(&aws.Config{MaxRetries: aws.Int(7), Endpoint: aws.String(endpointOverrides.Route53)}))
	ec2Client := ec2.New(sdkConfig.Merge(&aws.Config{MaxRetries: aws.Int(7), Endpoint: aws.String(endpointOverrides.EC2)}))
	s3Client := s3.New(sdkConfig.Merge(&aws.Config{MaxRetries: aws.Int(7), Endpoint: aws.String(endpointOverrides.S3), S3ForcePathStyle: aws.Bool(true)}))
	cloudformationClient := cloudformation.New(sdkConfig.Merge(&aws.Config{MaxRetries: aws.Int(7), Endpoint: aws.String(endpointOverrides.Cloudformation)}))

	return &Client{
		EC2:            ec2Client,
		S3:             s3Client,
		Route53:        route53Client,
		Cloudformation: cloudformationClient,
		// HostedZoneID:   config.HostedZoneID,
		// HostedZoneName: config.HostedZoneName,
		Bucket: config.Bucket,
	}
}
Example #7
0
// GetSnapshotsByTag returns a slice of EBS Snapshots that match the provided region and Tag key/value
func GetSnapshotsByTag(region, key, value string) (Snapshots, error) {
	svc := ec2.New(session.New(&aws.Config{Region: aws.String(region)}))

	params := &ec2.DescribeSnapshotsInput{
		OwnerIds: []*string{aws.String("self")},
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag:" + key),
				Values: []*string{
					aws.String(value),
				},
			},
		},
	}

	result, err := svc.DescribeSnapshots(params)

	snapList := make(Snapshots, len(result.Snapshots))
	for i, snapshot := range result.Snapshots {
		snapList[i].Marshal(snapshot, region)
	}

	if len(snapList) == 0 {
		return snapList, errors.New("No Snapshot found with tag [" + key + "] of [" + value + "] in [" + region + "].")
	}

	return snapList, err
}
Example #8
0
// FindDefaultVPC looks for the default VPC in a given region
// and returns its ID if found.
func FindDefaultVPC(region string) (string, error) {
	ec2Service := ec2.New(&aws.Config{Region: aws.String(region)})

	// Call the DescribeInstances Operation
	resp, err := ec2Service.DescribeVpcs(&ec2.DescribeVpcsInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("isDefault"),
				Values: []*string{aws.String("true")},
			},
		},
	})
	if err != nil {
		return "", err
	}

	if len(resp.Vpcs) == 0 {
		return "", util.Errorf("no default VPC found in region %s", region)
	}
	if len(resp.Vpcs) > 1 {
		return "", util.Errorf("found %d default Vpcs in region %s", len(resp.Vpcs), region)
	}

	return *resp.Vpcs[0].VpcId, nil
}
Example #9
0
// This example will list instances with a filter
//
// Usage:
// filter_ec2_by_tag <name_filter>
func main() {
	sess, err := session.NewSession()
	if err != nil {
		log.Fatalf("failed to create session %v\n", err)
	}

	nameFilter := os.Args[1]
	awsRegion := "us-east-1"
	svc := ec2.New(sess, &aws.Config{Region: aws.String(awsRegion)})
	fmt.Printf("listing instances with tag %v in: %v\n", nameFilter, awsRegion)
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag:Name"),
				Values: []*string{
					aws.String(strings.Join([]string{"*", nameFilter, "*"}, "")),
				},
			},
		},
	}
	resp, err := svc.DescribeInstances(params)
	if err != nil {
		fmt.Println("there was an error listing instances in", awsRegion, err.Error())
		log.Fatal(err.Error())
	}
	fmt.Printf("%+v\n", *resp)
}
Example #10
0
func main() {
	svc := ec2.New(session.New(), &aws.Config{Region: aws.String("sa-east-1")})
	resp, err := svc.DescribeInstances(nil)
	if err != nil {
		panic(err)
	}
	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Tag Name", "Instance Id", "Instance Type", "AZ", "IP", "Status"})

	for _, res := range resp.Reservations {
		for _, instance := range res.Instances {
			var tag_name string
			for _, tag := range instance.Tags {
				if *tag.Key == "Name" {
					tag_name = *tag.Value
				}
			}
			table.Append([]string{
				tag_name,
				*instance.InstanceId,
				*instance.InstanceType,
				*instance.Placement.AvailabilityZone,
				*instance.PrivateIpAddress,
				*instance.State.Name,
			})

		}
	}
	table.Render()

}
func main() {
	defaults.DefaultConfig.Region = aws.String("us-west-2")
	svc := ec2.New(nil)

	var filters = []*ec2.Filter{
		&ec2.Filter{
			Name:   aws.String("instance-state-name"),
			Values: []*string{aws.String("running")},
		},
	}

	request := ec2.DescribeInstancesInput{Filters: filters}
	result, err := svc.DescribeInstances(&request)
	if err != nil {
		log.Fatalf("Error getting description: %s", err)
	}

	for _, reservation := range result.Reservations {
		for _, instance := range reservation.Instances {
			var id = *instance.InstanceId
			var publicIp = *instance.PublicIpAddress
			var privateIp = *instance.PrivateIpAddress
			var keyName = *instance.KeyName
			fmt.Printf("%s\t%s\t%s\t%s\n", id, publicIp, privateIp, keyName)
		}
	}
}
Example #12
0
func Stop(config *Config) {
	svc := ec2.New(session.New())

	for i := range config.EC2 {
		fmt.Println("Stopping ", config.EC2[i].Name)
		//fmt.Println(config.EC2[i].InstanceType)

		instances := getInstancesByName(svc, config.EC2[i].Name)
		for k := range instances {
			if *instances[k].State.Name == "terminated" {
				//fmt.Println("Instance is terminated:", *instances[k].InstanceId)
			} else {
				fmt.Println("Instance will be shutdown: ", *instances[k].InstanceId)
				instanceids := []*string{instances[k].InstanceId}
				sii := ec2.StopInstancesInput{InstanceIds: instanceids}
				_, err := svc.StopInstances(&sii)
				if err != nil {
					panic(err)
				}
				fmt.Println("Stopped instance ", *instances[k].InstanceId)

			}
		}
	}

}
Example #13
0
// GetImagesByTag returns a slice of Amazon Machine Images given the provided region, and tag key/values
func GetImagesByTag(region, key, value string) (Images, error) {
	svc := ec2.New(session.New(&aws.Config{Region: aws.String(region)}))

	params := &ec2.DescribeImagesInput{
		Owners: []*string{aws.String("self")},
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag:" + key),
				Values: []*string{
					aws.String(value),
				},
			},
		},
	}

	result, err := svc.DescribeImages(params)

	imgList := make(Images, len(result.Images))
	for i, image := range result.Images {
		imgList[i].Marshal(image, region)
	}

	if len(imgList) == 0 {
		return imgList, errors.New("No Images found with tag [" + key + "] of [" + value + "] in [" + region + "].")
	}

	return imgList, err

}
Example #14
0
// private function without prompts
func copyImage(image Image, region string, dryRun bool) (*ec2.CopyImageOutput, error) {

	svc := ec2.New(session.New(&aws.Config{Region: aws.String(region)}))

	// Copy image to the destination region
	params := &ec2.CopyImageInput{
		Name:          aws.String(image.Name),
		SourceImageId: aws.String(image.ImageID),
		SourceRegion:  aws.String(image.Region),
		DryRun:        aws.Bool(dryRun),
		//ClientToken: aws.String("String"),
		//Description: aws.String("String"),
		//Encrypted:   aws.Bool(true),
		//KmsKeyId:    aws.String("String"),
	}
	copyImageResp, err := svc.CopyImage(params)

	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok {
			return copyImageResp, errors.New(awsErr.Message())
		}
	}

	return copyImageResp, err
}
Example #15
0
File: api.go Project: rendon/insman
// GetRunningInstances returns the list of running instances.
func GetRunningInstances() ([]Instance, error) {
	var svc = ec2.New(session.New(), nil)

	request := ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			&ec2.Filter{
				Name:   aws.String("instance-state-name"),
				Values: []*string{aws.String("running")},
			},
		},
	}
	resp, err := svc.DescribeInstances(&request)
	if err != nil {
		return nil, err
	}

	var instances = make([]Instance, 0)
	for _, res := range resp.Reservations {
		for _, ins := range res.Instances {
			var tag = ""
			if len(ins.Tags) > 0 {
				tag = *ins.Tags[0].Value
			}
			var i = Instance{
				ID:        *ins.InstanceId,
				PublicIP:  *ins.PublicIpAddress,
				PrivateIP: *ins.PrivateIpAddress,
				Tag:       tag,
			}
			instances = append(instances, i)
		}
	}
	return instances, nil
}
Example #16
0
func NewEBSService() (*ebsService, error) {
	var err error

	s := &ebsService{}
	s.metadataClient = ec2metadata.New(nil)
	if !s.isEC2Instance() {
		return nil, fmt.Errorf("Not running on an EC2 instance")
	}

	s.InstanceID, err = s.metadataClient.GetMetadata("instance-id")
	if err != nil {
		return nil, err
	}

	s.Region, err = s.metadataClient.Region()
	if err != nil {
		return nil, err
	}

	s.AvailabilityZone, err = s.metadataClient.GetMetadata("placement/availability-zone")
	if err != nil {
		return nil, err
	}

	config := aws.NewConfig().WithRegion(s.Region)
	s.ec2Client = ec2.New(config)

	return s, nil
}
Example #17
0
func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
	ec2conn := state.Get("ec2").(*ec2.EC2)
	ui := state.Get("ui").(packer.Ui)
	amis := state.Get("amis").(map[string]string)

	if len(s.Tags) > 0 {
		for region, ami := range amis {
			ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami))

			var ec2Tags []*ec2.Tag
			for key, value := range s.Tags {
				ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value))
				ec2Tags = append(ec2Tags, &ec2.Tag{Key: &key, Value: &value})
			}

			regionconn := ec2.New(&aws.Config{
				Credentials: ec2conn.Config.Credentials,
				Region:      region,
			})
			_, err := regionconn.CreateTags(&ec2.CreateTagsInput{
				Resources: []*string{&ami},
				Tags:      ec2Tags,
			})
			if err != nil {
				err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err)
				state.Put("error", err)
				ui.Error(err.Error())
				return multistep.ActionHalt
			}
		}
	}

	return multistep.ActionContinue
}
Example #18
0
// amiRegionCopy does a copy for the given AMI to the target region and
// returns the resulting ID or error.
func amiRegionCopy(state multistep.StateBag, config *AccessConfig, imageId string,
	target string, source string) (string, error) {

	// Connect to the region where the AMI will be copied to
	awsConfig, err := config.Config()
	if err != nil {
		return "", err
	}
	awsConfig.Region = target

	regionconn := ec2.New(awsConfig)
	resp, err := regionconn.CopyImage(&ec2.CopyImageInput{
		SourceRegion:  &source,
		SourceImageID: &imageId,
	})

	if err != nil {
		return "", fmt.Errorf("Error Copying AMI (%s) to region (%s): %s",
			imageId, target, err)
	}

	stateChange := StateChangeConf{
		Pending:   []string{"pending"},
		Target:    "available",
		Refresh:   AMIStateRefreshFunc(regionconn, *resp.ImageID),
		StepState: state,
	}

	if _, err := WaitForState(&stateChange); err != nil {
		return "", fmt.Errorf("Error waiting for AMI (%s) in region (%s): %s",
			*resp.ImageID, target, err)
	}

	return *resp.ImageID, nil
}
Example #19
0
// GetRegionSnapshots returns a list of a regions Snapshots into the provided Snapshots slice that match the provided search term and optional completed flag
func GetRegionSnapshots(region string, snapList *Snapshots, search string, completed bool) error {
	svc := ec2.New(session.New(&aws.Config{Region: aws.String(region)}))
	result, err := svc.DescribeSnapshots(&ec2.DescribeSnapshotsInput{OwnerIds: []*string{aws.String("self")}})

	if err != nil {
		return err
	}

	snap := make(Snapshots, len(result.Snapshots))
	for i, snapshot := range result.Snapshots {
		snap[i].Marshal(snapshot, region)
	}

	if search != "" {
		term := regexp.MustCompile(search)
	Loop:
		for i, in := range snap {
			rInst := reflect.ValueOf(in)

			for k := 0; k < rInst.NumField(); k++ {
				sVal := rInst.Field(k).String()

				if term.MatchString(sVal) && ((completed && snap[i].State == "completed") || !completed) {
					*snapList = append(*snapList, snap[i])
					continue Loop
				}
			}
		}
	} else {
		*snapList = append(*snapList, snap[:]...)
	}

	return nil
}
Example #20
0
func main() {
	svc := ec2.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})

	// Call the DescribeInstances Operation
	resp, err := svc.DescribeInstances(nil)
	if err != nil {
		panic(err)
	}

	fmt.Println("> Number of reservation sets: ", len(resp.Reservations))

	for idx := range resp.Reservations {
		for _, inst := range resp.Reservations[idx].Instances {
			// for each instance lets parse out the info we want
			instance := parseInstanceInfo(inst)
			fmt.Println("Processing: ", instance.name, " (", instance.ip, ")")
			scanHost(&instance)
			instance.parsedXmlScanResult, err = nmap.Parse([]byte(instance.rawXmlScanResult))
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			parseOpenPorts(&instance)
			describeSecurityGroups(svc, &instance)
			//fmt.Println(instance.securityGroups)
		}
	}
}
func main() {
	kingpin.Version("2.1.0")
	kingpin.Parse()

	sl, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_LOCAL0, "[varnish-purge-proxy]")
	defer sl.Close()
	if err != nil {
		log.Println("Error writing to syslog")
	} else {
		log.SetFlags(0)
		log.SetOutput(sl)
	}

	if len(*tags) == 0 {
		fmt.Println("No tags specified")
		return
	}

	region, err = ec2metadata.New(session.New()).Region()
	if err != nil {
		log.Printf("Unable to retrieve the region from the EC2 instance %v\n", err)
	}

	// Set up access to ec2
	svc := ec2.New(session.New(), &aws.Config{Region: &region})

	go serveHTTP(*port, *listen, svc)

	select {}
}
Example #22
0
func testAccStepReadSTS(t *testing.T, name string) logicaltest.TestStep {
	return logicaltest.TestStep{
		Operation: logical.ReadOperation,
		Path:      "sts/" + name,
		Check: func(resp *logical.Response) error {
			var d struct {
				AccessKey string `mapstructure:"access_key"`
				SecretKey string `mapstructure:"secret_key"`
				STSToken  string `mapstructure:"security_token"`
			}
			if err := mapstructure.Decode(resp.Data, &d); err != nil {
				return err
			}
			log.Printf("[WARN] Generated credentials: %v", d)

			// Build a client and verify that the credentials work
			creds := credentials.NewStaticCredentials(d.AccessKey, d.SecretKey, d.STSToken)
			awsConfig := &aws.Config{
				Credentials: creds,
				Region:      aws.String("us-east-1"),
				HTTPClient:  cleanhttp.DefaultClient(),
			}
			client := ec2.New(session.New(awsConfig))

			log.Printf("[WARN] Verifying that the generated credentials work...")
			_, err := client.DescribeInstances(&ec2.DescribeInstancesInput{})
			if err != nil {
				return err
			}

			return nil
		},
	}
}
Example #23
0
// describeEc2Instances Describes EC2 instances by container istance ID
func describeEc2Instances(svc *ecs.ECS, cluster *string, containerInstances []*string) (*ec2.DescribeInstancesOutput, error) {

	params := &ecs.DescribeContainerInstancesInput{
		Cluster:            cluster,
		ContainerInstances: containerInstances,
	}
	ins, err := svc.DescribeContainerInstances(params)
	if err != nil {
		return nil, err
	}
	insfail := cli.Failure(ins.Failures, err)
	if insfail != nil {
		return nil, insfail
	}
	var ec2Instances = make([]*string, len(ins.ContainerInstances))
	for i, v := range ins.ContainerInstances {
		ec2Instances[i] = v.Ec2InstanceId
	}
	ec2client := ec2.New(sess.InitSession())
	ec2params := &ec2.DescribeInstancesInput{
		DryRun:      aws.Bool(false),
		InstanceIds: ec2Instances,
	}
	return ec2client.DescribeInstances(ec2params)
}
Example #24
0
// NewEC2Client creates an instance of ec2Client object.
func NewEC2Client(params *config.CliParams) EC2Client {
	client := ec2.New(session.New(params.Config))
	client.Handlers.Build.PushBackNamed(clients.CustomUserAgentHandler())
	return &ec2Client{
		client: client,
	}
}
func (p *EBSPlugin) prepare() error {
	if p.AccessKeyID != "" && p.SecretAccessKey != "" {
		p.Credentials = credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, "")
	}

	p.EC2 = ec2.New(session.New(&aws.Config{Credentials: p.Credentials, Region: &p.Region}))
	resp, err := p.EC2.DescribeVolumes(&ec2.DescribeVolumesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("attachment.instance-id"),
				Values: []*string{
					&p.InstanceID,
				},
			},
		},
	})
	if err != nil {
		return err
	}
	if resp.NextToken != nil {
		return errors.New("DescribeVolumes response has NextToken")
	}

	p.Volumes = resp.Volumes
	if len(p.Volumes) == 0 {
		return errors.New("DescribeVolumes response has no volumes")
	}

	return nil
}
Example #26
0
func newEC2(region string) *ec2.EC2 {
	session := session.New()
	return ec2.New(session, &aws.Config{
		Credentials: creds,
		Region:      aws.String(region),
	})
}
Example #27
0
func allNodes() NodeList {
	if list := cachedList(); list != nil {
		return list
	}

	client := ec2.New(session.New())
	params := &ec2.DescribeInstancesInput{}
	resp, err := client.DescribeInstances(params)

	if err != nil {
		fmt.Printf("Error connecting to AWS: %s\n", err)
		os.Exit(1)

		return nil
	}

	list := NodeList{}

	for _, reservation := range resp.Reservations {
		for _, instance := range reservation.Instances {
			ip := ipAddr(instance)
			name := instanceName(instance)

			list[name] = ip
		}
	}

	cacheList(list)
	return list
}
Example #28
0
// Get the resource tag value given the resource id
func getResourceTagValue(id, tag string) string {
	c := ec2.New(session.New(&aws.Config{Region: &region}))
	params := &ec2.DescribeTagsInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("resource-id"),
				Values: []*string{
					aws.String(id),
				},
			},
			{
				Name: aws.String("key"),
				Values: []*string{
					aws.String(tag),
				},
			},
		},
	}
	resp, err := c.DescribeTags(params)
	if err != nil {
		log.Printf("Cannot get tag %q of %q resource: %q.\n", tag, id, err)
		return ""
	}
	if len(resp.Tags) > 0 {
		for _, t := range resp.Tags {
			return *t.Value
		}
	}
	log.Printf("Cannot get tag %q of %q resource.\n", tag, id)
	return ""
}
Example #29
0
func (a *Artifact) Destroy() error {
	errors := make([]error, 0)

	for region, imageId := range a.Amis {
		log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region)

		regionConfig := &aws.Config{
			Credentials: a.Conn.Config.Credentials,
			Region:      aws.String(region),
		}
		regionConn := ec2.New(regionConfig)

		input := &ec2.DeregisterImageInput{
			ImageID: &imageId,
		}
		if _, err := regionConn.DeregisterImage(input); err != nil {
			errors = append(errors, err)
		}

		// TODO(mitchellh): Delete the snapshots associated with an AMI too
	}

	if len(errors) > 0 {
		if len(errors) == 1 {
			return errors[0]
		} else {
			return &packer.MultiError{errors}
		}
	}

	return nil
}
Example #30
0
// getInstances grabs a list of instances in a particular region and,
// optionally, accepts a regular expression to filter the results
func getInstances(region *string, filter string) {
	// initialise a connection to the EC2 API
	svc := ec2.New(session.New(), &aws.Config{
		Region: region})
	// call the DescribeInstances Operation
	resp, err := svc.DescribeInstances(nil)
	if err != nil {
		panic(err)
	}
	for r := range resp.Reservations {
		for _, i := range resp.Reservations[r].Instances {
			inst := instance{}

			// ignore nodes without an IP address
			if i.PrivateIpAddress == nil {
				continue
			}

			hasName := false
			var nameTag string

			for _, t := range i.Tags {
				if *t.Key != "Name" {
					continue
				} else {
					hasName = true
					nameTag = *t.Value
				}

			}

			// pump some metadata into our instance map
			inst.Id = *i.InstanceId

			if hasName {
				match, _ := regexp.MatchString(filter, nameTag)
				if match {
					inst.Name = nameTag
				} else {
					continue
				}
			}

			inst.PrivateIp = *i.PrivateIpAddress

			if i.PublicIpAddress != nil {
				inst.PublicIp = *i.PublicIpAddress
			}

			inst.AvailabilityZone = *i.Placement.AvailabilityZone

			// add the instance struct into the instances array
			instances = append(instances, inst)

		}
	}
	// pretty print the result as json
	out, _ = json.MarshalIndent(&instances, "", "  ")
	fmt.Println(string(out))
}