This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related issue: #294 PiperOrigin-RevId: 281226268
- Loading branch information
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sysvars | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
) | ||
|
||
func ec2Vars(sysVars map[string]string) error { | ||
s, err := session.NewSession() | ||
if err != nil { | ||
return fmt.Errorf("ec2Vars: could not create session %v", err) | ||
} | ||
|
||
md := ec2metadata.New(s) | ||
// Doing the availability check in module since we need a session | ||
if md.Available() == false { | ||
sysVars["EC2_METADATA_Available"] = "false" | ||
return nil | ||
} | ||
|
||
id, err := md.GetInstanceIdentityDocument() | ||
if err != nil { | ||
sysVars["EC2_METADATA_Available"] = "false" | ||
return fmt.Errorf("ec2Vars: could not get instance document %v", err) | ||
} | ||
|
||
sysVars["EC2_METADATA_Available"] = "true" | ||
sysVars["EC2_AvailabilityZone"] = id.AvailabilityZone | ||
sysVars["EC2_PrivateIP"] = id.PrivateIP | ||
sysVars["EC2_Region"] = id.Region | ||
sysVars["EC2_InstanceID"] = id.InstanceID | ||
sysVars["EC2_InstanceType"] = id.InstanceType | ||
sysVars["EC2_ImageID"] = id.ImageID | ||
sysVars["EC2_KernelID"] = id.KernelID | ||
sysVars["EC2_RamdiskID"] = id.RamdiskID | ||
sysVars["EC2_Architecture"] = id.Architecture | ||
return nil | ||
} |