EC2 Instance not available in ECS - amazon-web-services

I have created an EC2 instance via Terraform with the following configuration:
EC2 instance is using the latest Amazon ECS-Optimized Amazon Linux 2 AMI.
Instance is sitting in a private subnet, with a route to a NAT GW. Tested internet connectivity fine.
SG rules are configured correctly.
EC2 Instance profile is using AmazonEC2ContainerServiceforEC2Role
EC2 user-data is configured (with my cluster name) with:
echo ECS_CLUSTER=my-cluster-name >> /etc/ecs/ecs.config
When I go to my ecs-cluster, no instances show in the EC2 Instance section of the console.
Is there anything else I'm missing as to why this cluster can't register with the EC2 instance?

Related

Auto configure private DNS resolve in VPC network for EC2 instances running from AMI

I have AMI template server in EC2 AWS witch run my server.
For sure it's running in single VPC network.
I want to be able to connect any my server using ssh once it's running using hostname dns resolve.
For example I have gateway, server-01, server-02 in my ec2 instances list.
Once I launch one more server from my AMI (server-03), I need to connect to it from gateway server using ssh server-03
How I can do it?
I would suggest using terraform to manage your EC2 instances. This will allow you to do many things you would normally do manually.
You can have a private or public hosted zone assigned to your VPCs (public would require a bit more)
Then on terraform, you can have the following:
Your ec2 instance creation.
A tfvar file containing the variables for all your EC2 instances
Your Hosted Zone attaching the EC2 private IP to a DNS
Output afterwards to print out your new EC2 instance with the private DNS you can SSH to

Why doesn't my EC2 instance register with the ECS cluster

I am creating an AWS ECS cluster (Networking + Linux).
I follow all the steps, set up the subnets and use the existing VPC and the EC2 instance is created.
However, when I go into my cluster > ECS Instances I don't see any EC2 instances there. It doesn't seem to register there.
My EC2 instance has a public IP so that should not be an issue. What could be the problem?
You haven't specified in the question, but normally you also should modify your UserData so that it registers with the non-default cluster:
#!/bin/bash
echo ECS_CLUSTER=<your-cluster-name> >> /etc/ecs/ecs.config
Also Amazon ECS-optimized AMI should be used which has pre-installed ECS Agent.
Edit: Also need to make sure that instances have access to the ECS Service, for example by having public IP and internet access. Without that, ECS Agent won't be able to communicate with the ECS Service.
UserData in console can be specified in the following configuration:
You can also use Launch Templates or Launch Configurations to specify the UserData reduce the amount of work needed when launching new instances.

Auto Scaling, Elastic IP

When I use Network Load Balancer with Auto Scaling, everytime an instance is spawned it gets a new public IP.
I would like to have an EIP for each instance. I was hoping that when I assigned an EIP to availability zones in the Network Loadbalancer configuration would do the trick.
Is there a a way to have autoscale and static IPs for the instances spawned?
https://aws.amazon.com/blogs/aws/new-network-load-balancer-effortless-scaling-to-millions-of-requests-per-second/
According to this it looks it's not possible:
Unfortunately, there is no way to make autoscaling automatically
assign an Elastic IP address to newly launched instances
Static IP for Auto Scale in AWS
and according to this:
Assigning static IPs to auto scaled EC2 instance
Write a script and put it on your startup script in launch configuration for your autoscale group, that script can do anything you want, range from assigning the new EIP to check other services for the white/blacklist.
For more info read
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
As others have mentioned this can be accomplished by provisioning new EIP and using UserData to associate the instance with newly issued EIP. However, following setup would have the following issues:
1) EIP have limits by default its 5 per VPC, and even you can increase the limit you need to know the maximum number of EIP your VPC will use
2) When instance gets terminated you will need to create a process to delete staled EIP or figure out how to reassociated previously allocated EIP
Having said that I do use a static EIP in my ASG but its only for HA rather than scalability, so in the following example I'm reusing existing EIP each time I launch a new instance
#!/bin/bash -xe
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 disassociate-address --association-id ${OpenPVNEIP.AllocationId} --region ${AWS::Region} || true
aws ec2 associate-address --instance-id "${!INSTANCE_ID}" --allocation-id ${OpenPVNEIP.AllocationId} --region ${AWS::Region}
I created a Lambda inside a private subnet.
I linked the private subnet to a NAT Gateway and thus I managed to get a static IP for my Lambda. The limit of 5 EIPs was an issue for me.
https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/
With EC2 & Auto scaling, You need using user data in EC2 to Auto Attach Elastic IP to EC2 Instance For Auto scaling
#!/bin/bash
aws configure set aws_access_key_id "XYZ..."
aws configure set aws_secret_access_key "ABC..."
aws configure set region "ap-..."
aws ec2 associate-address --instance-id "$(curl -X GET "http://169.254.169.254/latest/meta-data/instance-id")" --public-ip your_elastic_IP
Note: you should create new user & IAM have only permission associate-address to create/get aws key
Hope it be help you :)

Unable to see EC2 instance in amazon aws

I created a free account in amazon aws and created an EC2 instance using "Amazon Linux AMI". I could ssh to the instance and I installed NodeJS in it. I also purchased a domain name and configured it with an elastic IP to that instance.
Now when i login to the EC2 management console I couldn't see the instance. What might be reason for this?
In which region did you create the instance?
Check the region at the top menu, to the right.

LaunchWaitCondition failed. The expected number of EC2 instances were not initialized within the given time

The error message is:
Stack named 'awseb-e-r3uhxvhyz7-stack' aborted operation. Current state: 'CREATE_FAILED' Reason: The following resource(s) failed to create: [AWSEBInstanceLaunchWaitCondition].
I am trying to use Multi-Container Docker in AWS Elastic Beanstalk.
Can someone help me to get rid of this error.Is it necessary to use more than one EC2 instance for using Multi-Container Docker in AWS Elastic Beanstalk?
This sound kinda what your issue is:
If you use Amazon VPC with Elastic Beanstalk, Amazon EC2 instances deployed in a private subnet cannot communicate directly with the Internet. Amazon EC2 instances must have Internet connectivity to communicate to Elastic Beanstalk that they were successfully launched. To provide EC2 instances in a private subnet with Internet connectivity, you must add a load balancer and NAT to the public subnet. You must create the appropriate routing rules for inbound and outbound traffic through the load balancer and NAT. You must also configure the default Amazon VPC security group to allow traffic from the Amazon EC2 instances to the NAT instance.
Source: Amazon EC2 Instances Fail to Launch within the Wait Period
I've fixed this. It looks the like IAM role created by default for the single docker EB deployment didn't contain the necessary ECS Policy (unconfirmed).
I followed the instructions to create a policy to add the role and everyhing worked.