kintax
kintax

Reputation: 61

AWS cli s3api put-bucket-tagging - cannot add tag to bucket unless bucket has 0 tags

As there is no create-tag for s3, only put-bucket-tagging can be used, which requires that you include all tags on the resource, not just the new one. Thus there is no way to add a new tag to a bucket that already has tags unless you include all existing tags PLUS your new tag. This makes it way more difficult to use for bulk operations, as you need to get all the tags first, extrapolate it into json, edit the json to add your new tag to every bucket, and then feed that to put-bucket-tagging.

Does anyone have a better way to do this or have a script that does this?

Command I'm trying: aws s3api put-bucket-tagging --bucket cbe-res034-scratch-29 --tagging "TagSet=[{Key=Environment,Value=Research}]"

Error I get: An error occurred (InvalidTag) when calling the PutBucketTagging operation: System tags cannot be removed by requester

I get the 'cannot be removed' error because put-bucket-tagging is trying to delete the other 10 tags on this bucket (because I didn't include them in the TagSet) and I don't have access to do so.

Upvotes: 2

Views: 3437

Answers (2)

PAS
PAS

Reputation: 2075

I would strongly recommend using json file instead of command line flags. I have spent few hours yesterday without any success making key and value with white spaces work. This is in the context of Jenkins pipline in groovy calling bash shell script block. Here is the syntax for calling json file.

aws resourcegroupstaggingapi tag-resources --cli-input-json file://tags.json

If you don't know exact format of json file then just run following, which will spit out format in tags.json file in current directory.

aws resourcegroupstaggingapi tag-resources  --generate-cli-skeleton  > tags.json

tags.json will have json. Just update the file and run the first commmand.

{
    "ResourceARNList": [
        ""
    ],
    "Tags": {
        "KeyName": ""
    }
}

You can fill up your data. e.g. for S3 bucket

{
    "ResourceARNList": [
        "arn:aws:s3:::my-s3-bucket"
    ],
    "Tags": {
        "Application": "My Application"
    }
}

Upvotes: 1

Azize
Azize

Reputation: 4476

You can use resourcegroupstaggingapi to accomplish the result you expect, see it below.

aws resourcegroupstaggingapi tag-resources --resource-arn-list arn:aws:s3:::cbe-res034-scratch-29 --tags Environment=Research

To handle spaces on tag name or value, use it as json.

aws resourcegroupstaggingapi tag-resources --resource-arn-list arn:aws:s3:::cbe-res034-scratch-29 --tags '{"Environment Name":"Research Area"}'

Upvotes: 4

Related Questions