Lambda Access to EC2 Service

0

Greetings,

Can a Lambda call an ECS Service endpoint if Service discovery is not enabled and only a public DNS record exists.

I am trying to connect a Lambda that is in the same VPC as the ECS Service. The Lambda is internal. I have done the following:

  1. Created an inbound security group rule on the ECS Service's security group (TCP 443) bound to the Lambda's Security Group
  2. Created an outbound security group rule on the Lambda (443 TCP) to the ECS Services security group.

This is for a test, and not a long-term config.

Since the public url can't be used, what is the simplest way to make a call to the ECS Service via the Lamda. Can I use the Load Balancer IP HTTP/HTTPS?

profile picture
asked 15 days ago69 views
2 Answers
1
Accepted Answer

Hello,

Yes, a Lambda function can call an ECS Service endpoint even if Service Discovery is not enabled. Here the traffic will route to public DNS if the VPC has access to internet then to a public IP and it will go to ECS. However, there are some considerations to keep in mind.

Since your Lambda function is in the same VPC as the ECS Service, you have a few options to establish connectivity:

  1. Use the internal DNS name of the Application Load Balancer (ALB) if you have one in front of your ECS Service. This is often the simplest and most reliable method. The internal DNS name of the ALB will resolve to private IP addresses within the VPC, allowing your Lambda to communicate with the ECS Service.

  2. If you're not using an ALB, you can use the private IP address of the ECS task. However, this is not recommended for production use as the IP address can change if the task is restarted.

  3. You cannot use the public DNS or IP address of the service from within the VPC, as this would route traffic out to the internet and back in, which won't work for an internal Lambda.

  4. Using the Load Balancer's IP address directly is not recommended, as these can change. Always use the DNS name instead.

For your test configuration, the simplest approach would be:

  1. If you have an ALB, use its internal DNS name.
  2. If not, you can temporarily use the private IP of the ECS task, but be aware this is not a stable long-term solution.

Your security group configuration sounds correct, allowing traffic between the Lambda and ECS Service. However, ensure that the ECS tasks are in private subnets if you want to keep them internal.

For a more robust, long-term solution, consider enabling ECS Service Discovery or using an Application Load Balancer. These methods provide stable endpoints for your services and offer better scalability and management of incoming traffic.

AWS
answered 15 days ago
0

Yes, a Lambda function can call an ECS Service endpoint even if Service Discovery is not enabled, but there are some considerations to keep in mind.

Since your Lambda function is in the same VPC as the ECS Service, you have a few options to establish connectivity:

  1. Use the internal DNS name of the Application Load Balancer (ALB) if you have one in front of your ECS Service. This is often the simplest and most reliable method. The internal DNS name of the ALB will resolve to private IP addresses within the VPC, allowing your Lambda to communicate with the ECS Service.

  2. If you're not using an ALB, you can use the private IP address of the ECS task. However, this is not recommended for production use as the IP address can change if the task is restarted.

  3. You cannot use the public DNS or IP address of the service from within the VPC, as this would route traffic out to the internet and back in, which won't work for an internal Lambda.

  4. Using the Load Balancer's IP address directly is not recommended, as these can change. Always use the DNS name instead.

For your test configuration, the simplest approach would be:

  1. If you have an ALB, use its internal DNS name.
  2. If not, you can temporarily use the private IP of the ECS task, but be aware this is not a stable long-term solution.

Your security group configuration sounds correct, allowing traffic between the Lambda and ECS Service. However, ensure that the ECS tasks are in private subnets if you want to keep them internal.

For a more robust, long-term solution, consider enabling ECS Service Discovery or using an Application Load Balancer. These methods provide stable endpoints for your services and offer better scalability and management of incoming traffic.
Sources
ECS Task with public ip connect permanent | AWS re:Post
Can I call a service that is inside Fargate from a lambda function? | AWS re:Post
Connect ECS Fargate Service/Task from public | AWS re:Post

profile picture
answered 15 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions