adding formatting
This commit is contained in:
parent
6e2146324f
commit
65d1447b64
|
@ -132,7 +132,7 @@ type RunConfig struct {
|
||||||
// The name of an [IAM instance
|
// The name of an [IAM instance
|
||||||
// profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html)
|
// profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html)
|
||||||
// to launch the EC2 instance with.
|
// to launch the EC2 instance with.
|
||||||
IamInstanceProfile string `mapstructure:"iam_instance_profile" required:"false"`
|
IamInstanceProfile string `mapstructure:"iam_instance_profile" required:"false"`
|
||||||
// Temporary IAM instance profile policy document
|
// Temporary IAM instance profile policy document
|
||||||
// If IamInstanceProfile is specified it will be used instead. Example:
|
// If IamInstanceProfile is specified it will be used instead. Example:
|
||||||
//
|
//
|
||||||
|
|
|
@ -3,16 +3,15 @@ package common
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/aws/aws-sdk-go/service/iam"
|
||||||
"github.com/hashicorp/packer/common/uuid"
|
"github.com/hashicorp/packer/common/uuid"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/aws/aws-sdk-go/service/iam"
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepIamInstanceProfile struct {
|
type StepIamInstanceProfile struct {
|
||||||
|
@ -87,8 +86,8 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
|
||||||
ui.Say(fmt.Sprintf("Creating temporary role for this instance: %s", profileName))
|
ui.Say(fmt.Sprintf("Creating temporary role for this instance: %s", profileName))
|
||||||
|
|
||||||
roleResp, err := iamsvc.CreateRole(&iam.CreateRoleInput{
|
roleResp, err := iamsvc.CreateRole(&iam.CreateRoleInput{
|
||||||
RoleName: aws.String(profileName),
|
RoleName: aws.String(profileName),
|
||||||
Description: aws.String("Temporary role for Packer"),
|
Description: aws.String("Temporary role for Packer"),
|
||||||
AssumeRolePolicyDocument: aws.String("{\"Version\": \"2012-10-17\",\"Statement\": [{\"Effect\": \"Allow\",\"Principal\": {\"Service\": \"ec2.amazonaws.com\"},\"Action\": \"sts:AssumeRole\"}]}"),
|
AssumeRolePolicyDocument: aws.String("{\"Version\": \"2012-10-17\",\"Statement\": [{\"Effect\": \"Allow\",\"Principal\": {\"Service\": \"ec2.amazonaws.com\"},\"Action\": \"sts:AssumeRole\"}]}"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -115,8 +114,8 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
|
||||||
ui.Say(fmt.Sprintf("Attaching policy to the temporary role: %s", profileName))
|
ui.Say(fmt.Sprintf("Attaching policy to the temporary role: %s", profileName))
|
||||||
|
|
||||||
_, err = iamsvc.PutRolePolicy(&iam.PutRolePolicyInput{
|
_, err = iamsvc.PutRolePolicy(&iam.PutRolePolicyInput{
|
||||||
RoleName: roleResp.Role.RoleName,
|
RoleName: roleResp.Role.RoleName,
|
||||||
PolicyName:aws.String(profileName),
|
PolicyName: aws.String(profileName),
|
||||||
PolicyDocument: aws.String(string(policy)),
|
PolicyDocument: aws.String(string(policy)),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -128,7 +127,7 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
|
||||||
s.createdPolicyName = aws.StringValue(roleResp.Role.RoleName)
|
s.createdPolicyName = aws.StringValue(roleResp.Role.RoleName)
|
||||||
|
|
||||||
_, err = iamsvc.AddRoleToInstanceProfile(&iam.AddRoleToInstanceProfileInput{
|
_, err = iamsvc.AddRoleToInstanceProfile(&iam.AddRoleToInstanceProfileInput{
|
||||||
RoleName: roleResp.Role.RoleName,
|
RoleName: roleResp.Role.RoleName,
|
||||||
InstanceProfileName: profileResp.InstanceProfile.InstanceProfileName,
|
InstanceProfileName: profileResp.InstanceProfile.InstanceProfileName,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -140,7 +139,7 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
|
||||||
s.roleIsAttached = true
|
s.roleIsAttached = true
|
||||||
state.Put("iamInstanceProfile", aws.StringValue(profileResp.InstanceProfile.InstanceProfileName))
|
state.Put("iamInstanceProfile", aws.StringValue(profileResp.InstanceProfile.InstanceProfileName))
|
||||||
|
|
||||||
time.Sleep(5*time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
|
@ -156,7 +155,7 @@ func (s *StepIamInstanceProfile) Cleanup(state multistep.StateBag) {
|
||||||
|
|
||||||
_, err := iamsvc.RemoveRoleFromInstanceProfile(&iam.RemoveRoleFromInstanceProfileInput{
|
_, err := iamsvc.RemoveRoleFromInstanceProfile(&iam.RemoveRoleFromInstanceProfileInput{
|
||||||
InstanceProfileName: aws.String(s.createdInstanceProfileName),
|
InstanceProfileName: aws.String(s.createdInstanceProfileName),
|
||||||
RoleName: aws.String(s.createdRoleName),
|
RoleName: aws.String(s.createdRoleName),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf(
|
ui.Error(fmt.Sprintf(
|
||||||
|
@ -168,7 +167,7 @@ func (s *StepIamInstanceProfile) Cleanup(state multistep.StateBag) {
|
||||||
ui.Say("Removing policy from temporary role...")
|
ui.Say("Removing policy from temporary role...")
|
||||||
iamsvc.DeleteRolePolicy(&iam.DeleteRolePolicyInput{
|
iamsvc.DeleteRolePolicy(&iam.DeleteRolePolicyInput{
|
||||||
PolicyName: aws.String(s.createdPolicyName),
|
PolicyName: aws.String(s.createdPolicyName),
|
||||||
RoleName: aws.String(s.createdRoleName),
|
RoleName: aws.String(s.createdRoleName),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if s.createdRoleName != "" {
|
if s.createdRoleName != "" {
|
||||||
|
@ -192,4 +191,4 @@ func (s *StepIamInstanceProfile) Cleanup(state multistep.StateBag) {
|
||||||
"Error %s. Please delete the instance profile manually: %s", err.Error(), s.createdInstanceProfileName))
|
"Error %s. Please delete the instance profile manually: %s", err.Error(), s.createdInstanceProfileName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue