From 92c21ed5b0c3c7b72f923790926cfff80d7de358 Mon Sep 17 00:00:00 2001 From: Steven Maude Date: Mon, 7 Apr 2025 18:05:09 +0100 Subject: [PATCH 1/3] Update code for AWS SDK for Go v2 --- main.go | 64 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 804857f..47586f4 100644 --- a/main.go +++ b/main.go @@ -1,45 +1,44 @@ package main import ( - "io/ioutil" + "context" + "io" "log" - "net/http" "os" "time" - "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/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" + "github.com/aws/aws-sdk-go-v2/service/ec2" ) -func Metadata(path string) (string, error) { - resp, err := http.Get("http://169.254.169.254/latest/meta-data/" + path) +func Metadata(imdsClient *imds.Client, path string) (string, error) { + metadataOutput, err := imdsClient.GetMetadata(context.Background(), &imds.GetMetadataInput{Path: path}) if err != nil { return "", err } - defer func() { - _ = resp.Body.Close() - }() + defer metadataOutput.Content.Close() - body, err := ioutil.ReadAll(resp.Body) + metadataOutputBytes, err := io.ReadAll(metadataOutput.Content) if err != nil { return "", err } - return string(body), nil + return string(metadataOutputBytes), nil } // Returns current IP address via metadata endpoint or "" on error. -func MyIP() string { - ip, _ := Metadata("public-ipv4") +func MyIP(imdsClient *imds.Client) string { + ip, _ := Metadata(imdsClient, "public-ipv4") return ip } -func WaitForIP(target string) bool { +func WaitForIP(imdsClient *imds.Client, target string) bool { const MAX = 120 // Maximum number of seconds to wait for i := 0; i < MAX; i++ { - ip := MyIP() + ip := MyIP(imdsClient) if ip == target { log.Printf("IP updated!: %q", ip) return true @@ -50,12 +49,12 @@ func WaitForIP(target string) bool { return false } -func ThisInstanceID() (string, error) { - return Metadata("instance-id") +func ThisInstanceID(imdsClient *imds.Client) (string, error) { + return Metadata(imdsClient, "instance-id") } -func ThisAvailabilityZone() (string, error) { - result, err := Metadata("placement/availability-zone") +func ThisAvailabilityZone(imdsClient *imds.Client) (string, error) { + result, err := Metadata(imdsClient, "placement/availability-zone") if err == nil { result = result[:len(result)-1] } @@ -71,26 +70,31 @@ func main() { } publicIP := args[0] - thisInstanceID, err := ThisInstanceID() + cfg, err := config.LoadDefaultConfig(context.Background()) + if err != nil { + log.Fatalf("Unable to load AWS config: %v", err) + } + + imdsClient := imds.NewFromConfig(cfg) + + thisInstanceID, err := ThisInstanceID(imdsClient) if err != nil { log.Fatalf("Unable to determine instance id: %v", err) } - thisAvailabilityZone, err := ThisAvailabilityZone() + thisAvailabilityZone, err := ThisAvailabilityZone(imdsClient) if err != nil { log.Fatalf("Unable to determine availability zone: %v", err) } log.Println("InstanceID:", thisInstanceID, "AZ:", thisAvailabilityZone) - s := session.Must(session.NewSession()) + cfg.Region = thisAvailabilityZone - svc := ec2.New(s, &aws.Config{ - Region: &thisAvailabilityZone, - }) + svc := ec2.NewFromConfig(cfg) - desc, err := svc.DescribeAddresses(&ec2.DescribeAddressesInput{ - PublicIps: []*string{aws.String(publicIP)}, + desc, err := svc.DescribeAddresses(context.Background(), &ec2.DescribeAddressesInput{ + PublicIps: []string{(publicIP)}, }) if err != nil { @@ -103,7 +107,7 @@ func main() { allocation := desc.Addresses[0] - resp, err := svc.AssociateAddress(&ec2.AssociateAddressInput{ + resp, err := svc.AssociateAddress(context.Background(), &ec2.AssociateAddressInput{ InstanceId: &thisInstanceID, AllowReassociation: aws.Bool(true), AllocationId: allocation.AllocationId, @@ -115,7 +119,7 @@ func main() { log.Println("Associated:", *resp.AssociationId) - if !WaitForIP(publicIP) { + if !WaitForIP(imdsClient, publicIP) { log.Fatal("Failed to see public IP update in a timely fashion.") } } From 90b063aed20163f2160913220c8575123660a826 Mon Sep 17 00:00:00 2001 From: Steven Maude Date: Mon, 7 Apr 2025 18:05:28 +0100 Subject: [PATCH 2/3] Update go.mod and go.sum --- go.mod | 20 ++++++++++++++++++-- go.sum | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 9da2218..b755c3b 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,22 @@ module github.com/sensiblecodeio/associate-eip go 1.23.0 -require github.com/aws/aws-sdk-go v1.55.5 +require ( + github.com/aws/aws-sdk-go-v2 v1.36.3 + github.com/aws/aws-sdk-go-v2/config v1.29.14 + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 + github.com/aws/aws-sdk-go-v2/service/ec2 v1.212.0 +) -require github.com/jmespath/go-jmespath v0.4.0 // indirect +require ( + github.com/aws/aws-sdk-go-v2/credentials v1.17.67 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.33.19 // indirect + github.com/aws/smithy-go v1.22.3 // indirect +) diff --git a/go.sum b/go.sum index d323266..a8b547b 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,28 @@ -github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= -github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM= +github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg= +github.com/aws/aws-sdk-go-v2/config v1.29.14 h1:f+eEi/2cKCg9pqKBoAIwRGzVb70MRKqWX4dg1BDcSJM= +github.com/aws/aws-sdk-go-v2/config v1.29.14/go.mod h1:wVPHWcIFv3WO89w0rE10gzf17ZYy+UVS1Geq8Iei34g= +github.com/aws/aws-sdk-go-v2/credentials v1.17.67 h1:9KxtdcIA/5xPNQyZRgUSpYOE6j9Bc4+D7nZua0KGYOM= +github.com/aws/aws-sdk-go-v2/credentials v1.17.67/go.mod h1:p3C44m+cfnbv763s52gCqrjaqyPikj9Sg47kUVaNZQQ= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 h1:x793wxmUWVDhshP8WW2mlnXuFrO4cOd3HLBroh1paFw= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30/go.mod h1:Jpne2tDnYiFascUEs2AWHJL9Yp7A5ZVy3TNyxaAjD6M= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 h1:ZK5jHhnrioRkUNOc+hOgQKlUL5JeC3S6JgLxtQ+Rm0Q= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34/go.mod h1:p4VfIceZokChbA9FzMbRGz5OV+lekcVtHlPKEO0gSZY= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 h1:SZwFm17ZUNNg5Np0ioo/gq8Mn6u9w19Mri8DnJ15Jf0= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34/go.mod h1:dFZsC0BLo346mvKQLWmoJxT+Sjp+qcVR1tRVHQGOH9Q= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 h1:bIqFDwgGXXN1Kpp99pDOdKMTTb5d2KyU5X/BZxjOkRo= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3/go.mod h1:H5O/EsxDWyU+LP/V8i5sm8cxoZgc2fdNR9bxlOFrQTo= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.212.0 h1:z5thR/zKUlw7gd1OT59xBHm4AKBf2kPXKHFvVzLMfBk= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.212.0/go.mod h1:ouvGEfHbLaIlWwpDpOVWPWR+YwO0HDv3vm5tYLq8ImY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 h1:eAh2A4b5IzM/lum78bZ590jy36+d/aFLgKF/4Vd1xPE= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3/go.mod h1:0yKJC/kb8sAnmlYa6Zs3QVYqaC8ug2AbnNChv5Ox3uA= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 h1:dM9/92u2F1JbDaGooxTq18wmmFzbJRfXfVfy96/1CXM= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15/go.mod h1:SwFBy2vjtA0vZbjjaFtfN045boopadnoVPhu4Fv66vY= +github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 h1:1Gw+9ajCV1jogloEv1RRnvfRFia2cL6c9cuKV2Ps+G8= +github.com/aws/aws-sdk-go-v2/service/sso v1.25.3/go.mod h1:qs4a9T5EMLl/Cajiw2TcbNt2UNo/Hqlyp+GiuG4CFDI= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1 h1:hXmVKytPfTy5axZ+fYbR5d0cFmC3JvwLm5kM83luako= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1/go.mod h1:MlYRNmYu/fGPoxBQVvBYr9nyr948aY/WLUvwBMBJubs= +github.com/aws/aws-sdk-go-v2/service/sts v1.33.19 h1:1XuUZ8mYJw9B6lzAkXhqHlJd/XvaX32evhproijJEZY= +github.com/aws/aws-sdk-go-v2/service/sts v1.33.19/go.mod h1:cQnB8CUnxbMU82JvlqjKR2HBOm3fe9pWorWBza6MBJ4= +github.com/aws/smithy-go v1.22.3 h1:Z//5NuZCSW6R4PhQ93hShNbyBbn8BWCmCVCt+Q8Io5k= +github.com/aws/smithy-go v1.22.3/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI= From d8d4558360964d4043ffdb3e67ec5c1d9cb62270 Mon Sep 17 00:00:00 2001 From: Steven Maude Date: Mon, 7 Apr 2025 18:06:07 +0100 Subject: [PATCH 3/3] Update build to Go 1.24.2 --- Dockerfile | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 71e0162..f1fe2ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.23.0-alpine +FROM golang:1.24.2-alpine # Turn off cgo for a more static binary. # Specify cache directory so that we can run as nobody to build the binary. diff --git a/go.mod b/go.mod index b755c3b..c28d338 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sensiblecodeio/associate-eip -go 1.23.0 +go 1.24.2 require ( github.com/aws/aws-sdk-go-v2 v1.36.3