EC2 instances are not able to send emails - amazon-web-services

We have EC2 instances in private subnet and the traffic is routed via the NAT Gateway to reach Internet.
EC2 instances are not able to send emails to relaycloud.xyz.com
All the inbound and outbound traffic is allowed in NSG and NACLs.
Error Message says
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed connected host has failed to respond."
Security Group and NACL have all the traffic allowed both inbound and outbound.

From Remove port 25 restriction from your EC2 instance:
AWS blocks outbound traffic on port 25 (SMTP) of all EC2 instances and Lambda functions by default. If you want to send outbound traffic on port 25, you can request for this restriction to be removed.
It should work fine if you contact your SMTP server on a different port. Otherwise, you'll need to request for the restriction to be removed.

Related

AWS Security Group rules: How does ssh connection to EC2 still works when I have removed outbound rules

Here is my security group, inbound and outbound rules for the EC2 instance in AWS.
My understanding was that if I block every outbound traffic i will not be able to able to ssh into the system even if the inbound connection is allowed.
I did go through many documentation on it and did not really understand how the system is sending back data to ssh connection when the outbound rules are not allowing it.
Does this mean, a web server will still work without any outbound rules, provided ports for inbound, let's say 80, 443 are opened ?
The SSH connection is still working because security groups are stateful which means that if a connection/traffic can get inside, it can go outside. NACLs on the other hand are stateless which means that the challenge/test happens on entry and exit of traffic.

Setting ELB IP address for outbound UDP traffic

Setup: Multiple EC2 servers running behind an ELB. The "elastic" IP address associated with the ELB receives the UDP traffic and passes to an EC2 instance. An API adds items to an SQS, which the EC2 pull from the Queue and trigger outbound UDP messages.
Problem: The solution requires that the Inbound IP address and outbound IP address be the same. The traffic is UDP, and the outbound traffic is triggered by the EC2 pulling data off an SQS. Currently, the outbound traffic is the instances IP, and I need the traffic originating from the EC2 instance to have the same IP as the inbound traffic(to the ELB).
I don't think I can accomplish this with an ELB alone, and I'm looking for solutions that I can use to achieve this.
Any directions would be helpful.

Firewall : is inbound rule required for getting SYN-ACK from the server while outbound rule already there?

I have a VPC, within which I have a EC2 instance deployed in private subnet.
I have my NACL(Subnet firewall) ALLOW ALL on inbound and outbound.
I have my security group rule(Ec2 firewall) , It Blocks Public ip on Inbound rule and Allows to the public world on Outbound rule.
Now my EC2 instance initiates a connection to call provider(twilio)and initiates a call and it is successful.
From my ec2 application, it initiates a three way handshake, Since my outbound rule is ALLOW all it reaches the twilio through NAT.
Packet will be like (the Source Ip: is NAT ip and Random port number and Destination ip: is twilio ip and service listening port number)
The twilio then accepts the requests and provides the SYN-ACK response to the ec2 server.
Now the Packet will be (Source ip: twilio ip and random port number and Destination ip: is Ec2 instance's NAT ip and port number is (Ec2 Source Initiated Random port number). )
Now my question is, Eventhough the Inbound rule doesn't allow twilio ip address , how did the three way handshake is successful?
Got it after gooogling,
Firewall protection uses stateful inspection to track current connections. Stateful inspection tracks source and destination IP addresses, ports, applications, and other connection information. Before the client inspects the firewall rules, it makes the traffic flow decisions that are based on the connection information.
For example, if a firewall rule allows a computer to connect to a Web server, the firewall logs the connection information. When the server replies, the firewall discovers that a response from the Web server to the computer is expected. It permits the Web server traffic to flow to the initiating computer without inspecting the rule base. A rule must permit the initial outbound traffic before the firewall logs the connection.

Route only api traffic to other AWS EC2 instance

I am new to networking. And I am trying to route only traffic from one VM traffic to another VM. Therefore, I have done this.
I have two AWS EC2 instances as:
Application Server
Database Server
And they have their own security groups and I have allowed all traffic is permissible. Now I want to Database_server accepts only Application_server traffic not all public traffic. Database_server is MySQL which is running on 3306 port.
Suppose:
Application_server Public IP: 14.233.245.51
Database_server Public IP: 15.233.245.51
So I have allowed on port 3306 like this 14.233.245.51/32 for only Database_server but it did not work. It was before this 0.0.0.0/0 and ::/0.
How can I solve this?
First, the application server should communicate with the database server via private IP address. This will keep all traffic within the VPC and will enable security groups to work correctly.
Second, configure the security groups:
App-SG should be associated with the application server and permit incoming traffic on the appropriate ports for the application (eg 80, 443)
DB-SG should be associated with the database server and permit incoming traffic on port 3306 from App-SG
That is, DG-SG permits inbound traffic from App-SG by referring to the ID of App-SG. There is no need to specify an IP address. The security groups will automatically recognize the traffic and permit the App server to send traffic to the DB server. Return traffic will also be permitted because security groups are stateful.
You MUST communicate with the database server via private IP address for this to work.

AWS security group and Django Email SMTP TLS port 587

I am running Django on AWS Lambda with Zappa. I have AWS Lambda set in a VPC and I have an EC2 NAT Instance in the same VPC. The NAT Instance security group takes inbound connections from by Lambda subnets (through HTTPS and HTTP) and outbounds to the public internet. This way, my Django app can communicate with the public internet if it needs to send Push Notifications to Apple Push Notification services.
One thing that does not work is sending emails from Django. It works in development but not on AWS Lambda. I send emails with SMTP using port 587, and I use TLS.
I figured I had to add Inbound rules to my NAT Instance security group. The problem is I can't add inbound SMTP with port 587.
Here are screenshots to show this.
When I select SMTP, the port number is fixed at 25 and greyed out so I can't modify the port to 587.
I looked at other options in the list but none of them set the port number to 587.
I even tried 'Custom TCP Rule' and setting the port to 587 but that does not work. Emails are not sending!