AWS: How should VPC Endpoint for EC2 be used? - amazon-web-services

Question
Why do we need VPC Endpoint for EC2?
How should it be used?
My understanding
VPC Endpoint is a service that enables services inside VPC to access outside VPC through AWS network.
For example... (See the screenshot, black thin lines)
We assume that we have a Lambda function in a private subnet and want the function to access S3.
Without VPC Endpoint: Lambda function --> NAT Gateway --> Internet Gateway -(via Internet)-> S3 bucket
With VPC Endpoint: Lambda function --> VPC Endpoint -(via AWS network)-> S3 bucket
Problem
I find VPC Endpoint for EC2 (service name: com.amazonaws.ap-southeast-1.ec2 in Singapore region). EC2 instances are always in a specific VPC, so now I do not understand why VPC Endpoint for EC2 is needed.
Does it work like: Lambda function --> VPC Endpoint -(via AWS network)-> EC2 instance (not in VPC)?
This is wrong if I understand the settings of EC2 instances correctly.

Related

Not able to connect to an S3 bucket using a gateway VPC endpoint

I’m using a gateway endpoint to connect to a S3 bucket from an EC2 instance in the default VPC. However, the connection isn't working.
I have checked the following configurations:
VPC DNS resolution to yes.
VPC route table table has access to Amazon S3 using the gateway VPC endpoint.
Security group outbound rules for EC2 permits all traffic on all ports.
VPC network ACL is permitting all traffic.
Bucket policy allows public access.
EC2 instance is attached to IAM role which is attached to S3FullAccess Policy.
Both bucket and EC2 are in us-east-2.
Error Details:
[ec2-user#ip-172-31-37-114 ~]$ aws s3 ls
Connect timeout on endpoint URL: "https://s3.amazonaws.com/"
[ec2-user#ip-172-31-37-114 ~]$
Can you please explain why it is not working without it --region us-east-2?
It was working because you were using s3.amazonaws.com endpoint which is for us-east-1 region. Gateway VPC endpoints are regional, and your endpoint was created for us-east-2. So you had to explicitly tell aws s3 to use us-east-2, rather then default us-east-1.

Distinguish between two VPC Endpoints calling my VPC Endpoint Service

I have a VPC Endpoint Service exposing a MicroService deployed in a private VPC. There are multiple VPC Endpoints created in other AWS accounts and private VPCs that connect to my VPC Endpoint Service.
Is there a way to tell from with in the MicroSevice which VPC Endpoint called it?
You can use VPC flow log to check traffic flowing through your VPC or subnet level. These VPC flow logs while creating them has to be configured to be send logs to either S3 or CloudWatch logs.

Why can my lambda function not access S3 and SQS when an EC2 instance in the same VPC can?

I have the following resources provisioned in AWS:
A VPC (the default VPC for my AWS region) with 3 subnets and an internet gateway
An EC2 instance in the VPC with an elastic IP attached, and a NodeJS application server running
A RDS instance in the VPC
A Lambda function configured to run in the VPC (because it needs to access RDS)
An S3 bucket
An SQS queue
The application server running on my EC2 instance is able to connect to S3 and SQS using the AWS SDK for NodeJS. All I had to do was specify the S3 bucket's name and SQS queue's url.
However, my lambda function was unable to do the same until I set up a VPC Gateway Endpoint for S3, and a VPC Interface Endpoint for SQS. This despite the lambda function having internet access - I was able to retrieve a file on the internet in a test run of the lambda function.
What was preventing the lambda function from accessing S3 and SQS until the VPC endpoints were created?
default VPC for my AWS region
The default VPC has all its subnets public. Lambda does not have internet access, even if you place it in such a subnet. Thus it can't access S3 nor anything else.
To enable internet access for your lambda, it must be placed in private subnet and use NAT to access the internet, as explained in AWS docs.
Alternatively, you have to create VPC interface endpoints for S3 and SQS. This way your lambda will use the VPC endpoints to access these services, rather then trying to do it using internet.

How to add a step to emr cluster within a vpc through lambda function which is inside a same vpc and private subnet?

when the lambda function is not within vpc then iam able to add a step to emr cluster but if the lambda function is residing inside vpc where emr cluster is present and same private VPC subnet also.
This time iam getting timeout error when iam trying to add a step in emr cluster using boto3 client module "add_job_flow_steps"
"errorMessage": "2020-05-14T02:48:46.771Z ad979ac2-ff26-476a-b301-23797caeeaa9 Task timed out after 123.10 seconds".
Do i need to add a VPC Endpoint for me to communicate between AWS services within same VPC Subnet or is there any other way which i could communicate?
when the lambda function is not within vpc then iam able to add a step to emr cluster
This works because lambda not in vpc, can access internet. Subsequently, you can connect to public endpoint of AWS services, such as EMR.
if the lambda function is residing inside vpc where emr cluster is present and same private VPC subnet also.
This does not work, because lambda in VPC does not have internet access:
If your function needs internet access, use NAT. Connecting a function to a public subnet does not give it internet access or a public IP address.
To enable your lambda access the EMR service you need to use either NAT gateway or VPC interface endpoint as shown in the following link:
Connect to Amazon EMR Using an Interface VPC Endpoint
Please not that lambda in VPC also requires modified execution role.

Putting both Lambda and RDS in default VPC behind an API Gateway?

I am trying to have an architecture with:
Route53 <-> API gateway <-> Lambda <-> RDS and DynamoDB.
I am confused about some networking aspects here!
From most of the documentation, what I understand is that Lambda is by default launched in default VPC and can access internet from there but no resources inside a "VPC". And this 2nd VPC (in quotes) refers to non-default VPCs in most discussions. But what is not clear is what if I placed the Lambda and RDS both in default VPC, lambda in a public subnet with --vpc-config info and RDS in a private subnet, will my Lambda have the internet connection?
Even when everything is in default subnet, should I put my lambda function in to a private subnet with Internet access through an Amazon VPC NAT gateway?
I know it is a theoretical question - documents are confusing me by not explicitly mentioning what cannot be done!
From most of the documentation, what I understand is that Lambda is by
default launched in default VPC and can access internet from there but
no resources inside a "VPC".
That is incorrect. By default Lambda is not launched in a VPC at all. Or if it is in a VPC it is in one that you cannot see because it doesn't exist in your AWS account.
what if I placed the Lambda and RDS both in default VPC, lambda in a
public subnet with --vpc-config info and RDS in a private subnet, will
my Lambda have the internet connection?
No, your Lambda function will not have internet access, even in a public subnet. This is because it is never assigned a public IP address. Once you place a Lambda function inside a VPC you have to have a NAT gateway in order to for the Lambda function to access anything outside the VPC.
Even when everything is in default subnet, should I put my lambda
function in to a private subnet with Internet access through an Amazon
VPC NAT gateway?
Yes, that is the correct way to provide a Lambda function with access to both a VPC and resources that exist outside the VPC.
Also note that DynamoDB (and the AWS API) does not run in your VPC. So if you place a Lambda function inside your VPC that needs to access DynamoDB, or anything else that is accessed via the AWS API, you will have to add a NAT gateway to the VPC.
Note that the "Default VPC" is the term for a the VPC that is setup for you when you first create your AWS account. You can see this VPC in your account in the VPC service console. Aside from it being created for you with default settings, you should just think of this as another VPC in your account. The Default VPC is not used by Lambda when you don't specify a VPC, and it is not used by other services like DynamoDB that exist outside your VPC network.