We are facing two problems with EC2 and RDS instances:
We are loosing existing data, when the existing EC2 instance is terminated and new EC2 instance is created automatically. Once we create Elastic Beanstalk and upload our application files, its creating one EC2 instance and running. And then next if we upload any files to the existing EC2 instance and in case its going down/terminated, one more new instance is getting created and loosing the data from the old EC2 instance.
If we add the EC2 instance ip address to the RDS inbound security group to restrict others from accessing RDS instance from other ips, we are facing problem to access RDS, when the old EC2 instance is terminated and new EC2 instance is created or any other new ec2 instance is created based on load.
Kindly suggest some solution for the above issues.
Any data you store in EC2 is going to get lost if/when the instance is terminated. You need to design your solution so that it is not dependent on ephemeral subsystems.
Exactly how, will depend on what you are doing, but for example, if you are hosting a web server and the user can upload images, better to store them on S3 instead of directly in the EC2 instance. Data should be stored in a database also not on the EC2 instance, so in an RDS instance or DynamoDB both of which will survive an EC2 termination and restart.
For the access problem, you create a security group (call it sg-1234 for example), and then make each of your new instances created with that security group when they start.
Also create an RDS security group (call if db-1234 for example) and in the DB-1234 security group create a rule that allow Sg-1234 as a source.