How to connect AWS ECS Service to database in EC2 instance? - amazon-web-services

I have a SQL Server database running on Windows Server EC2 instance. I also have Web API (ASP.NET Core WebAPI) deployed as a Service in ECS cluster (Fargate launch type).
What connection string should I use to access this database from my web API?
Right now I'm trying:
data source=NAME_OF_THE_EC2_INSTANCE;initial
catalog=DATABASE_NAME;User
Id=USER_NAME;Password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=10;
But it doesn't work. The error returned suggests that the app doesn't even see the database at all.

It seems you'll need to use a NAT instance/Gateway
This will enable connectivity between your Fargate instance and EC2 instance where DB is installed.
Another source and also the official documentation
"...Container instances need external network access to communicate with the Amazon ECS service endpoint, so if your container instances are running in a private VPC, they need a network address translation (NAT) instance to provide this access. For more information, see NAT Instances in the Amazon VPC User Guide."

Related

Using Session Manager to connect RDS without having EC2 instance

When I go through the documents, using session manager we can connect instance in private subnet without having bastion host itself [direct port forwarding from local to private ec2].
But in RDS case, even though we are making connection using session manager we need a EC2 instance in between local and private RDS.
Could you anyone explain me why it is like that? please share some document that explains that as well.
AWS Systems Manager Session Manager allows you to connect to an instance in a Private Subnet because the instance is actually running an 'SSM Agent'. This piece of code creates an outbound connection to the AWS Systems Manager service.
Then, when you request a connection to the instance, your computer connects to the AWS Systems Manager service, which forwards the request to the agent on the instance. The AWS Systems Manager service is effectively acting as a Bastion for your connection.
AWS Systems Manager Session Manager cannot provide a connection to an Amazon RDS server because there is no ability to 'login' to an Amazon RDS server. Given that your RDS server is running in a Private Subnet, it is therefore necessary to port-forward via an EC2 instance in the same VPC as the RDS server. This can be done via a traditional Bastion EC2 instance in a Public Subnet, or via an EC2 instance in a Private Subnet by taking advantage of the Port Forwarding capabilities of AWS Systems Manager Session Manager.
I have got answered the same question in the AWS repost by #Uwe K. Please refer below.
SSM allows many more functions - and changes! - to an instance then just connecting to it. Having full SSM functionality on an RDS instance thus would undermine the Shared Responsibility Model we use for RDS (you could also say: it would violate the "Black Box" principle of RDS). Therefore, you need an intermediary instance that forwards the TCP Port exposed by RDS to your local machine.
Further reading:
The RDS-specific Shared Responsibility Model is explained here https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.html
a general overview of the Shared responsibility model https://aws.amazon.com/compliance/shared-responsibility-model/
In order to connect to any EC2 instance with AWS systems manager, the SSM agent must be installed on that machine and the appropriate permissions need to be set up for the instance.
At the moment, AWS does not support this to RDS directly. In order for them to support such a setup, they'd probably need to install the agent on all RDS instances which generates quite some overhead and who knows what else the complexities of such a setup would have.
So at the present moment the most effective way to connect is setting up a tunnel via an EC2 instance.

How to update a private server?

I have a Django web application, running on AWS Elastic Beanstalk, inside of an EC2 linux instance.
Only the load balancer is accessible to the internet, for security reasons. Deployment to the EC2 app server is facilitated by Elastic Beanstalk, but new packages added to the repository can't be installed without an internet connection.
What is the standard way to automatically update without an internet connection?
Should I just create another EC2 and use it as a proxy server? How can I configure this?
Are there any references on this topic?
Typically NAT gateway is used for that:
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.
There are also NAT instances which also can be used for the same purpose. However NAT gateway is a managed and scalable service and is usually preferred over NAT instances.

How to develop a AWS Web App that uses AWS RDS locally?

Before moving to Amazon Web Services, I was using Google Cloud Platform to develop my aplication, CloudSQL to be specific, and GCP have something called Cloud SQL Proxy that allows me to connect to my CloudSQL instance using my computer, instead of having to deploy my code to the server and then test it. How can I make the same thing using AWS?
I have a python environment on Elastic Beanstalk, that uses Amazon RDS.
AWS is deny be default so you cannot access an RDS instance outside of the VPC that your application is running in. With that being said... you can connect to the RDS instance via a VPN that can be stood up in EC2 that has rules open to the RDS instance. This would allow you to connect to the VPN on whatever developer machine and then access the RDS instance as if your dev box was in the VPC. This is my preferred method because it is more secure. Only those with access to the VPN have access to the RDS instance. This has worked well for me in a production sense.
The VPN provider that I use is https://aws.amazon.com/marketplace/pp/OpenVPN-Inc-OpenVPN-Access-Server/B00MI40CAE
Alternatively you could open up a hole in your VPC to the RDS instance and make it publicly available. I don't recommend this however because it will leave your RDS instance open to attack as it is publicly exposed.
You can expose your AWS RDS to the internet by proper VPC setting, I did it before.
But it has some risks
So usually you can use those ways to figure it out:
Create a local database server and restore snapshot from your AWS RDS
or use VPN to connect to your private subnet which hold your RDS
A couple people have suggested putting your RDS instance in a public subnet, and allowing access from the internet.
This is generally considered to be a bad idea, and should be the last resort.
So you have a couple of options for getting access to RDS in a private subnet.
The first option is to set up networking between your local network and your AWS VPC. You can do this with Direct Connect, or with a point-point VPN. But based on your question, this isn't something you feel comfortable with.
The second option is to set up a bastion server in the public subnet, and use ssh port forwarding to get local access to the RDS over the SSH tunnel.
You don't say if you on linux or Windows, but this can be accomplished on either OS.
What I did to solve was:
Go to Elastic Beanstalk console
Chose you aplication
Go to Configurations
Click on the endpoint of your database in Databases
Click on the identifier of your DB Instance
In security group rules click in the security groups
Click in the inbound tab
Click edit
Change type to All Traffic and source to Anywhere
Save
This way you can expose the RDS connected to your Elastic Beanstalk aplication to the internet, which is not recommended as people sugested, but it is what I was looking for.

Executing Spring boot jar in Amazon EC2 instance

I am new to amazon EC2 instances. I am able to deploy my jar in EC2 linux instance and execute it but the jar is running in Private DNS mode. I am not able to access the http and https urls from my local machine. I already enabled all traffic to access the api (in Security Group). Do I miss anything to get the api access over the internet? How can I run my api in public DNS mode?

Working with External server inside Amazon VPC

I have an external server hosted and managed on my side. There's another server I use, which is based on Amazon EC2. I would like to get them to work together using Amazon VPC (Virtual Private Cloud).
Is that possible? If so, how can it be done?
If you intend to migrate your EC2 instance to a VPC, then no, you can not. You will require to create a new instance inside your VPC and replicate the setup and data. The fastest way to accomplish this is creating an AMI from your current EC2 instance and deploy it as a new instance inside VPC.
Furthermore, if your VPC instance is deployed in a private subnet, you will have to deploy a public NAT instance, configure the NAT instance DST-NAT rules to map external ports to your app instance, setup the VPC routing tables properly and adjust the security groups in order to allow forwarding traffic.
Now, about the external server, if you are going to migrate it to VPC, I would recommend you to directly create a new instance with a prebuilt standard Amazon AMI and then manully replicate configuration and data. There are some VM migration tools but my experience with such tools has not been friendly so...