Unable to call my ec2-instance which receives a request response data from my worker script

I use a javascript in the workers in Cloudflare SaaS account that i am trying out for a POC.I create a request response object and am passing it to the public ip of my ec2-instance.
In order to receive it i have enabled all traffic on my ec2-instance.
Still i am getting a 502 error.Though i can make the same call from the postman installed on my PC,

Any idea as to what can be done to fix this?

Workers cannot fetch IP addresses.

1 Like

but then how do i send data to the ec2-instance.The reason I ask is because this is a production code that was working and I am just making some changes to the code and for testing,using my ec2-instance.

You have to create a DNS record pointing to your EC2 instance. Then you can fetch('https://ec2-instance.example.com/') in the Worker.

2 Likes

but being on a trial license i am unable to register domain as it involves cost.
Isn’t there any other way to get my script in worker to connect to the ec2-instance?i just need to send the data and check the json structure.

You need a domain name pointing to the EC2 instance to be able to send requests from a Worker. You can register a .xyz domain for $1 for the first year.

If you absolutely cannot afford to spend $1 on a domain, you have to option of getting a .eu.org domain for free here.

Created a dns entry in Cloudflare zero trust.Does that suffice?

You can’t create a DNS entry in Cloudflare Zero Trust. Not sure what it is you tried, but you need a proper domain added to your Cloudflare account. It does not matter whether it is a free .eu.org, a paid .xyz or something else.

Please see teh screenshots of what i did.I hope this helps explain what i did.

What you’re doing is creating a DNS override for clients connected to your Zero Trust organization. Workers do not connect through ZT so it will not work. Like I already said, you need to get a proper domain and create a DNS record. It is the only way.

2 Likes

Every ec2 instance already has a default public DNS record (hostname) given by AWS. Just look in the instance properties.

Ya i got that part albert.A final question -->Does docker tunnel used in Cloudflare work?

Created a domain in AWS and have successfully added it under websites in Cloudflare and am even able to access an application hosted on an AWS ec2 using the DNS mapping.
However even now when i call an api on another ec2-instance from inside the worker javascript it still gives a 502 gateway.I did also add the dns of this ec2-instance to the domain and i am able to perform a get to it using the domain from my browser.However the call from the worker script to the api on the ec2-instance gives a 502 bad gateway.
Am i missing some configuration here?

What port are you trying to reach on the second EC2 instance? Could you share the full URL (feel free to replace the domain with example.com)?

I am trying to access https://example.com:3000/myapi/v1/http/myurl from the worker.

Also in the website DNS i have added my reg website/domain viz., mytestingdomain.org as the A and the IP4 address of this ec2 instance.

In the worker route i have added mytestingdomain.org/* and added my script to it.These are the changes i did

You can only send requests to non-standard ports if the Worker runs on the same domain as the URL you are trying to fetch. Therefore, if your Worker runs on mytestingdomain.org/*, you must create a DNS record on e.g. origin.mytestingdomain.org pointing to your EC2 instance. You can then call fetch('https://origin.mytestingdomain.org:3000') in the Worker.

I had also made the call without providing the port https://example.com/myapi/v1/http/myurl.

The url on to which this worker will send data is provided by the user to whom the worker is delivered as a product.In this case too i have pointed the domain to the url of the instance to which the worker sends this request.So the code ideally would take the url provided by the user in the settings—>variables—> given here.I hope you can understand whta i am trying to convey.
However in the production sense also where this worker was earlier working the domain on which the worker runs and the place to where the worker sends its data is in a different domain.

I have as of now created the DNS record in the Cloudflare websites—>DNS settings.Added two entries