Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elbv2 register_targets returning 200 when registering a stopped instance #8286

Open
smparekh opened this issue Nov 4, 2024 · 1 comment
Open

Comments

@smparekh
Copy link

smparekh commented Nov 4, 2024

From boto3 docs:

If the target is an EC2 instance, it must be in the running state when you register it.

Reproduce:

  • Create an ec2 instance in moto
  • Stop that instance
  • Create a target group and register that instance in the target group, moto returns 200
    • Expectation: ClientError (InvalidTargetError) is raised
EXAMPLE_AMI_ID = "ami-12c6146b"
pytest.aws_region = "us-west-2"

def test_tg_registration(elbv2):
    from boto3 import client, resource
    import os

    ec2_client = client("ec2")
    ec2_resource = resource("ec2")
    ec2_client.stop_instances(InstanceIds=[elbv2[0]])

    print(elbv2[0])

    res = ec2_resource.instances.filter(Filters=[{"Name": "instance-id", "Values": [elbv2[0]]}])
    for instance in res:
        print(instance.state)
    elb_client = client("elbv2")
    tg_res = elb_client.create_target_group(
            Name="test",
            Protocol="HTTP",
            Port=80,
            VpcId=os.environ["VPC_ID"],
            HealthCheckEnabled=True,
            HealthCheckProtocol="HTTP",
            HealthCheckPort=str(80),
            TargetType="instance",
        )
    print(tg_res["TargetGroups"][0]["TargetGroupArn"])
    target_reg_res = elb_client.register_targets(TargetGroupArn=tg_arn, Targets=targets)
    print(target_reg_res)

@pytest.fixture(scope="session")
def aws_credentials():
    os.environ["AWS_ACCESS_KEY_ID"] = "testing"
    os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
    os.environ["AWS_SECURITY_TOKEN"] = "testing"
    os.environ["AWS_SESSION_TOKEN"] = "testing"
    os.environ["AWS_DEFAULT_REGION"] = "us-west-2"
    os.environ["LOG_LEVEL"] = "debug"

@pytest.fixture(scope="session", autouse=True)
def ec2(aws_credentials):
    with mock_aws():
        client = boto3.client("ec2", region_name=pytest.aws_region)
        reservation = client.run_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
        instance = reservation["Instances"][0]
        instance_id = instance["InstanceId"]
        private_ip_address = instance["PrivateIpAddress"]
        vpc = client.create_vpc(CidrBlock="10.0.0.0/16")
        subnet = client.create_subnet(VpcId=vpc["Vpc"]["VpcId"], CidrBlock="10.0.0.0/18")
        subnet_id = subnet["Subnet"]["SubnetId"]
        os.environ["VPC_ID"] = vpc["Vpc"]["VpcId"]
        logger.info(">>> VPC ID: %s", vpc["Vpc"]["VpcId"])
        logger.info(">>> Subnet ID: %s", subnet["Subnet"]["SubnetId"])
        yield client, instance_id, subnet_id, private_ip_address

@pytest.fixture(scope="session", autouse=True)
def elbv2(ec2):
    with mock_aws():
        client = boto3.client("elbv2", region_name=pytest.aws_region)
        lb = client.create_load_balancer(Name="app-lb", Subnets=[ec2[2]])
        lb_arn = lb["LoadBalancers"][0]["LoadBalancerArn"]

        listener = client.create_listener(
            LoadBalancerArn=lb_arn,
            Protocol="HTTP",
            Port=80,
            DefaultActions=[
                {
                    "Type": "fixed-response",
                    "FixedResponseConfig": {
                        "MessageBody": "test",
                        "StatusCode": "200",
                        "ContentType": "text/plain",
                    },
                }
            ],
        )
        listener_arn = listener["Listeners"][0]["ListenerArn"]
        os.environ["LB_ARN"] = lb_arn
        os.environ["LISTENER_ARN"] = listener_arn

        yield ec2[1], lb_arn, listener_arn

Outputs:

LISTENER_ARN: arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/app-lb/50dc6c495c0c9188/804761638992
{'Code': 80, 'Name': 'stopped'}
arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/test/50dc6c495c0c9188
Target Registration Result: {'ResponseMetadata': {'RequestId': 'dbtP5ZFm6zIoVvxNN8VJi38KivH0HbJYuWeVZpLlWLaZPzasvAIW', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com', 'date': 'Sun, 03 Nov 2024 23:57:00 GMT', 'x-amzn-requestid': 'dbtP5ZFm6zIoVvxNN8VJi38KivH0HbJYuWeVZpLlWLaZPzasvAIW'}, 'RetryAttempts': 0}}
@bblommers
Copy link
Collaborator

Hi @smparekh, thanks for raising this! Marking it as an enhancement to add validation around this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants