Internet access via VPC endpoint - amazon-web-services

Is there anyone who can explain me that situation below ?
I connected to Ec2 intance in private subnet via Bastion Host.Then I created a vpc gateway endpoint to reach s3. I assigned the required role to ec2 and the connection to s3 is done. My question is, how can Ec2 instance in private subnet download something from the internet?

A VPC endpoint is used to access a certain service, in this case S3, over the AWS network instead of over the internet.
If you want your instance to be able to talk to the internet, you'll need to configure a NAT gateway in a public subnet, and you'll need to configure the route table of the private subnet to forward traffic to the NAT gateway.

A simple solution would be:
Put everything in a Public Subnet
Assign a Public IP address to the Amazon EC2 instance
Use Security Groups to secure access on the Amazon EC2 instance (that is, only allow Inbound connections from the Bastion)
The effective result is the same as using a Private Subnet, but the EC2 instance will be able to establish Outbound connections to the Internet (but not Inbound, so it remains secure).

Related

Multiple EC2 instance in public subnet should have same IP address for outgoing traffic

I have an application hosted on an EC2 instance in public subnet. To integrate this app with a partner we need to whitelist public IP address of the EC2 instance on partner's firewall.
I want to configure auto-scaling of the application in such a way that outgoing traffic for my application's EC2 instances should be from same single IP address that only needs to be whitelisted on partner's end.
For a private subnet, I know that a NAT Gateway is a solution.
But for instances in a Public Subnet, how this can be achieved? any solution/suggestions will be highly appreciated.
All traffic from the instances will need to be redirected to send via a single resource, such as a proxy or a Gateway.
The simplest solution is as you stated -- configure the subnet to route all Internet-bound traffic to a NAT Gateway or a NAT Instance. All traffic from that instance will then come from a single IP address.
However, configuring the subnet in this way will mean that it is no longer a "public subnet", since a public subnet has Internet-bound traffic sent through an Internet Gateway rather than a NAT. So, it's actually the same as using a private subnet as you suggested in your question.
The only way to do it in a Public Subnet would be to specifically configure your apps to send requests via a proxy server, which would act in a similar way to a NAT Gateway/Instance.

How can a lambda inside a private subnet access EC2 in a public subnet?

I've created an EC2 instance inside a public subnet (so that I can access it from my home network) and I have created some Lambda's inside the private subnets of my VPC.
My 1st lambda can freely access the internet (through a NAT Gateway) and do its job. Which is to fetch a file from the internet and upload it to S3, once per day.
My 2nd lambda is supposed to retrieve this file from S3 (which it does without issue) read the file & then upload the data to MySQL running on the EC2 instance. It is unable to connect to the Database (using either the EC2's public or private IP's) and the Cloudwatch logs show that the session times out, making me think this is a networking issue.
I have a 3rd lambda that will also need to interact with the EC2/DB instance.
My security group allows for all incoming traffic from my home network IP, the NAT gateway & the VPC IP range. All outbound traffic is allowed.
I appreciate its not usual to have an EC2/DB set up this way, in a public subnet, but its my preference to interact it with it this way from home using Sequel Pro.
However, is my set up even possible? Eg can my private subnet lambdas interact with a public subnet ec2 instance? if so, does anybody have any ideas how I can make this happen?
It appears that your situation is:
An Amazon EC2 instance running in a public subnet, with MySQL
The EC2 instance has a Security Group allowing all incoming traffic from your home network IP, the NAT gateway and the VPC IP range
An AWS Lambda function connected to a private subnet of the same VPC
A NAT Gateway allowing private subnets to connect to the Internet
The Lambda function is unable to connect with the MySQL database running on the EC2 instance
The normal security configuration for this scenario would be:
A Security Group on the Lambda function (Lambda-SG) that allows all Outbound access (no Inbound required)
A Security Group on the EC2 instance (EC2-SG) that allows inbound access from Lambda-SG on port 3306, plus whatever inbound permissions you want for accessing your instance via SSH, etc.
Given that your Security Group includes "the VPC IP range", this should be sufficient to permit inbound access.
The Lambda function should reference the EC2 instance via its private IP address to keep traffic within the VPC. By default, all subnets within a VPC can communicate with each other unless the Network ACLs have been modified (and they should generally be left at default values).
This means that the only remaining explanation would be that the MySQL database is not accepting traffic from the VPC IP range. (I'm not a MySQL person, but I know that PostgreSQL requires incoming IP ranges to be defined, so this might be true for MySQL too.)
To diagnose what might be happening in your network, I recommend:
Launch another Amazon EC2 instance in the public subnet
Connect to the new instance and try to connect to the MySQL database via the private IP address
If that works, repeat the process but from an EC2 instance in the private subnet. To use this you will need to connect to the 'public' EC2 instance, and from there connect to the 'private' EC2 instance. Then, try and connect to MySQL from that private instance.
These steps will progressively let you identify where the network problem might lie. Let us know what you find!

How to use ftp client in AWS ec2

Anyone here that have a ec2 instance that is on a private subnet in AWS that is able to connect to outside FTP server? It seems that my ec2 instance cannot connect to 3rd party ftp servers without moving to public subnet and using elastic/public ip.
Private subnet by default have no connectivity to outside world or internet. But there is a resource called NAT Gateway that you can configure in your VPC to provide an outgoing internet connectivity from your private subnets.
You can use a network address translation (NAT) gateway to enable
instances in a private subnet to connect to the internet or other AWS
services, but prevent the internet from initiating a connection with
those instances.
You can follow the link here to setup the NAT gateway.
https://aws.amazon.com/premiumsupport/knowledge-center/nat-gateway-vpc-private-subnet/
Private subnets do not have internet connectivity by default. You need to create a nat gateway on the public subnet. Nat gateways are bit expensive. You can also create a nat instance in that case.

Internet access for EC2 private instance

We would like to know if its possible to make our on-premise firewall the NAT instance for the private instance in AWS VPC for outbound internet access (to get updates for example)?
We currently have AWS VPC scenario 4 implemented.
Thanks!!
For Scenario 4: VPC with a Private Subnet Only and Hardware VPN Access, all outbound (non-VPC) traffic has to go through the customer gateway to on-prem. So if you want to access internet, the traffic goes through on-prem and then outbound from on-prem/corporate network.
In short: From AWS VPC, if you want to access any AWS endpoint, you have to go through the corporate/customer network. You have to allow outbound in your on-prem firewall. There is no other option.
If you really want to access internet through NAT, you should have created Scenario 3: VPC with Public and Private Subnets and Hardware VPN Access

The ec2 instance can't access internet in a public subnet without a elastic ip address?

I working on aws. I created a public subnet which has a network ACL allows all net traffic, and associated with a internet gateway in the route table.
And then I create a ec2 instance without elastic ip and a ec2 instance with elastic ip in it, and the security group also allows all traffic.
I ssh to the instance which has elastic ip. It works well when I run yum update and curl www.baidu.com and so on. But I can't access internet when I ssh to the ec2 instance which has no elastic ip. anyone knows that is why?
For accessing internet from EC2 instance in public subnet using Internet Gateway, the instance needs to have public IP address. Either one can configure the instance to have public IP address or attach EIP.
Reference: http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Internet_Gateway.html
If you do not want to attach public IP address for instances with Internet access (consider private subnets), NAT instance and NAT gateway can help.
Reference: http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-nat.html
As well as being in a subnet that has a route to an Internet gateway, an instance must have a public IP address to communicate with the outside world (this is distinct from an elastic IP).
You can specify this when launching an instance, if not there is a per subnet setting that controls this. The per subnet setting defaults to false other than default subnets in your default VPC. There are more details in the aws docs.
The problem lies with the gateway connected to the subnet you are using. To check the gateway you are using:
Go to the subnet you are using
Click on the Route table
Under Routes -> Destination look for 0.0.0.0/0 and check the gateway you have for it under Target
Here's how the gateways behave:
Internet Gateway (IGW) allows instances with public IPs to access the internet.
NAT Gateway (NGW) allows instances with no public IPs to access the internet.
This is a good article to help you understand and solve your problem:
https://medium.com/awesome-cloud/aws-vpc-difference-between-internet-gateway-and-nat-gateway-c9177e710af6