Skip to content

Instantly share code, notes, and snippets.

@inokappa
Created Jul 23, 2017
Embed
What would you like to do?
EC2 のタグを付けたり、外したり
package main
import (
"os"
"fmt"
"flag"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/ec2"
)
var (
argProfile = flag.String("profile", "", "Profile 名を指定.")
argRegion = flag.String("region", "ap-northeast-1", "Region 名を指定.")
argEndpoint = flag.String("endpoint", "", "AWS API のエンドポイントを指定.")
argInstances = flag.String("instances", "", "Instance ID 又は Instance Tag 名を指定.")
argTags = flag.String("tags", "", "Tag Key(Key=) 及び Tag Value(Value=) を指定.")
argAdd = flag.Bool("add", false, "タグをインスタンスに付与.")
argDel = flag.Bool("del", false, "タグをインスタンスから削除.")
)
func awsEc2Client(profile string, region string) *ec2.EC2 {
var config aws.Config
if profile != "" {
creds := credentials.NewSharedCredentials("", profile)
config = aws.Config{Region: aws.String(region), Credentials: creds, Endpoint: aws.String(*argEndpoint)}
} else {
config = aws.Config{Region: aws.String(region), Endpoint: aws.String(*argEndpoint)}
}
sess := session.New(&config)
ec2Client := ec2.New(sess)
return ec2Client
}
func createTag(ec2Client *ec2.EC2, instances string, tags []*ec2.Tag) {
splitedInstances := strings.Split(instances, ",")
var instanceIds []*string
for _, i := range splitedInstances {
instanceIds = append(instanceIds, aws.String(i))
}
input := &ec2.CreateTagsInput{
Resources: instanceIds,
Tags: tags,
// Tags: []*ec2.Tag{
// {
// Key: aws.String("Name"),
// Value: aws.String("xxxxxxxxxxxxxxxxxxxx"),
// },
// },
}
result, err := ec2Client.CreateTags(input)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(result)
}
func deleteTag(ec2Client *ec2.EC2, instances string, tags []*ec2.Tag) {
splitedInstances := strings.Split(instances, ",")
var instanceIds []*string
for _, i := range splitedInstances {
instanceIds = append(instanceIds, aws.String(i))
}
input := &ec2.DeleteTagsInput{
Resources: instanceIds,
Tags: tags,
}
result, err := ec2Client.DeleteTags(input)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(result)
}
func generateTags(tags string) []*ec2.Tag {
var allTags []*ec2.Tag
splitedTags := strings.Split(*argTags, " ")
for _, tag := range splitedTags {
var Tag *ec2.Tag
var tagKey string
var tagValue string
splitedTag := strings.Split(tag, ",")
for _, t := range splitedTag {
splitedT := strings.Split(t, "=")
if splitedT[0] == "Key" {
tagKey = splitedT[1]
} else if splitedT[0] == "Value" {
tagValue = splitedT[1]
} else {
fmt.Println("Tags Parse error.")
os.Exit(1)
}
}
Tag = &ec2.Tag{
Key: aws.String(tagKey),
Value: aws.String(tagValue),
}
allTags = append(allTags, Tag)
}
return allTags
}
func main() {
flag.Parse()
if *argTags == "" {
fmt.Println("Please Set `-tags`.")
os.Exit(1)
}
allTags := generateTags(*argTags)
ec2Client := awsEc2Client(*argProfile, *argRegion)
if *argAdd {
createTag(ec2Client, *argInstances, allTags)
} else if *argDel {
deleteTag(ec2Client, *argInstances, allTags)
} else {
fmt.Println("Please Set `-add` or `-del`.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment