@@ -72,23 +72,40 @@ Resources:
7272 <body>
7373 <center>
7474 <?php
75- # Get the instance ID from meta-data and store it in the $instance_id variable
75+ # Get session token for IMDSv2
76+ $token_url = "http://169.254.169.254/latest/api/token";
77+ $context = stream_context_create([
78+ "http" => [
79+ "method" => "PUT",
80+ "header" => "X-aws-ec2-metadata-token-ttl-seconds: 21600"
81+ ]
82+ ]);
83+ $token = file_get_contents($token_url, false, $context);
84+
85+ # Create context with token for metadata requests
86+ $metadata_context = stream_context_create([
87+ "http" => [
88+ "header" => "X-aws-ec2-metadata-token: " . $token
89+ ]
90+ ]);
91+
92+ # Get the instance ID from meta-data
7693 $url = "http://169.254.169.254/latest/meta-data/instance-id";
77- $instance_id = file_get_contents($url);
78- # Get the instance's availability zone from metadata and store it in the $zone variable
94+ $instance_id = file_get_contents($url, false, $metadata_context );
95+ # Get the instance's availability zone from metadata
7996 $url = "http://169.254.169.254/latest/meta-data/placement/availability-zone";
80- $zone = file_get_contents($url);
81- # Get the instance AMI ID and store it in the $ami_id variable
97+ $zone = file_get_contents($url, false, $metadata_context );
98+ # Get the instance AMI ID
8299 $url = "http://169.254.169.254/latest/meta-data/ami-id";
83- $ami_id = file_get_contents($url);
100+ $ami_id = file_get_contents($url, false, $metadata_context );
84101 ?>
85102 <h2>EC2 Instance ID: <?php echo $instance_id ?></h2>
86103 <h2>Availability Zone: <?php echo $zone ?></h2>
87104 <h2>AMI ID: <?php echo $ami_id ?></h2>
88105 </center>
89106 </body>
90107 </html>
91- mode : 000644
108+ mode : 644
92109 owner : apache
93110 group : apache
94111 /etc/cfn/cfn-hup.conf :
@@ -125,6 +142,10 @@ Resources:
125142 InstanceType : !FindInMap [EnvironmentToInstanceType, !Ref EnvironmentType, InstanceType]
126143 SecurityGroupIds :
127144 - !Ref WebServerSecurityGroup
145+ MetadataOptions :
146+ HttpTokens : required
147+ HttpPutResponseHopLimit : 1
148+ HttpEndpoint : enabled
128149 Tags :
129150 - Key : Name
130151 Value : !Join [ ' ', [ !Ref EnvironmentType, Web Server ] ]
0 commit comments