Timeout calling PRIVATE API Gateway from another AWS account - amazon-web-services

I am trying to call my private API gateway from a lambda function in another AWS account. But i am getting timeout while making the call (Actually the http call is just stuck and hits my client side timeout).
In the AWS account which hosts the private AWS account i did the following
* Created my private API gateway and lambda behind the API
Create resource policy and provided access to the VPC endpoint of the other AWS caller (caller)
create VPC endpoint with private DNS enabled.
Attached policy to VPC endpoint with full access, security group with access to all traffic.
In the second AWS account (caller) i did the following
* Create VPC and VPC endpoint for API gateway with full access.
created lambda in the VPC with security group with access to all traffic (inbound and outbound)
In lambda I am using the public DNS of the VPC endpoint(Account 1) and used header 'x-apigw-api-id'
I already tried the debugging steps mentioned in this page https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-private-endpoint-connection/.
Also tried whitelisting based on the VPC ID instead of VPC endpoint ID.
Any suggestions for resolving this issue.

Just found the isssue. I was using the VPC endpoint of the other aws account while making a call. When i used the VPC endpoint of the same account it worked.

Related

How to prevent AWS execute-api VPC endpoint blocking other accounts API gateways?

weird functionality with AWS execute-api VPC endpoint and wondering if anyone has found a workaround?
Let's say there is a lambda function running inside VPC and it has API gateway pointing to it. On top of that, VPC has execute-api VPC endpoint with private DNS enabled, so that the API gateway DNS name returns the internal IP instead of public IP.
Above works fine but the downside is that now it prevents us connecting to some other AWS accounts API gateway as all the api-gateway subdomains are now resolving to internal IPs?
Any workaround on this?
Didn't find any solution, so decided to create yet another VPC where execute-api VPC endpoint is not enabled and moved external connection components into that VPC.

how to connect a lambda function (behind an api gateway) to a private rds in a vpc

I have the following AWS VPC configuration:
2 public subnets
2 private subnets
Cloudfront is in front of the load balancer. The load balancer is linked to the public subnets and my RDS database reside in the private subnets.
What i want to accomplish is:
Create an API Gateway (publicly accessible) but restricted to only a specific IP address that can access it. The API Gateway is connected to a lambda function that, when called/triggered, the function must update some table data of my (private) RDS Postgres instance.
From my understanding the first step would be to create an IAM role for the lambda function to be only accessible by the IP address mentioned above.
Now what is the best way to access a private RDS from that lambda connected to API Gateway? Especially without using Nat Gateway because of the crazy cost associated to it.
Can someone help me just figure out how to do it, i am referring to the high level understanding how to do it?
As Norman said in their comment, you can control access to your API in API Gateway, and limit it to certain IP addresses, with API Gateway Resource Policies. This will at the same time control access to your Lambda that's behind that API.
You can then edit the Lambdas Configuration -> VPC setting and add it to a subnet that has access to your RDS database (and put it either in the same subnet as the database, or another subnet that has access permissions to the RDS subnet). To connect your Lambda to a VPC, the Lambda execution role will need the following permissions
ec2:CreateNetworkInterface
ec2:DescribeNetworkInterfaces
ec2:DeleteNetworkInterface
They are for example included in the managed policy AWSLambdaVPCAccessExecutionRole. Here you can read more about configuring a Lambda for VPC access.

How can I use a vpc security group to restrict inbound access to my Lambda?

I have a simply maked it with serverless endpoint for check some token. Lets say the path is "/checktoken". And want to access to it only with my Ip address, thats why connect my Lambda with VPC(2 private subnets and security group).
But problem is even I maked security group in/outbound rules as https with my IP address, I can request to the endpoint with different IP address.
I have used AWS = CloudFront + Route53 for DNS, Lambda + Api Gateway for endpoint.
I made a whitelist IP with resource policy in Api gateway which works, but its not the solution I want.
The required solution is vpc's security group should allow request only from whitelisted IP in its rule.
Adding the Lambda to a VPC will not direct traffic to the Lambda through your VPC.
In fact The Lambda is invoked through the Lambda API Service Endpoint, therefore inbound evaluation rules will have no effect on it at all. The purpose of a Lambda residing in a VPC is to access VPC resources.
The technical implementation is that an ENI is created in your VPC, that connects to the Lambda function in a shared AWS VPC. The Lambda is able to route out of its shared VPC to connect to resources.
When you configure your Lambda function to connect to your own VPC, it creates an elastic network interface in your VPC and then does a cross-account attachment. These network interfaces allow network access from your Lambda functions to your private resources. These Lambda functions continue to run inside of the Lambda service’s VPC and can now only access resources over the network through your VPC.
You will need to add these IP whitelists at either the CloudFront level through the use of a AWS WAF (using an IPSet), or through the API Gateway as a WAF or Policy (as you mentioned above).
thats why connect my Lambda with VPC(2private subnets and security group)
Placing lambda function in a VPC and giving it a security group does not have effect on whether the API gateway can invoke it or not.
API gateway does not use elastic network interface (ENI) of the lambda in the VPC to invoke it. Its done using Lambda service endpoint.
You can use WAF to control access to your CloudFront distribution:
Using AWS WAF to Control Access to Your Content
Try Black / White listing IPAdresses:
https://www.npmjs.com/package/serverless-secure

Access API Gateway from EC2

I have 1 Lambda Function connected to AWS Oracle RDS. I have exposed lambda to outer world via API Gateway.
How can i access same API Gateway in my EC2 Instance where i have my back-end services running?
[ec2-user#ip-22-22-22-22 dummy]$ ./telnet 10apexecute2.execute-api.eu-west-1.amazonaws.com 443
Trying 55.11.144.98...
Please help
As the account must be restrictive with its outbound communication over internet, the suggested solution would to make use of a VPC endpoint for API Gateway.
Once you create this endpoint, make sure to enable the "Enable Private DNS Name" option to override the DNS resolution for execute-api endpoints to speak to resolve through your VPC endpoint.
More information can be found in this blog post: Introducing Amazon API Gateway Private Endpoints | AWS Compute Blog

Inter VPC internet access between lambdas

I am working on a project where my main lambda function is in a VPC in private subnet and some sister lambda functions in a different VPC which are in their own private subnets. How can I go about calling these sister lambdas's from the main lambda across VPC without giving internet access to each of them via a NAT gateway linking to a public subnet which has an internet gateway attached to it.
Other AWS services that my main lambda invokes are:
1. S3
2. Dynamodb
3. Autoscaling
4. ECS
5. RDS
This can be done, but there are some complex steps involved.
First of all, when you use aws-sdk, the calls are made through the internet. To avoid this situation and access the services within the AWS network, The AWS has introduced some private VPC endpoints. I have only used S3 and API gateway private endpoints to date. But there is more type of VPC endpoints.
This is how I would do today,
setup a private API gateway API to invoke lambda - The private API's are only accessible through a private VPC endpoint for API gateway.
create a private VPC endpoint for API gateway.
setup VPC peering between the VPCs
(from the sister lambda on other VPC) invoke the API through the VPC endpoints public DNS URL
The drawback of adding an API in front of the lambda is, the API has a hard timeout of 29 seconds.
hope this helps.