@@ -1538,6 +1538,26 @@ func (d Deploy) orch(targetEnv string) error {
1538
1538
return err
1539
1539
}
1540
1540
1541
+ // getAWSAvailabilityZone retrieves the AWS availability zone using IMDSv2 with fallback to IMDSv1
1542
+ func getAWSAvailabilityZone () (string , error ) {
1543
+ // Try IMDSv2 first - requires getting a token
1544
+ tokenCmd := "curl -s -X PUT \" http://169.254.169.254/latest/api/token\" -H \" X-aws-ec2-metadata-token-ttl-seconds: 60\" "
1545
+ token , err := script .Exec (tokenCmd ).String ()
1546
+
1547
+ if err == nil && token != "" {
1548
+ // Use the token to get the AZ with IMDSv2
1549
+ azCmd := fmt .Sprintf ("curl -s -H \" X-aws-ec2-metadata-token: %s\" http://169.254.169.254/latest/meta-data/placement/availability-zone" , strings .TrimSpace (token ))
1550
+ az , err := script .Exec (azCmd ).String ()
1551
+ if err == nil && az != "" {
1552
+ return strings .TrimSpace (az ), nil
1553
+ }
1554
+ }
1555
+
1556
+ // Fall back to IMDSv1 if IMDSv2 fails
1557
+ az , err := script .Exec ("curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone" ).String ()
1558
+ return strings .TrimSpace (az ), err
1559
+ }
1560
+
1541
1561
func (d Deploy ) orchLocal (targetEnv string ) error {
1542
1562
targetConfig := getTargetConfig (targetEnv )
1543
1563
@@ -1578,7 +1598,7 @@ func (d Deploy) orchLocal(targetEnv string) error {
1578
1598
cmd = cmd + " " + fmt .Sprintf ("--set-string argo.aws.account=%s" , strings .Trim (awsAccountID , "\n " ))
1579
1599
1580
1600
// Get AWS region of this VM
1581
- az , err := script . Exec ( "curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone" ). String ()
1601
+ az , err := getAWSAvailabilityZone ()
1582
1602
if err != nil || az == "" {
1583
1603
return fmt .Errorf ("error retrieving the AWS AZ: %w" , err )
1584
1604
}
0 commit comments