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

Use the aws-sdk-go instance metadata client #2728

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions builder/amazon/chroot/step_instance_info.go
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer"
)

Expand All @@ -19,7 +19,8 @@ func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {

// Get our own instance ID
ui.Say("Gathering information about this EC2 instance...")
instanceIdBytes, err := common.GetInstanceMetaData("instance-id")
metadataClient := ec2metadata.New(&ec2metadata.Config{})
instanceId, err := metadataClient.GetMetadata("instance-id")
if err != nil {
log.Printf("Error: %s", err)
err := fmt.Errorf(
Expand All @@ -30,7 +31,6 @@ func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}

instanceId := string(instanceIdBytes)
log.Printf("Instance ID: %s", instanceId)

// Query the entire instance metadata
Expand Down
44 changes: 3 additions & 41 deletions builder/amazon/common/access_config.go
Expand Up @@ -2,14 +2,11 @@ package common

import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"unicode"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/mitchellh/packer/template/interpolate"
)

Expand Down Expand Up @@ -47,23 +44,9 @@ func (c *AccessConfig) Config() (*aws.Config, error) {
}, nil
}

// Region returns the aws.Region object for access to AWS services, requesting
// the region from the instance metadata if possible.
func (c *AccessConfig) Region() (string, error) {
if c.RawRegion != "" {
if valid := ValidateRegion(c.RawRegion); valid == false {
return "", fmt.Errorf("Not a valid region: %s", c.RawRegion)
}
return c.RawRegion, nil
}

md, err := GetInstanceMetaData("placement/availability-zone")
if err != nil {
return "", err
}

region := strings.TrimRightFunc(string(md), unicode.IsLetter)
return region, nil
client := ec2metadata.New(&ec2metadata.Config{})
return client.Region()
}

func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
Expand All @@ -80,24 +63,3 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {

return nil
}

func GetInstanceMetaData(path string) (contents []byte, err error) {
url := "http://169.254.169.254/latest/meta-data/" + path

resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
err = fmt.Errorf("Code %d returned for url %s", resp.StatusCode, url)
return
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return []byte(body), err
}