AWS execute-api endpoint request timeout / dial tcp i/o timeout - amazon-web-services

I want to access a public Rest Api gateway from a AWS Lambda within a VPC.
I already created an execute-api endpoint and added the 443 port to the security group. I also disabled private DNS Names, but I am still getting a "Endpoint request timed out"/ dial tcp 52.28..:443: i/o timeout error.
I also tested the API I want to access with postman, which works as intended.
What am I missing.

By default Lambda in a VPC does not have public internet access.
One option you have is to setup up a NAT gateway in the subnet Lambda is deployed to give it internet access, hence access to public REST API. More here -> Internet access for lambda functions
AWS Lambda uses the VPC information you provide to set up ENIs that allow your Lambda function to access VPC resources. Each ENI is assigned a private IP address from the IP address range within the Subnets you specify, but is not assigned any public IP addresses. Therefore, if your Lambda function requires Internet access (for example, to access AWS services that don't have VPC endpoints ), you can configure a NAT instance inside your VPC or you can use the Amazon VPC NAT gateway. For more information, see NAT Gateways in the Amazon VPC User Guide. You cannot use an Internet gateway attached to your VPC, since that requires the ENI to have public IP addresses.
Or you can have a look here -> Why can't I connect to my public API from an API Gateway VPC endpoint. It suggests this
Use the curl command line tool to test your private API. In your curl command, include the base URL used to invoke the API, as well as a Host header or x-apigw-api-id header. For more information, see Invoking Your Private API Using Endpoint-Specific Public DNS Hostnames.
The base URL to invoke the API includes the DNS name and stage name. It looks like this:
https://vpceId.execute-api.awsRegion.vpce.amazonaws.com/stageName
Note: Replace vpceId with the VPC endpoint ID you copied. Replace awsRegion with your private API's AWS Region (for example, us-east-1). Replace stageName with the name of the stage to which your API is deployed.
The Host header looks like this:
Host:apiId.execute-api.awsRegion.amazonaws.com
Note: Replace apiId with the API ID you copied. Replace awsRegion with your private API's AWS Region (for example, us-east-1).
The x-apigw-api-id header looks like this:
x-apigw-api-id:apiId
Note: Replace apiId with the API ID you copied.
If you set up everything correctly, you get a 200 response code.

try accessing the API using <vpce-id>.execute-api.<region>.vpce.amazonaws.com and passing <api_id>.execute-api.<region>.amazonaws.com in the Host header

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 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

Timeout calling PRIVATE API Gateway from another AWS account

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.

AWS Apigateway does have any static IP address

I have an aws api gateway which has a custom domain.
I have to access one of client's api from AWS api gateway.
Those api's will be accessible based on IP address.
If I want to access those api's from aws, I need to know the Ip address of my AWS apigateway.
But I am not sure where I can get this.
Any possibilities to get the static IP address of AWS apigateway?
Unfortunately the API Gateway doesn't support this scenario of invoking an IP-whitelisted API directly. Basically any AWS instance can be used to make the API call and there's many IPs that AWS is using for this.
There are some ways around this, depending on your situation;
If the server you are reaching is within your own VPC, you could create a VPC link (with NLB) and circumvent the IP-whitelist issue. This solution only works for resources within your control.
If the server is external and is expecting a static IP, your best solution is to make your call from within a VPC. To make sure you have a static public IP you can use an elastic IP and a NAT Gateway in your VPC (more info here).
To invoke the external API triggered from the API Gateway you can use a Lambda but because of the VPC a cold start will be quite slow, >10s. To make sure the API will be responding fast you can use an EC2 instance or ECS service on Fargate.
After you clarified your requirements in the comments above it appears that you need your AWS Lambda function, which is being triggered by API Gateway, to appear to a third party as if it has a static outgoing IP address.
The solution to this is to configure your Lambda function to run in your VPC, in a private subnet of your VPC that has a route to a NAT Gateway. Then all outgoing connections from the Lambda function which access resources outside your VPC will use the NAT Gateway's static IP address.

KMS decrypt works locally but not from within EC2 instance

I am trying to decrypt a file that is stored inside an S3 bucket using KMS. Within an EC2 instance, I am able to retrieve this file from S3, but when I try to decrypt it using KMS, I get the following error:
HTTPSConnectionPool(host='kms.us-east-1.amazonaws.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(, 'Connection to kms.us-east-1.amazonaws.com timed out. (connect timeout=60)'))
I am however able to decrypt this file successfully from my local machine. Does anyone know why this may be?
If additional info is required, I would be happy to provide it :)
When you enable VPC for your Lambda AWS shows you following message -
When you enable VPC, your Lambda function will lose default internet access. If you require external internet access for your function, ensure that your security group allows outbound connections and that your VPC has a NAT gateway.
So make sure your lambda has access to internet with correct security groups and NAT gateway.
Also note having IG for your subnet is not sufficient. You need to have a NAT instance or gateway and a route from your subnet to that gateway in route table for lambda to have internet access associated to a custom VPC.
AWS Lambda uses the VPC information you provide to set up ENIs that allow your Lambda function to access VPC resources. Each ENI is assigned a private IP address from the IP address range within the Subnets you specify, but is not assigned any public IP addresses. Therefore, if your Lambda function requires Internet access (for example, to access AWS services that don't have VPC endpoints, such as Kinesis), you can configure a NAT instance inside your VPC or you can use the Amazon VPC NAT gateway. You cannot use an Internet gateway attached to your VPC, since that requires the ENI to have public IP addresses.
Important :
If your Lambda function needs Internet access, do not attach it to a public subnet or to a private subnet without Internet access. Instead, attach it only to private subnets with Internet access through a NAT instance or an Amazon VPC NAT gateway.
Source : http://docs.aws.amazon.com/lambda/latest/dg/vpc.html
One case can be that you are connecting to s3 bucket using VPC endpoint and not have an internet connection.
If this is the case then you have to enable internet connectivity either via NAT or putting your instance in a public subnet.