Turn on and off ssh access to EC2 - amazon-web-services

I want the EC2 to be accessible only through session manager.
Originally I launched one, ssh into it, installed the ssm-manager, but then I found out I can't replace its ENI with a non-public-facing ENI.
So then I tried launching one with an ENI with only a private IP, associate a public EIP to it, but I fail to ssh into the machine, it keeps timing out.
After quite a lot of experimenting around I just can't find the solution to this problem, so here I am.
What do I need?
I want to know how to create an EC2 instance that has NO PUBLIC IP and NO PUBLIC SUBNET.
Attach a public IP to it and config it to work
SSH into it, install SSM agent
and then disable ALL PUBLIC FACING networking.
any suggestions? guides?
thanks

Create an AMI that has the SSM agent already installed and configured, or pick one of the existing AMIs that have SSM agent pre-installed. Then launch your EC2 instances from that AMI.

Related

Amazon Linux 2 instances won't appear in Systems Manager

I think I've done everything listed as a pre-req for this, but I just can't get the instances to appear in Systems Manager as managed instances.
I've picked an AMI which i believe should have the agent in by default.
ami-032598fcc7e9d1c7a
PS C:\Users\*> aws ec2 describe-images --image-ids ami-032598fcc7e9d1c7a
{
"Images": [
{
"ImageLocation": "amazon/amzn2-ami-hvm-2.0.20200520.1-x86_64-gp2",
"Description": "Amazon Linux 2 AMI 2.0.20200520.1 x86_64 HVM gp2",
I've also created my own Role, and included the following policy which i've used previously to get instances into Systems Manager.
Finally I've attached the role to the instances.
I've got Systems Manager set to a 30 min schedule and waited this out and the instances don't appear. I've clearly missed something here, would appreciate suggestions of what.
Does the agent use some sort of backplane to communicate, or should I have enabled some sort of communication with base in the security groups?
Could this be because the instances have private IPs only? Previous working examples had public IPs, but I dont want that for this cluster.
Besides the role for ec2 instances, SSM also needs to be able to assume role to securely run commands on the instances. You only did the first step. All the steps are described in AWS documentation for SSM.
However, I strongly recommend you use the Quick Setup feature in System Manager to setup everything for you in no time!
In AWS Console:
Go to Systems Manager
Click on Quick Setup
Leave all the defaults
In the Targets box at the bottom, select Choose instances manually and tick your ec2 instance(s)
Finish the setup
It will automatically create AmazonSSMRoleForInstancesQuickSetup Role and assign it to the selected ec2 instance(s) and also create proper AssumeRole for SSM
Go to EC2 Console, find that ec2 instance(s), right-click and reboot it by choosing Instance State > Reboot
Wait for a couple of minutes
Refresh the page and try to Connect via Session Manager tab
Notes:
It's totally fine and recommended to create your ec2 instances in private subnets if you don't need them to be accessed from internet. However, make sure the private subnet has internet access itself via NAT. It's a hidden requirement of SSM!
Some of the AmazonLinux2 images like amzn2-ami-hvm-2.0.20200617.0-x86_64-gp2 does not have proper SSM Agent pre-installed. So, recreate your instance using a different AMI and try again with the above steps if it didn't work.
Could this be because the instances have private IPs only? Previous working examples had public IPs, but I don't want that for this cluster.
If you place your instance in a private subnet (or in a public subnet but without a public IP), then the SSM agent can't connect to the SSM Service. Thus it can't register to it.
There are two solutions to this issue:
Setup VPC Interface endpoint in a private subnet for SSM System Manger. With this your intances will be able to connect to the SSM service without the internet.
Create a public subnet with NAT gateway/instance, and setup route tables to route internet traffic from the private subnets to the NAT gateway. This way your private instances will be able to access the SSM service over internet through the NAT device.

Packer cant connect to a EC2 instance spun up inside a VPC

Due to organizational restrictions all EC2 instances must be spun up inside a VPC. I am running Packer from an on prem server (via a Jenkins pipe) and during the image creation, it spins up an EC2 instance inside this VPC which is assigned a private IP.
Back on my on prem server, Packer is waiting for the instance to start up by querying the private IP assigned to it and there is no connectivity between the on prem Jenkins server and the EC2 instance spun up by Packer. Therefore the process hangs is stuck at Waiting for WinRM to become available forever.
Is there a workaround to this?
I am using the builder of type amazon-ebs
A bastion host on public subnet my help you in this case. You can find the Packer configuration for bastion host here: https://www.packer.io/docs/builders/amazon-ebs.html#communicator-configuration

EC2 Instance doesn't become managed after installing SSM Agent

I've installed SSM Agent (2.2.607.0) on Windows Server 2012 R2 Standard instance with the EC2 Config (4.9.2688.0). After installing it, i cannot see the server on the Managed Instances screen. I did the same steps on other servers (Windows and Linux) and it worked.
Tried to uninstall the EC2 Config, reinstalled it again with no luck. Tried to install a different SSM Agent version (2.2.546.0) with no luck also.
Any thoughts?
The agent is installed, but the instance still needs the proper role to communicate with the systems manager. Particularly this step of Configuring Access to Systems Manager.
By default, Systems Manager doesn't have permission to perform actions
on your instances. You must grant access by using an IAM instance
profile. An instance profile is a container that passes IAM role
information to an Amazon EC2 instance at launch.
You should review the whole configuration guide and make sure you have configured all required roles appropriately.
I had this problem, and of the four troubleshooting steps - SSM Agent, IAM instance role, Service Endpoint connectivity, Target operating system type, it turned out that the problem was endpoint connectivity.
My VPC, Subnet, route table, and internet gateway all looked correct (and were identical to another instance which was being managed by SSM). But the instance didn't have a public IP, and without that you can't use the IGW. You can't use a VPC endpoint and an Internet Gateway. So adding a public IP allowed the instance to connect to SSM and become managed.
Extra complication : I was trying to use EC2 Image Builder, which creates an instance without a public IP. So there is no way to use Image Builder in a VPC which has an Internet Gateway.
New SSM agent version comes with a diagnostic package.. You can run that to see which prerequisites is missing.
https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-cli.html

Launching an AWS EC2 with a specified private address and/or hostname?

We do continuous integration from Jenkins, and have Jenkins deploy to an EC2 instance. This EC2 instance exports an NFS share of the deployed code to EC2 processing nodes. The processing nodes mount the NFS share.
Jenkins needs to be able to "find" this code-sharing EC2 instance and scp freshly-built code, and the processing nodes need to "find" this code-sharing EC2 instance and mount its NFS share.
These communications happen over private IP space, with our on-premise Jenkins communicating with our EC2 in a Direct Connect VPC subnet, not using public IP addresses.
Is there a straightforward way to reliably "address" (by static private IP address, hostname, or some other method) this code-sharing EC2 that receives scp'd builds and exports them via NFS? We determine the subnet at launch, of course, but we don't know how to protect against changes of IP address if the instance is terminated and relaunched.
We're also eagerly considering other methods for deployment, such as the new EFS or S3, but those will have to wait a little bit until we have the bandwidth for them.
Thanks!
-Greg
If it is a single instance at any given time that is this "code-sharing" instance you can assign an Elastic IP to it when after you've launched it. This will give you a fixed public IP that you can target.
Elastic IPs are reserved and static until you release them. Keep in mind that they cost money when they are not reserved.
Further on you can use SecurityGroups to limit access to the instance.
In the end, we created & saved a network interface, assigning one of our private IPs to it. When recycling the EC2, and making a new one in the same role, we just assign that saved interface (with its IP) to the new EC2. Seems to get the job done for us!

Cannot access instances of openstack on AWS

I've openstack(single node) installed on an AWS instance. I can log into the openstack dashboard and I'm also able to spawn instances but, I'm not able to connect via SSH or ping those instances.
In the security group setting section, I've allowed all types of protocols for the instances.
SSH into the AWS instance first, from there you can access the openstack instances.
If you need your spawned instances to be available over the public internet, you need to manage your public (floating) IPs.