Create managed instance - amazon-web-services

I am trying to create a managed instance with AWS.
I followed this page to create the IAM role:https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html.
So it is AmazonSSMManagedInstanceCore
This is the code I am using to associate the IAM role with the EC2.
# Make EC2s with AWS Ubuntu 20
instances = subnet.create_instances(ImageId='ami-0885b1f6bd170450c',
InstanceType='m1.small',
MaxCount=num,
MinCount=num,
Monitoring={'Enabled': True},
SubnetId=subnet.subnet_id,
KeyName=key_name,
IamInstanceProfile={
'Arn': 'arn goes here',
},)
wait_until_running(instances)
And when I check in the console the role shows up.
But when I do
aws ssm describe-instance-information
I get
{
"InstanceInformationList": []
}
The ultimate goal here is to be able to send a command to the instance.

Based on the comments.
The instance does not have public IP address, which indicates it likely has no access to SSM service.
For SSM to work on your instance, it must be able to connect to the SSM service. This is usually enabled in one of three ways:
Instance is a public subnet and has direct internet access.
Instance is in a private subnet and uses NAT gateway to access internet.
Your VPC uses VPC interface endpoints for SSM service to connect to the SSM service. This does not require internet access and provides private communication between instance and the SSM service.

Related

Unable to Access IAM AWS Service Endpoint in a private subnet using a Private Link of VPC Endpoint

We're trying to access AWS Services from a private subnet in a VPC using a VPC endpoint or Private Link. We're trying to avoid adding a NAT gateway on the private subnet.
We created/added a VPC Endpoints (i.e. AWS Service for EC2 and S3)
com.amazonaws.us-east-2.ec2
com.amazonaws.us-east-2.s3
But we're not able to access something like com.amazonaws.us-east-2.iam; is there an iam endpoint or is this supported or is there any workaround
for us to use iam via VPC Endpoint on us-east-2?
Here's the error we're getting when we use the latest version of Java AWS SDK to get ec2 instance attached iam role programatically
org.apache.http.conn.ConnectTimeoutException: Connect to
iam.amazonaws.com:443 [iam.amazonaws.com/
Thanks for any information you can provide.
As of May 2022, VPC endpoint for IAM is not supported. You'd have to connect to IAM via internet. Or as a workaround, you can do IAM actions in CloudFormation.

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.

How to setup access to ElasticDomain from different AWS account?

I created an ElasticDomain in private subnet which has a VPC endpoint and a security group which allows access from port 443. We want to access this ElasticDomain from another AWS account's EC2 instance. But we are not able to do that.
We tried creating ElasticDomain with a public endpoint which is accessible from anywhere but is not the right option for us.
We are considering to use apache as a reverse proxy and then set up a VPC private-link on a consumer account. Is that a right approach?
AWS PrivateLink via VPC and Network Load Balancer doesn't support ElasticDomain for now.
Check out the following two links:
1) https://aws.amazon.com/blogs/security/how-to-control-access-to-your-amazon-elasticsearch-service-domain/
2) AWS ElasticSearch write to account "A" from lambda in account "B"

What is the best way to get the private IP addresses of other ec2's in an autoscaling group while on one of the ec2 instances?

I need to update a config file in a shared EFS drive with all of the private IP addresses of the current autoscaling group.
The approach I'm thinking is to run a user data script that queries the ASG for the private IP addresses then echo that into the config file. To do that the ec2 needs to have AWS CLI credentials and appropriate read-only access. Ideally, I don't want to store any credentials on this ec2.
Is there another way? Possibly VPC Endpoint or something?
Thanks!
You are asking two questions.
How do I provide credentials securely to an EC2 instance?
You use IAM Roles and assign the role to your EC2 instances. Then use the instance credentials in your code. The CLI examples below will automatically pick up these credentials.
Using an IAM Role to Grant Permissions to Applications Running on Amazon EC2 Instances
How do I get the private IP address of EC2 instances in an Auto Scaling Group (ASG)?
You need to get a list of instances attached to your ASG.
For each instance in your ASG call the describe API and extract the private IP address.
Example commands:
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-auto-scaling-group
aws ec2 describe-instances --instance-ids i-1234567890abcdef0
You can filter the command output. For example add the following to the second command to just display the private IP address:
--query 'Reservations[*].Instances[*].PrivateIpAddress'
Recommendation:
I would use the Python SDK and write a simple program that provides these features and updates your config file.

Access EC2 Instance Service Via AWS Lambda Function

I have a EC2 Instance which only have Private IP and I have installed the Apache Kafka on same Instance running on say 10.0.4.44:9092.
Now, I have created the AWS Lambda Function which read the document from given Bucket and sent the Document body to Apache Kafka running on EC2 Instance.
Now, AWS Lambda is unable to access the EC2 Instance Service.
How Can I grant access to Lambda for accessing Apache kafka Service on EC2?
You need to add the Lambda function to the VPC the EC2 server is running in. Here's the announcement blog post of Lambda VPC support with a walk through for setting it up. Here's the official documentation.
A Lambda function in a VPC will not get a public IP address. This means it won't be able to access anything that is outside the VPC. Since you also need to access S3 you will need to setup an S3 VPC Endpoint so that your function will still have S3 access.
If your function needs access to other things outside the VPC then you will have to add it to a private subnet of the VPC with a NAT Gateway attached.