Skip to content

Commit

Permalink
Allow templated S3 bucket names
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Aug 25, 2017
1 parent 9b05cee commit 5934598
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/fatih/color"
"github.com/nsf/termbox-go"
"github.com/aws/aws-sdk-go/service/ec2"
"text/template"
"bytes"
"github.com/aws/aws-sdk-go/service/sts"
)

func AwsSession(profile, region string) *session.Session {
Expand Down Expand Up @@ -139,8 +142,29 @@ func commandInstanceIds(sess *session.Session, commandId string) CommandInstance
}
}

func realBucketName(sess *session.Session, input string) string {
tmpl, err := template.New("bucket").Parse(input)
if err != nil { log.Panicf(err.Error()) }

buf := bytes.Buffer{}

region := *sess.Config.Region
identity, _ := sts.New(sess).GetCallerIdentity(&sts.GetCallerIdentityInput{})
accountId := *identity.Account

err = tmpl.Execute(&buf, map[string]string{
"Region": region,
"AccountId": accountId,
})
if err != nil { log.Panicf(err.Error()) }

return buf.String()
}

func doit(sess *session.Session, targets []*ssm.Target, bucket, keyPrefix, command string, timeout int64) {
client := ssm.New(sess)
bucket = realBucketName(sess, bucket)

resp, err := client.SendCommand(&ssm.SendCommandInput{
DocumentName: aws.String("AWS-RunShellScript"),
Targets: targets,
Expand Down

0 comments on commit 5934598

Please sign in to comment.