How to set instance name from cli - amazon-web-services

When logged in to the AWS EC2 Management Console, the list of instances has, as its first column, "Name" (followed by "Instance ID", etc).
For instances created through AWS CLI (using aws ec2 run-instances), the name field is empty. How can I set the name programmatically?
Also, is there any implication for giving it a name (e.g. does it have to be unique, and is the name used by something?) I would like to have it as a useful info, for managing my instances from the console.

By convention, the name that's displayed in the instance list is a resource tag with the Key Name and the name of your choice as its value.
You can do this via the AWS CLI using the --tag-specifications option as documented here:
aws ec2 run-instances [other options] --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyInstanceName}]'
Alternatively, you can also add tags, including the Name tag to existing resources using aws ec2 create-tag.

Related

Extract a list of Elastic IPs and Instance Names using AWS System Manager

In the AWS Cloud Environment i have to the get list of all Elastic IPs with instance name and in all region/AZs like eu-west-1 , eu-west-3, ap-south-1, us-east-1 etc..
How this can be achieved via AWS System manager if possible. If not, then do i have to write any other function/code or use other AWS functionality.
I would do it by writing a program (eg in Python) with these steps:
Loop through each (applicable) Region using describe_regions()
Loop through each Elastic IP address using describe_addresses()
Extract the InstanceId and PublicIp from the response
Call describe_instances() for the given InstanceId to obtain a list of Tags. The "instance name" is stored as a Tag with Key = 'Name'

How to set "Name" of security group (AWS EC2)

Creating an EC2 security group through the console allows you to set a "group name" and it automatically provides a "group id".
However the "name" is always blank, unless the security group was generated automatically by elastic beanstalk or another resource.
Is there any way to set this name in the console, otherwise how is it done in the CLI?
You can either edit the name directly in the console or attach a Name tag to your security group.
Using AWS CLI:
aws ec2 create-tags --resources <sg_id> --tags Key=Name,Value=Test-Sg

AWS Cloudformation: Is it possible to access user id inside a Cloudformation template?

I would like to auto-tag certain AWS resources defined in a CloudFormation template with the user who uses the template to create a stack. Is it possible to access any sort of user id in the template?
I don't think this is possible.
AWS services are tied to AWS Accounts. Once IAM confirms that a particular user has permission to make an API call, resources that are generated become associated with an Account rather than a particular User. For example, it is not possible to look at an EC2 instance and determine who launched the instance.
This information is, however, available in AWS CloudTrail, but that is more of an audit log — it does not provide the user information back to the service.
So, I suspect that a stack is not provided with information about the User that launched it.
There is one way of doing it, but it isn't pretty and there is a caveat.
You can run a bash script like the below on an ec2 instance:
AWS_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
USER_ID=`aws sts get-caller-identity --output text --query 'Arn'`
aws ec2 create-tags --resources "${AWS_INSTANCE_ID}" --tags 'Key=CreatedBy,Value="${USER_ID}"' --region eu-west-1
That will tag the current instance with the name of the user that run the CLI on that instance.
The caveat is however that the CLI needs to be run by the user - and not a role, so your users keys will have to be copied to the server - and then removed again at the end of the script.
Not ideal, but it gives you an option.

change AWS AMI Name

is it possible to use the aws cli to change an AMI name?
I can successfully add a "Name" tag (using aws ec2 create-tags) but not change the "AMI Name" that shows on the web UI.
This is an imported AMI so the AMI Name is something like
import-ami-XXXXX.
I've tried:
aws ec2 modify-image-attribute --image-id AMI_ID --attribute Name --value VALUE
but I'm getting back a:
An error occurred (InvalidParameterCombination) when calling the ModifyImageAttribute operation: No attributes specified.
any suggestion?
It is not possible to change the name of an AMI image. Once the name is set, it cannot be changed.
You have 2 possible resolutions:
Create the AMI image again, using a different name. However, in your case, the name may be automatically generated and this may not be an option.
Copy the AMI within the same region, giving the copy a more desirable name.
I see two name like fields in the EC2 console for AMIs. One called "Name" and another one called "AMI Name". The latter is immutable, as the previous answer suggests.
BUT, I was able to set the "Name" field by using create-tags as shown below:
aws ec2 create-tags --resources AMI_ID --tags Key=Name,Value=bbb

Specifying resource group name when creating resource group in aws cli

I'm trying to figure out how to specify a resource group name when creating resource groups via aws cli, when I look at the cli documentation for create-resource-group command I only see option to specify tags, how do you specify a name? , also I noticed when I access the console I don't see resource groups created using cli. Any ideas?