Need to override the constructor boto3.client
.
Assuming that tags are in LAUNCHTEMPLATE_TAGS_SPECIFICATIONS
constant:
# Will be wrapping the EC2.Client object to change its methods.
ORIG_BOTO3_CLIENT = boto3.client
def decorate_run_instances_with_tags_creation(orig_run_instances):
"""Add some AWS tags."""
def run_instances_emulate_tags_creation(*args, **kwargs):
assert "TagSpecifications" not in kwargs, "application has created tags, there may be a conflict with the unit tests"
kwargs["TagSpecifications"] = LAUNCHTEMPLATE_TAGS_SPECIFICATIONS
return orig_run_instances(*args, **kwargs)
return run_instances_emulate_tags_creation
def boto_client_factory_emulator(*args, **kwargs):
"""Retun an original EC2.Client with run_instances() that adds some AWS tags."""
client = ORIG_BOTO3_CLIENT(*args, **kwargs)
client.run_instances = decorate_run_instances_with_tags_creation(client.run_instances)
return client
And then patch functions or classes:
@patch("my_module.boto3.client", Mock(side_effect=boto_client_factory_emulator))