Skip to content

Commit bdf1fcf

Browse files
authored
aws: allow EC2 instance access to metadata tags (#681)
At the moment it is not possible to allow EC2 instances to access their metadata tags when they are created. The changes in this PR adds a field within the AWS backend to create EC2 instances, specifying if the `InstanceMetadataTags` option in `InstanceMetadataOptions` is `disabled` (the default) or `enabled`. This option can be configured with the `instance_metadata_tags` option in `runner-manager.yaml`
1 parent e4e66df commit bdf1fcf

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

runner_manager/models/backend.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
from string import Template
44
from typing import Dict, List, Literal, Optional, Sequence, TypedDict
55

6-
from mypy_boto3_ec2.literals import InstanceTypeType, VolumeTypeType
6+
from mypy_boto3_ec2.literals import (
7+
InstanceMetadataTagsStateType,
8+
InstanceTypeType,
9+
VolumeTypeType,
10+
)
711
from mypy_boto3_ec2.type_defs import (
812
BlockDeviceMappingTypeDef,
913
EbsBlockDeviceTypeDef,
1014
IamInstanceProfileTypeDef,
15+
InstanceMetadataOptionsRequestTypeDef,
1116
TagSpecificationTypeDef,
1217
TagTypeDef,
1318
)
@@ -141,6 +146,7 @@ class AWSConfig(BackendConfig):
141146
"MaxCount": int,
142147
"MinCount": int,
143148
"IamInstanceProfile": IamInstanceProfileTypeDef,
149+
"MetadataOptions": InstanceMetadataOptionsRequestTypeDef,
144150
},
145151
)
146152

@@ -159,6 +165,7 @@ class AWSInstanceConfig(InstanceConfig):
159165
volume_type: VolumeTypeType = "gp3"
160166
disk_size_gb: int = 20
161167
iam_instance_profile_arn: str = ""
168+
instance_metadata_tags: InstanceMetadataTagsStateType = "disabled"
162169

163170
def configure_instance(self, runner: Runner) -> AwsInstance:
164171
"""Configure instance."""
@@ -197,6 +204,11 @@ def configure_instance(self, runner: Runner) -> AwsInstance:
197204
iam_instance_profile: IamInstanceProfileTypeDef = IamInstanceProfileTypeDef(
198205
Arn=self.iam_instance_profile_arn
199206
)
207+
instance_metadata_options: InstanceMetadataOptionsRequestTypeDef = (
208+
InstanceMetadataOptionsRequestTypeDef(
209+
InstanceMetadataTags=self.instance_metadata_tags,
210+
)
211+
)
200212
return AwsInstance(
201213
ImageId=self.image,
202214
InstanceType=self.instance_type,
@@ -208,6 +220,7 @@ def configure_instance(self, runner: Runner) -> AwsInstance:
208220
MinCount=self.min_count,
209221
BlockDeviceMappings=block_device_mappings,
210222
IamInstanceProfile=iam_instance_profile,
223+
MetadataOptions=instance_metadata_options,
211224
)
212225

213226

tests/unit/backend/test_aws.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def test_aws_instance_config(runner: Runner):
4949
tags={"test": "test"},
5050
subnet_id="i-0f9b0a3b7b3b3b3b3",
5151
iam_instance_profile_arn="test",
52+
instance_metadata_tags="enabled",
5253
)
5354
instance: AwsInstance = instance_config.configure_instance(runner)
5455
assert instance["ImageId"] == instance_config.image
@@ -57,6 +58,10 @@ def test_aws_instance_config(runner: Runner):
5758
instance["IamInstanceProfile"]["Arn"]
5859
== instance_config.iam_instance_profile_arn
5960
)
61+
assert (
62+
instance["MetadataOptions"]["InstanceMetadataTags"]
63+
== instance_config.instance_metadata_tags
64+
)
6065
assert runner.name in instance["UserData"]
6166
tags = instance["TagSpecifications"][0]["Tags"]
6267
assert TagTypeDef(Key="test", Value="test") in tags

0 commit comments

Comments
 (0)