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

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?