# Can lambda connect to external websocket?

**URL:** <https://awshq.com/t/can-lambda-connect-to-external-websocket/14018>\
**Category:** AWS Compute Discussion\
**Tags:** lambda, ec2\
**Created:** [December 4, 2021, 8:02pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018 "2021-12-04T20:02:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SzJack](https://awshq.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@SzJack](https://awshq.com/u/SzJack)\
**Post date:** [December 4, 2021, 8:02pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/1 "2021-12-04T20:02:50Z")

</div>

Hi!

I thought it was not possible, since lambda times out after 15 minutes and it’s mostly for short burst functions. But I’m not so sure anymore.

Can it connect to for example external exchange websocket? Or would I need to have EC2 instance for that?

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [December 4, 2021, 8:29pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/2 "2021-12-04T20:29:04Z")

</div>

You can connect, but as you pointed out, it will timeout after 15 minutes.

For this type of work, I suggest using Fargate - much less to manage than with EC2.

---

<div class="post-metadata">

**Author:** ![SzJack](https://awshq.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@SzJack](https://awshq.com/u/SzJack)\
**Post date:** [December 4, 2021, 8:30pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/3 "2021-12-04T20:30:08Z")

</div>

> [@mradzikowski](#):
>
> You can connect, but as you pointed out, it will timeout after 15 minutes. For this type of work, I suggest using Fargate - much less to manage than with EC2.

hmm Okay, I’ve never heard of Fargate [I’m a beginner] Does it integrate with lambda easily?

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [December 4, 2021, 8:42pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/4 "2021-12-04T20:42:31Z")

</div>

> [@SzJack](#):
>
> hmm Okay, I’ve never heard of Fargate [I’m a beginner] Does it integrate with lambda easily?

Fargate allows to run a docker container, for how long you need it. Just keep in mind it takes around minute to start it.  
You can start container from lambda, you can make trigger lambda from container, with usual AWS SDK calls.

---

<div class="post-metadata">

**Author:** ![fedspfedsp](https://awshq.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@fedspfedsp](https://awshq.com/u/fedspfedsp)\
**Post date:** [December 4, 2021, 8:42pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/5 "2021-12-04T20:42:35Z")

</div>

> [@SzJack](#):
>
> hmm Okay, I’ve never heard of Fargate [I’m a beginner] Does it integrate with lambda easily?

On your case fargate will replace lambda

---

<div class="post-metadata">

**Author:** ![SzJack](https://awshq.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@SzJack](https://awshq.com/u/SzJack)\
**Post date:** [December 4, 2021, 8:45pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/6 "2021-12-04T20:45:17Z")

</div>

> [@mradzikowski](#):
>
> Fargate allows to run a docker container, for how long you need it. Just keep in mind it takes around minute to start it. You can start container from lambda, you can make trigger lambda from container, with usual AWS …

I would need a constant stream, non stop 24/7 instead of invoking lambda every X seconds/minutes.

I already have a whole app written in Lambda, I’m just wondering if for some of the jobs Fargate would be a better choice. Thanks for giving me a direction, I’ll read more about it.

Dzięki!

---

<div class="post-metadata">

**Author:** ![BraveNewCurrency](https://awshq.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@BraveNewCurrency](https://awshq.com/u/BraveNewCurrency)\
**Post date:** [December 4, 2021, 11:07pm UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/7 "2021-12-04T23:07:10Z")

</div>

> Can lambda connect to external websocket?

Yes. But the “direct” use case is only useful for short “request reply” situations where Lambda makes a request to something listening on websockets, then lambda reads the reply and shuts off. Lambda isn’t useful for “run this all the time” websockets server.

On the other hand, you CAN fire off a lambda when you get a message using either API GW or IOT. (I think they both do it, but at least one can do it.) This is useful for the OTHER side of request reply, where a browser makes a request over WebSockets and lambda runs and generates a reply.

---

<div class="post-metadata">

**Author:** ![jonzezzz](https://awshq.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@jonzezzz](https://awshq.com/u/jonzezzz)\
**Post date:** [December 5, 2021, 12:39am UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/8 "2021-12-05T00:39:07Z")

</div>

You can if you use Api Gateway Websockets with Lambda: [https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html)

---

<div class="post-metadata">

**Author:** ![SzJack](https://awshq.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@SzJack](https://awshq.com/u/SzJack)\
**Post date:** [December 5, 2021, 1:09am UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/9 "2021-12-05T01:09:25Z")

</div>

> [@jonzezzz](#):
>
> You can if you use Api Gateway Websockets with Lambda: [https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html)

I don’t want to create a websocket, please read my post 🙂

---

<div class="post-metadata">

**Author:** ![jonzezzz](https://awshq.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@jonzezzz](https://awshq.com/u/jonzezzz)\
**Post date:** [December 5, 2021, 1:10am UTC](https://awshq.com/t/can-lambda-connect-to-external-websocket/14018/10 "2021-12-05T01:10:12Z")

</div>

> [@SzJack](#):
>
> I don’t want to create a websocket, please read my post 🙂

Ah sorry my bad
