Skip to content
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

store: fail faster if not running in EC2 #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/alessio/shellescape v1.4.1
github.com/aws/aws-sdk-go v1.44.312
github.com/kevinburke/isec2 v0.1.0
github.com/magiconair/properties v1.8.7
github.com/segmentio/analytics-go/v3 v3.2.1
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
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/kevinburke/isec2 v0.1.0 h1:BhEAlWqZR6lNL7o/C3W46mhIBpJHimYNYQtXtvwXk6o=
github.com/kevinburke/isec2 v0.1.0/go.mod h1:axcGKXslLWfmqxnN+O0NqA8oGKqkQ37MQArV2brBVpw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
16 changes: 12 additions & 4 deletions store/shared.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package store

import (
"context"
"os"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/kevinburke/isec2"
)

const (
Expand Down Expand Up @@ -47,10 +50,15 @@ func getSession(numRetries int) (*session.Session, *string, error) {

// If region is still not set, attempt to determine it via ec2 metadata API
if aws.StringValue(retSession.Config.Region) == "" {
session := session.New()
ec2metadataSvc := ec2metadata.New(session)
if regionOverride, err := ec2metadataSvc.Region(); err == nil {
region = aws.String(regionOverride)
ec2ctx, ec2cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
runningEC2, ec2err := isec2.IsEC2(ec2ctx)
ec2cancel()
if ec2err == nil && runningEC2 {
session := session.New()
ec2metadataSvc := ec2metadata.New(session)
if regionOverride, err := ec2metadataSvc.Region(); err == nil {
region = aws.String(regionOverride)
}
}
}

Expand Down