43

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 ?

1
  • 1
    Check the documentation here
    – Paolo
    Commented Dec 17, 2019 at 9:33

1 Answer 1

82

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

5
  • 1
    Thanks for that. Is the syntax/format same for any kind of resource.ex:lambda function ,database
    – MeghP
    Commented Dec 17, 2019 at 10:51
  • 1
    @MeghP mostly, but have a look at docs.aws.amazon.com/general/latest/gr/…
    – user4093955
    Commented Dec 17, 2019 at 11:15
  • Can ec2 arn is valid entry in resource based policy? I will put it up as question if it is not too silly.
    – old-monk
    Commented Dec 22, 2020 at 21:57
  • 13
    Why doesn't AWS just list it in the instance details to avoid users making typos when building it? Commented Sep 10, 2021 at 21:08
  • 7
    This answer is technically correct but specifically for commercial aws accounts. The hardcoded "aws" in the example ARN is where the AWS partition goes and would need to be modified for other partitions used aside from the traditional commercial account (e.g. for GovCloud this would need to be changed from "aws" => "aws-us-gov"). If assembling the ARN dynamically in CloudFormation you can use the Pseudo parameter {AWS::Partition} to avoid hardcoding this and maximum reusability.
    – smk081
    Commented Jul 29, 2022 at 17:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.