Host java Application on AWS - java

I have 2 projects.
Spring Restful Webservice
AngularJS
Database is MySQL.
i am new to AWS, I want to host my application using AWS. So i want to deploy these projects on AWS.
When i create MySQL database on AWS, then what will be the DB_URL then i can use it on my java application.
When i deploy the WAR file on AWS, then what will be the URL then i can use it on my AngularJS application

Database: use RDS Mysql. After create, go detail of RDS instance you will get RDS Endpoint with this pattern: RDS_INSTANCE_NAME.XXXXXXXXXXX.ap-northeast-1.rds.amazonaws.com and default port 3306. You can access this RDS with that endpoint and user/pass as the local database (Import/export, Create schema,...)
About the web layer, you can create 2 EC2 instances: 1 for Web-service, 1 for AngrulaJS or create 1 EC2 instance and deploy both of them into that EC2 instance.
You need SSH key to access via ssh. Then install your web container into EC2 instances and deploy as local server
AngrulaJS web: There are many ways to public your app:
you can use Route53 to create A Record point to public IP of EC2 instance (Containt AngrularJS app)
Use Internet-facing ELB point to EC2 instance.
Use public IP that associated to EC2 instance.
I found a clearly architect design in this, you can reference it https://creately.com/diagram/example/h4gr4x8d4/3-Tier%20Architecture

When you deploy the application in EC2.
You have Public DNS (IPv4) value for Ec2 ec2-xx-xx-xx-xx.compute-1.amazonaws.com. You will use this endpoint for WAR file.
If you do not assign Elastic ip for the EC2 Instance when you stop and start instance the ip address changed for Instance.
Based on your requirements you can leverage other services like Load Balancer and Route53.

Related

Can't connect to a database on Amazon RDS from Amazon EC2 ubuntu 16.04 image

I have hosted a play framework web application on Amazon EC2 instance. The application is not able to connect to a mysql database located on the Amazon RDS instance. When I run the play framework web application on my local computer it successfully connects to the database on the Amazon RDS instance. I checked the database connection credentials in the production conf file many times. Looks correct.
When I do
mysql -h********.rds.amazonaws.com -p****** -u****** db_name
from the local computer it quickly connects. But when I try same connection to RDS from EC2 it throws:
ERROR 2003 (HY000): Can't connect to MySQL server on ********.rds.amazonaws.com
Can anyone kindly help me with this or suggest anything that I may be possibly missing.
Is RDS in VPC?
Yes, Check if EC2 is also in VPC or not.
No, Check security group that EC2 is in, allows the communication over database port
For more info and scenarios, head here

Java web application cannot connect to AWS RDS DB Instance

I created DB Instance (MySQL) with Publicly Accessible option. In DB Security group, I opened MySQL Port for EC2 instance security group(Web server). In EC2 security group it allows ssh, web server ports. I can able to connect DB Instance from EC2 instance. I deployed web app on web server and it can't able to connect with RDS Instance.
I am getting the exception in my local web server:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not
get JDBC Connection; nested exception is java.sql.SQLException: Server
connection failure during transaction. Due to underlying exception:
'java.net.UnknownHostException:
"kbawstry2.cdhtaamn5ynq.us-east-1.rds.amazonaws.com"'.
EC2 Security Group
DBInstance Security Group
Using MySQL WorkBench, I can connect to RDS DB Instance using TCP/IP SSH Tunnelling option. But in Java Programming How can I connect with database?
Thanks in advance.
Finally I solved the issue. The problem is in application the way I mentioned connection string. I rectified and tested application in localhost then updated to tomcat server in EC2 instance. Another change in DB Security Group. I replaced source ip as EC2 instance's private IP address as both RDS and EC2 Instance are in same VPC.
Now It is working fine.

How configure aws-ec2 Instance to run playframework 1.2.7 application

I have deployed our playframework 1.2.7 web application to aws-ec2 ubuntu instance. The started the application on port 8081 since 80 or 8080 complains about not able to bind to those ports. How can I configure the ubuntu instance either througth the aws security group or on ubuntu itself so that I wouldn't have to add the port 8081 to the end of the public url or the public ip provided by aws.
ie I don't want do this:
example.com:8081 / ip4:8081
But I just want to use:
example.com / ip4
to access the application.
Please I need help on this.
The problem is that on Ubuntu ports < 1024 are privileged. This means that normal users can do nothing with it. To start play on port 80 you could simply start it as root user. Anyway it's not a best practice to start webserver as root due to possible security issues.
I'd suggest to start it on whatever non-privileged port you want, as normal user, and make use of an Elastic Load Balancer (ELB) to redirect all inbound traffic on port 80(or 443 for instance) to your play port. You can accomplish this simply using AWS web interface, when creating an ELB
So users will reach your play instance calling ELB on port 80 using Amazon auto-assigned dns name.
Example flow:
User browser --> http://your-elb-dns-name.com --> your_play_server_ip:8081
Just make sure that the Security Group associated to your play server instance will accept inbound traffic on 8081 from your ELB (you can identify your ELB using the amazon id assigned during its creation)
Another great advantage of using this ELB approach is that you can use it as reverse proxy to hide your ec2-instance(s) ip(s) to the internet. In fact, if you use ELB you could also avoid assignin a public ip to your ec2 instance during creation. ELB doesn't need to know a public ip beacuse it will have access to the Virtual Private Cloud (VPC) in which your ec2 instance was started
Another possible approach, if you don't want to use ELB, is to install NGINx or Apache on your ec2 instance to act as reverse proxy, but I think you should make use of Amazon web services to accomplish that. You may want to use an internal NGINX or Apache reverse proxy if you need to hide a particular resource of your play server to the internet.
https://aws.amazon.com/it/elasticloadbalancing/

Communication between Android and EC2 with RDS

I have an EC2 instance on AWS configured with Apache tomcat 8 and java.
Following some tutorials from here, I can create one .war of my Web Service in eclipse and put it inside webapps folder of tomcat in my EC2 instance.
I am able to access this services from the IP address of EC2 with the specific port and .war file name.
For example:
IPADDRESS:8080/MyApplication.
And everything seems to work fine, I can open the services and see the wsdl files. My Web Service (in .war file) is configured for make a connection with a RDS instance from amazon.
The connection itself is ok, I tested in localhost a direct connection with RDS from Android.
But, when I try to connect with services that I put in EC2 (from there
the connection with my RDS is created) from Android, no data returns and I get a Null Pointer.
The link that I used to make a connection with EC2 from Android is the Public IP of EC2 and the port (8080), followed by the specific path of wsdl file.
For example:
http://99.999.999.99:8080/MyApplication/services/SomeServiceName?wsdl
I am using the KSOAP library to consume web service on Android side and I
have no idea of the reason that I don't get data from EC2 instance.
All methods in the web service and the comsumption in Android works fine, I tested all in local host.
I tested all my methods of web service using SOAP UI, and all of them is working
For test the RDS instance that contains my DB, I run the web service in localhost with tomcat 8 and the communication Android -> My PC (localhost) -> RDS instance (My DB) is working.
The problem are when I try to change the communication line from:
*Android -> My PC (localhost) -> RDS instance (My DB)*
TO:
*Android -> EC2 instance -> RDS instance (My DB)*
Again, in EC2 I make the deploy of my web service.
I use the same security group for RDS and EC2:
Inbound rules:
http://i.stack.imgur.com/RUdyl.png
Outbound rules:
http://i.stack.imgur.com/W4gLc.png
I finally solved the problem.
I did the following:
I connected to my ECB2 instance and open the tomcat 8 log, so I saw that the error was ClassNotFoundException, related to Postgres (my engine DB).
I opened my web service eclipse project and saw this (stupid) error:
The .jar postgres driver was in that lib folder:
Instead of that folder:
So, basically, when I did the deploy of my Web service in EC2 instance the postgres driver does not go together.
Thanks for the help, I hope that helps someone else.

EC2 to RDS connection fails but localhost to RDS works

I am attempting to query a MySQL database from a java program on EC2. My database is stored on Amazon Web Services (AWS) RDS.
When I test my java program from my local tomcat server, I successfully query the RDS database. However, when I query my RDS database from my EC2 instance, I fail to connect.
In my attempts to solve this problem, I investigated my security settings. I've set up an RDS security group associated with my EC2 Security group that is associated with my instance. In my EC2 security group I've enabled all types of inbound traffic to all ports.
Other answers to similar questions suggest that setting up the RDS security groups to accept connections from the EC2 instance is the resolution to this problem. However, I have seemingly done this, yet cannot connect from my EC2 instance (but can from my local machine).
Am I overlooking something?
My guess would be, your EC2 instance is probably in different VPC , and you have made the RDS db instance private or you did not launch it in the specific vpc where your EC2 instance is located in.

Categories