AWS instance type and AZ issues when running aws-cli-run-instances script - amazon-web-services

I just created a VPC complete with:
NATGateway
Public/Private Subnets (Region us-east, AZ's 1a,1b)
Route Tables
Security Group.
I attempted to launch instances and test the VPC with a userdata script:
aws ec2 run-instances --image-id ami-0dfcb1ef8550277af --instance-type t2.micro --security-group-ids sg-03f30b6b4fabcf8c1 --subnet-id subnet-0c1f99185e9e1f6ef --key-name johnson1keypair --user-data file://user-data-subnet-id.txt
The output has my instance type: m1.small instead of t2.micro, AZ as us-east-1d instead of 1a and an error stating --instance-type: command not found.
I thought of manually changing the AZ for the created instance but not possible due to the fact that you can't move an existing instance to another subnet, Availability Zone, or VPC. I thought of manually migrating the instance but have not figured out to perform that as of yet.
I forgot to mention the script was ran from my terminal on my local Ubuntu machine.
Any help would be appreciated.

Related

EC2 Instance not available in ECS

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?

Kops Create Cluster fails with gossip in AWS Linux

I'm following the tutorial in https://github.com/kubernetes/kops/blob/master/docs/aws.md
with a bootstrap EC2 instance with Amazon Linux installed.
And everything seems to be working fine until I need to start configuring the cluster.
This error when running the kops command to create a configuration for the cluster. I couldn`t find any post on how to solve this issue.
Any help?
[ec2-user#ip-172-31-19-231 ~]$ kops create cluster --zones us-west-2a,us-west-2b,us-west-2c,us-west-2d ${NAME}
I0224 22:43:29.639232 3292 create_cluster.go:496] Inferred --cloud=aws from zone "us-west-2a"
I0224 22:43:29.699503 3292 subnets.go:184] Assigned CIDR 172.20.32.0/19 to subnet us-west-2a
I0224 22:43:29.699582 3292 subnets.go:184] Assigned CIDR 172.20.64.0/19 to subnet us-west-2b
I0224 22:43:29.699632 3292 subnets.go:184] Assigned CIDR 172.20.96.0/19 to subnet us-west-2c
I0224 22:43:29.699677 3292 subnets.go:184] Assigned CIDR 172.20.128.0/19 to subnet us-west-2d
error assigning default machine type for nodes: error finding default machine type: could not find a suitable supported instance type for the instance group "nodes" (type "Node") in region "us-west-2"
The error is stating you haven't specified an instance type for the EC2 nodes that will act as master and worker nodes.
The following is an example command:
kops create cluster --name=my-cluster.k8s.local \
--state=s3://kops-state-1234 --zones=eu-west-1a \
--node-count=2 --node-size=t2.micro --master-size=t2.micro

EC2 auto scaling with elastic IP

I have deployed an auto scaling EC2 and has associated an Elastic IP address with it. I'm not using a load balancer, because the total number of users doesn't exceed 20. Therefore, my current settings are to have 1 minimum and 1 maximum servers.
If the EC2 server fails, another one is created instead, which is what i'm trying to do. However, the elastic IP is not automatically remapped to the newly created server.
How can i assign the elastic IP automatically to the newly created EC2 instance? Is there a workaround this issue?
UPDATE:
I've added the following to User Data, but the new EC2 is created without a public ip still.
#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id=eipalloc-**.***.***.***
Without an ELB to manage your Elastic IPs, you'll need to use the User Data field on your EC2 instance to call the aws ec2 associate-address API endpoint upon instance creation:
aws ec2 associate-address --instance-id <instance id> --allocation-id <eip-alloc-id>
The EIP allocation ID can be found using the AWS Console. You can obtain the Instance ID by making this call in the User Data:
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
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 :)

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 :)

How to attach Elastic IP to EC2 instance during bootstrapping in aws CLI?

I can able to create an instace with follwoing command
aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type ${INSTANCE_TYPE} --key-name KEY_NAME --region us-east-1 --security-groups MYSECURITY_GROUP
But I did not find any option to attach elastic IP address to it. Is it possible to attach a Elastic IP during bootstrapping? Or post bootstrapping?
You can use --user-data (string) option to run-instances. The user data that you pass will contain the CLI to associate the elastic IP. The CLI command is given below. To get the instance-id in user data, use the metadata server:
curl instance-data/latest/meta-data/instance-id
You can also attach an elastic IP after you launch. Use associate-address to attach an elastic IP.
More examples in: associate-address
This example uses the new style (longer) instance id.
aws ec2 associate-address --instance-id i-0b263919b6498b123 --allocation-id eipalloc-64d5890a
You can get the allocation id from
aws ec2 describe-addresses
describe-addresses
The desire I read in the question is "how to start an instance with a given known IP address (from an elastic IP pool,) without first starting it with another temporary IP address."
The way to do this that I've found, is to first allocate a NetworkInterface, and then allocate the IP address, and then bind the IP address to the NetworkInterface, and then bind the pre-allocated NetworkInterface to the eth0 interface as part of the NetworkInterface launch parameters. Yup, four steps, just because you want your instance to start out with an IP address that won't change for the duration of its lifetime!
For "infrastructure as cattle" behind a NAT/load balancer of some sort, this doesn't matter of course. But for "cloud developer hosts" that you SSH to or "open remote" to from your IDE, keeping the IP address the same all the way from the beginning is a pretty important requirement.