Giving Jenkins access to AWS instance behind a VPN - amazon-web-services

I need to trigger a Jenkins build that will access a Chef server installed on an EC2 instance which is behind a VPN.
Potentially the security group could be relaxed to allow the Jenkins server's IP, but I hope to avoid that.
Is there a way to connect Jenkins to a VPN while executing a build?
A Google search of this yielded this plugin on github but in its Jenkins page there's no real information nor is it downloadable.

One way to do this is to have a well-secured bastion server acting as an interface into the VPC and having access to tunnel (with SSH, VPN or something else) only the kind of requests you want to allow in.

You should create a NAT gateway in the account where Jenkins is installed. NAT Gateway will have Elastic IP and all the resources in this VPC (including Jenkins) will reach to Internet using this EIP. Then you configure your target security group to allow connections from this Elastic IP.

create OpenVPN ec2 instance from aws marketplace and create ec2 instance for ur jenkin and add the security group of ur openvpn instance to jenkin instance .. so that once u connect your openvpn then only u access ur jenkin instance...
Note: Openvpn instance is not free of cost

Related

AWS: can't connect to Amazon Linux EC2 instance

I'm working with AWS, I have an EC2 instance (Amazon Linux) but I can't connect to it, I've checked all VPC parameters and they are enabled as well as the instance, but when I try to connect it using EC2 Instance Connect I get this message:
I'm using the default user account, also I generated a key pair however I'm getting this other message:
Also, session manager can't connect.
So my question is: what settings do I need to update or check in order to connect to my EC2 instance?
Thanks a lot for your comments.
There are multiple ways to login to an Amazon EC2 instance.
SSH
Your screenshot shows that you are wanting to login via SSH, but it is saying that no Keypair was selected when the instance was launched. Therefore, this option is not available for you.
EC2 Instance Connect
If you ware wanting to login to the Amazon EC2 instance using EC2 Instance Connect and you are experiencing connectivity problems, then make sure that your Security Group permits Inbound access on port 22 from the IP address range of the EC2 Instance Connect service (not your own IP address).
This is because the EC2 Instance Connect client on your computer connects to AWS on port 443 (as a web connection), and then the traffic goes from the EC2 Instance Connect service to the EC2 instance as a normal SSH connection on port 22. Therefore, the Security Group needs to permit Inbound connections on port 22 from the IP address range of the EC2 Instance Connect service (or you can be lazy and just select 0.0.0.0/0, but that is a lower level of security).
You can find the IP address ranges for AWS services at: AWS IP address ranges - AWS General Reference
Please note that your EC2 instance must be in a public subnet and you must connect via a public IP address.
AWS Systems Manager Session Manager
The Session Manager connects in a totally different way, without using SSH. It requires an Agent to be installed on the EC2 instance (and it is there by default if you launched from an Amazon Linux AMI). This Agent then creates an Outbound connection to AWS, so it does not require any Inbound security rules (but it does require the default "Allow All" Outbound rule).
Session Manager has the additional benefit that it allows you to connect to EC2 instances that are in private subnets, as long as the EC2 instance can access the Internet via a NAT Gateway or if the VPC has a VPC endpoint for Systems Manager.

Elastic Beanstalk & EC2 Instance Connect: Can't connect

I created an Elastic Beanstalk environment from Visual Studio and need to login to service the underlying ec2 vm.
I don't have an credentials for the server, so I wanted to use EC2 Instance Connect.
When I click connect, I get an error message:
We were unable to connect to your instance. Make sure that your instance’s network settings are configured correctly for EC2 Instance Connect. For more information, see Task 1: Configure network access to an instance.
Following the link, I found the instructions:
Ensure that the security group associated with your instance allows inbound SSH traffic on port 22 from your IP address or from your network.
(Amazon EC2 console browser-based client) We recommend that your instance allows inbound SSH traffic from the recommended IP block published for the service. Use the EC2_INSTANCE_CONNECT filter for the service parameter to get the IP address ranges in the EC2 Instance Connect subset.
How do I connect to the Elastic Beanstalk underlying EC2 via EC2 Instance Connect?
What I've tried:
I created a new security group that contains my client IP address, but that didn't work. Which makes sense, as it's the EC2 Instance Connect app running in the Console making the SSH connection, not my local machine.
Also looked at the the ip ranges json file (https://ip-ranges.amazonaws.com/ip-ranges.json), but not sure what to do with that.
I misunderstood the Set up EC2 Instance Connect instructions. This support article had clearer instructions: https://aws.amazon.com/premiumsupport/knowledge-center/ec2-instance-connect-troubleshooting/
Browser-based SSH connections require that your instance's security group inbound rules allow EC2 Instance Connect access to SSH on TCP port 22.
The key was to find the IP for EC2 Instance Connect and then create
a security group to whitelist that ip address.
EC2 Instance Connect IP can be retrieved via PowerShell by using Get-AWSPublicIpAddressRange (or curl). For us-west-2:
> Get-AWSPublicIpAddressRange -Region us-west-2 -ServiceKey EC2_INSTANCE_CONNECT | select IpPrefix
IpPrefix
--------
18.237.140.160/29
Once I configured my Security Group to use that Source I could connect.
Generally better way then using EC2 Instance Connect is through SSM System Manager. It does not require opening any inbound ports. Instead you should add/modify your install role to allow SSM to work. What's more you can control access to your instance using regular IAM permissions, unlike for EC2 Instance Connect.
Connection through SSM is in the same menu in the AWS console as EC2 Instance Connect. Thus, once you setup your instance role, wait few minutes for the instance to register with SSM, and once this happens you should be able to use SSM System Manager to connect to your instance from the console.
Regarding EC2 Instance Connect IP range. I don't know which range published applies only to EC2 Instance Connect. You would have to filter it by region probably and then find one which works. In worse case scenario its try-and-see approach.

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.

How do I set up and log into a vpn from my mac in aws?

I have an instance and s3 bucket in AWS (which I'm limiting to a range of IPs). I'm wanting to create a VPN and be able to authenticate myself while trying to log into that VPN to get to that instance.
To simplify, I'm trying to set up a dev environment for my site. I'm wanting to make sure I can limit access to that instance. I'm wanting to use a service to authenticate anybody wanting to get to that instance. Is there a way to do all of this in AWS?
Have you looked at AWS Client VPN:
https://docs.aws.amazon.com/vpn/latest/clientvpn-admin/what-is.html
This allows you to create a managed VPN server in your VPC which you can connect to using any OpenVPN client. You could then allow traffic from this vpn to your instance using security group rules.
Alternatively you can achieve the same effect using OpenVPN on an EC2 server, available from the marketplace:
https://aws.amazon.com/marketplace/pp/B00MI40CAE/ref=mkt_wir_openvpn_byol
Requires a bit more set up but works just fine, perfect if AWS Client VPN isn't available in your region yet.
Both these approaches ensure that your EC2 instance remains in a private subnet and is not accessible directly from the internet. Also, the OpenVPN client for mac works just fine.

Is there a managed way to do P2S VPN in AWS?

I want to connect my (OSX) laptop to a VPC in AWS but I couldn't find a way to do this since I don't have a static IP.
Azure has managed P2S VPN: https://learn.microsoft.com/en-us/azure/vpn-gateway/point-to-site-about. Is there something like this in AWS?
You can stand up a virtual device (such as a CheckPoint firewall) in the VPC on an EC2 instance that has point to site VPN functionality. There are a number of AMI’s and quick starts for this available. You would also need a VPN client running on your laptop.
As an alternative, you can set up a bastion server and whitelist your IP. You can create a shell script that gets your IP and makes an AWS CLI call to update the security group with that value if it doesn’t exist. Then, log into the bastion and you’re in the VPC.
You can setup a client VPN under the VPC menu:
https://docs.aws.amazon.com/vpn/latest/clientvpn-admin/cvpn-working-endpoints.html
It will require to have server cert, as well as client certificates or a link to managed AD for authentication.