Skip to content

Update to v2 of the AWS SDK #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 19 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
module github.com/sensiblecodeio/associate-eip

go 1.23.0
go 1.24.2

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
)
42 changes: 28 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
64 changes: 34 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
}
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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.")
}
}
Loading