change AWS AMI Name - amazon-web-services

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

Related

How to set instance name from cli

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.

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

How to get arn of EC2 instance in AWS

How do I get the arn of ec2 instance in AWS.
I am trying to use resource tag api to add the tags to an EC2 instance.
Resource tag api requires arn for all the resources.
How do I fetch that ?
You can "build" it yourself:
arn:aws:ec2:<REGION>:<ACCOUNT_ID>:instance/<instance-id>.
For this purpose, I think you can even use * as <REGION> and <ACCOUNT_ID>, and it will work.
To retrieve the <instance_id> you can use the Console or the CLI, or from within the instance itself with ec2metadata --instance-id

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.

Code deploy with Packer

I am creating an AWS AMI that is provisioned with Chef using Packer. At the creation of snapshots tags of the AWS AMI, I want it to be tagged with tags that assign it to be deployed with CodeDeploy:
{
"aws:autoscaling:groupName": "Env1"
}
In my JSON configuration for Packer, I am using snapshot_tags to define these.
The problem is that on creating the AMI, Packer fails with:
Build 'amazon-ebs' errored: Error adding tags to Resources ([]*string{(*string)(0xc420107170), (*string)(0xc420478758)}): InvalidParameterValue: Tag keys starting with 'aws:' are reserved for internal use
status code: 400, request id: fef34822-b692-4225-a2eb-a1cfac33a924
Cannot I use CodeDeploy with Packer since I must use aws in the tag?
ANSWER:
There is no need to use a tag for an AMI to deploy with CodeDeploy. My mistake.
Don't know about CodeDeploy, but you can never create a tag which starts with aws:.
Do not use the aws: prefix in your tag names or values because it is reserved for AWS use. You can't edit or delete tag names or values with this prefix. Tags with this prefix do not count against your tags per resource limit.
See AWS Docs: Using Tags - tag restrictions