fetch autoscaling group name in AWS

How does one know which autoscaling group an instance belongs to via api/aws cli commands?

You can use the autoscaling describe-auto-scaling-instances command together with the option --instance-ids, like this:

aws autoscaling describe-auto-scaling-instances --instance-ids="i-zzxxccvv"

Problem with autoscaling groups, is that these tags are not available to the instance. [...] I need to use instance tags in scripts, however, they are not available. Suggestions?

I'm interpreting this as you want to get the tags of the autoscaling group from which the instance belongs to? Using the AutoScalingGroupName returned from the command shown above, you can use the following command:

aws autoscaling describe-tags --filters Name=auto-scaling-group,Values=name-of-the-auto-scaling-group-here

The simplest way of finding the autoscaling group of an instance is to examine the tags. Any instances in an autoscaling group will have a tag named aws:autoscaling:groupName that contains the group name. You can retrieve that tag using the following command:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}" "Name=key,Values=aws:autoscaling:groupName"

Then you can grab the tags as described by Bazze.

An alternate solution to this is to propagate the tags from the autoscaling group to the instance.

If you are using cloudformation to create the ASG then set "PropagateAtLaunch": "true" for each tag you want to propagate (see Aws Cloud Formation Docs).

If you are creating the group manually make sure you check the "Tag New Instances" box for each tag.

Once you've done this, the tags will be copied to the instance and you can simply inspect the tags of your instance using the describe tags command:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}"

I find jq helpful for processing the output of the aws cli tool. For example, the following command produces a key=value list from the JSON:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}" | jq -r '.Tags[] | @text "\(.Key)=\(.Value)"'

To put it all together :

INSTANCE_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
ASG_NAME=`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 | jq '.Tags[] | select(.["Key"] | contains("aws:autoscaling:groupName")) | .Value'`
ASG_NAME=`echo $ASG_NAME | tr -d '"'`

Do an echo $ASG_NAME and you should see it.