Skip to content

Commit

Permalink
Add code for IMDS call if env unconfigured
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreya Anil Naik committed Sep 9, 2021
1 parent c4d61f3 commit 277a1d7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/aws/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type Cloud interface {

// NewCloud constructs new Cloud implementation.
func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud, error) {

metadata := (services.EC2Metadata)(nil)
if len(cfg.VpcID) == 0 {
metadataSess := session.Must(session.NewSession(aws.NewConfig()))
metadata := services.NewEC2Metadata(metadataSess)
metadata = services.NewEC2Metadata(metadataSess)
vpcId, err := metadata.VpcID()
if err != nil {
return nil, errors.Wrap(err, "failed to introspect vpcID from EC2Metadata, specify --aws-vpc-id instead if EC2Metadata is unavailable")
Expand All @@ -59,6 +59,18 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud,
if region == "" {
region = os.Getenv("AWS_REGION")
}

if region == ""{
if metadata == nil {
metadataSess := session.Must(session.NewSession(aws.NewConfig()))
metadata = services.NewEC2Metadata(metadataSess)
}
err := (error)(nil)
region, err = metadata.Region()
if err != nil {
return nil, errors.Wrap(err, "failed to introspect region from EC2Metadata, specify --aws-region instead if EC2Metadata is unavailable")
}
}
cfg.Region = region
}

Expand Down

0 comments on commit 277a1d7

Please sign in to comment.