diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 388061bee..80d6ae1b9 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -198,6 +198,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state := new(multistep.BasicStateBag) state.Put("config", &b.config) state.Put("ec2", ec2conn) + state.Put("awsSession", session) state.Put("hook", hook) state.Put("ui", ui) state.Put("wrappedCommand", CommandWrapper(wrappedCommand)) @@ -305,7 +306,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe artifact := &awscommon.Artifact{ Amis: state.Get("amis").(map[string]string), BuilderIdValue: BuilderId, - Conn: ec2conn, + Session: session, } return artifact, nil diff --git a/builder/amazon/chroot/step_instance_info.go b/builder/amazon/chroot/step_instance_info.go index 00fd10dde..652808952 100644 --- a/builder/amazon/chroot/step_instance_info.go +++ b/builder/amazon/chroot/step_instance_info.go @@ -16,13 +16,13 @@ type StepInstanceInfo struct{} func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) + session := state.Get("awsSession").(*session.Session) ui := state.Get("ui").(packer.Ui) // Get our own instance ID ui.Say("Gathering information about this EC2 instance...") - sess := session.New() - ec2meta := ec2metadata.New(sess) + ec2meta := ec2metadata.New(session) identity, err := ec2meta.GetInstanceIdentityDocument() if err != nil { err := fmt.Errorf( diff --git a/builder/amazon/common/artifact.go b/builder/amazon/common/artifact.go index 1c3817d6f..90d7dec10 100644 --- a/builder/amazon/common/artifact.go +++ b/builder/amazon/common/artifact.go @@ -6,14 +6,14 @@ import ( "sort" "strings" + "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/hashicorp/packer/packer" ) // Artifact is an artifact implementation that contains built AMIs. type Artifact struct { - AccessConfig *AccessConfig - // A map of regions to AMI IDs. Amis map[string]string @@ -21,7 +21,7 @@ type Artifact struct { BuilderIdValue string // EC2 connection for performing API stuff. - Conn *ec2.EC2 + Session *session.Session } func (a *Artifact) BuilderId() string { @@ -69,11 +69,9 @@ func (a *Artifact) Destroy() error { for region, imageId := range a.Amis { log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region) - session, err := a.AccessConfig.Session() - if err != nil { - return err - } - regionConn := ec2.New(session) + regionConn := ec2.New(a.Session, &aws.Config{ + Region: aws.String(region), + }) // Get image metadata imageResp, err := regionConn.DescribeImages(&ec2.DescribeImagesInput{ diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go index 89a388205..d3183c7a7 100644 --- a/builder/amazon/common/step_create_tags.go +++ b/builder/amazon/common/step_create_tags.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" retry "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/packer" @@ -13,7 +14,6 @@ import ( ) type StepCreateTags struct { - AccessConfig *AccessConfig Tags map[string]string SnapshotTags map[string]string Ctx interpolate.Context @@ -21,6 +21,7 @@ type StepCreateTags struct { func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) + session := state.Get("awsSession").(*session.Session) ui := state.Get("ui").(packer.Ui) amis := state.Get("amis").(map[string]string) @@ -36,22 +37,16 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { } // Adds tags to AMIs and snapshots - for ami := range amis { + for region, ami := range amis { ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami)) - // Declare list of resources to tag - session, err := s.AccessConfig.Session() - if err != nil { - err := fmt.Errorf("Error creating AWS session: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - regionconn := ec2.New(session) + regionConn := ec2.New(session, &aws.Config{ + Region: aws.String(region), + }) // Retrieve image list for given AMI resourceIds := []*string{&ami} - imageResp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{ + imageResp, err := regionConn.DescribeImages(&ec2.DescribeImagesInput{ ImageIds: resourceIds, }) @@ -103,7 +98,7 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { // Retry creating tags for about 2.5 minutes err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) { // Tag images and snapshots - _, err := regionconn.CreateTags(&ec2.CreateTagsInput{ + _, err := regionConn.CreateTags(&ec2.CreateTagsInput{ Resources: resourceIds, Tags: amiTags, }) @@ -116,7 +111,7 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { // Override tags on snapshots if len(snapshotTags) > 0 { - _, err = regionconn.CreateTags(&ec2.CreateTagsInput{ + _, err = regionConn.CreateTags(&ec2.CreateTagsInput{ Resources: snapshotIds, Tags: snapshotTags, }) diff --git a/builder/amazon/common/step_modify_ami_attributes.go b/builder/amazon/common/step_modify_ami_attributes.go index 3e86e5391..295d98ce9 100644 --- a/builder/amazon/common/step_modify_ami_attributes.go +++ b/builder/amazon/common/step_modify_ami_attributes.go @@ -23,6 +23,7 @@ type StepModifyAMIAttributes struct { func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) + session := state.Get("awsSession").(*session.Session) ui := state.Get("ui").(packer.Ui) amis := state.Get("amis").(map[string]string) @@ -152,22 +153,13 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc // Modifying image attributes for region, ami := range amis { ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami)) - awsConfig := aws.Config{ - Credentials: ec2conn.Config.Credentials, - Region: aws.String(region), - } - session, err := session.NewSession(&awsConfig) - if err != nil { - err := fmt.Errorf("Error creating AWS session: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - regionconn := ec2.New(session) + regionConn := ec2.New(session, &aws.Config{ + Region: aws.String(region), + }) for name, input := range options { ui.Message(fmt.Sprintf("Modifying: %s", name)) input.ImageId = &ami - _, err := regionconn.ModifyImageAttribute(input) + _, err := regionConn.ModifyImageAttribute(input) if err != nil { err := fmt.Errorf("Error modify AMI attributes: %s", err) state.Put("error", err) @@ -181,16 +173,13 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc for region, region_snapshots := range snapshots { for _, snapshot := range region_snapshots { ui.Say(fmt.Sprintf("Modifying attributes on snapshot (%s)...", snapshot)) - awsConfig := aws.Config{ - Credentials: ec2conn.Config.Credentials, - Region: aws.String(region), - } - session := session.New(&awsConfig) - regionconn := ec2.New(session) + regionConn := ec2.New(session, &aws.Config{ + Region: aws.String(region), + }) for name, input := range snapshotOptions { ui.Message(fmt.Sprintf("Modifying: %s", name)) input.SnapshotId = &snapshot - _, err := regionconn.ModifySnapshotAttribute(input) + _, err := regionConn.ModifySnapshotAttribute(input) if err != nil { err := fmt.Errorf("Error modify snapshot attributes: %s", err) state.Put("error", err) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 392162652..e22901a74 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -113,6 +113,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("ec2", ec2conn) + state.Put("awsSession", session) state.Put("hook", hook) state.Put("ui", ui) @@ -269,7 +270,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe artifact := &awscommon.Artifact{ Amis: state.Get("amis").(map[string]string), BuilderIdValue: BuilderId, - Conn: ec2conn, + Session: session, } return artifact, nil diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index 4831b8ba7..c622a4b73 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -127,6 +127,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state := new(multistep.BasicStateBag) state.Put("config", &b.config) state.Put("ec2", ec2conn) + state.Put("awsSession", session) state.Put("hook", hook) state.Put("ui", ui) @@ -283,7 +284,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe artifact := &awscommon.Artifact{ Amis: amis.(map[string]string), BuilderIdValue: BuilderId, - Conn: ec2conn, + Session: session, } return artifact, nil diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 59c329515..776f4fc67 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -198,6 +198,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state := new(multistep.BasicStateBag) state.Put("config", &b.config) state.Put("ec2", ec2conn) + state.Put("awsSession", session) state.Put("hook", hook) state.Put("ui", ui) @@ -341,7 +342,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe artifact := &awscommon.Artifact{ Amis: state.Get("amis").(map[string]string), BuilderIdValue: BuilderId, - Conn: ec2conn, + Session: session, } return artifact, nil diff --git a/post-processor/amazon-import/post-processor.go b/post-processor/amazon-import/post-processor.go index c9cbd2077..90e87d6c7 100644 --- a/post-processor/amazon-import/post-processor.go +++ b/post-processor/amazon-import/post-processor.go @@ -369,7 +369,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac *config.Region: createdami, }, BuilderIdValue: BuilderId, - Conn: ec2conn, + Session: session, } if !p.config.SkipClean {