resources: Add UserData field to AwsEc2

UserData specifies first-launch bash and cloud-init commands. See
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
for documentation and examples.
This commit is contained in:
Jonathan Gold
2017-10-29 06:19:53 -04:00
parent 277ecc901b
commit e7c4bc7f47

View File

@@ -19,6 +19,7 @@ package resources
import ( import (
"context" "context"
"encoding/base64"
"fmt" "fmt"
"log" "log"
"sync" "sync"
@@ -66,10 +67,14 @@ var AwsRegions = []string{
// http://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html // http://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html
type AwsEc2Res struct { type AwsEc2Res struct {
BaseRes `yaml:",inline"` BaseRes `yaml:",inline"`
State string `yaml:"state"` // state: running, stopped, terminated State string `yaml:"state"` // state: running, stopped, terminated
Region string `yaml:"region"` Region string `yaml:"region"` // region must match an element of AwsRegions
Type string `yaml:"type"` // the ec2 instance type, ie. t2.micro Type string `yaml:"type"` // type of ec2 instance, ie. t2.micro
ImageID string `yaml:"imageid"` ImageID string `yaml:"imageid"` // imageid must be available on the chosen region
// UserData is used to run bash and cloud-init commands on first launch.
// See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
// for documantation and examples.
UserData string `yaml:"userdata"`
client *ec2.EC2 // client session for AWS API calls client *ec2.EC2 // client session for AWS API calls
} }
@@ -541,6 +546,10 @@ func (obj *AwsEc2Res) CheckApply(apply bool) (checkOK bool, err error) {
} }
runParams.SetMinCount(1) runParams.SetMinCount(1)
runParams.SetMaxCount(1) runParams.SetMaxCount(1)
if obj.UserData != "" {
userData := base64.StdEncoding.EncodeToString([]byte(obj.UserData))
runParams.SetUserData(userData)
}
runResult, err := obj.client.RunInstances(runParams) runResult, err := obj.client.RunInstances(runParams)
if err != nil { if err != nil {
return false, errwrap.Wrapf(err, "could not create instance") return false, errwrap.Wrapf(err, "could not create instance")
@@ -625,6 +634,9 @@ func (obj *AwsEc2Res) Compare(r Res) bool {
if obj.ImageID != res.ImageID { if obj.ImageID != res.ImageID {
return false return false
} }
if obj.UserData != res.UserData {
return false
}
return true return true
} }