View a markdown version of this page

Trabalhar com a marcação de recursos do DynamoDB usando a AWS Command Line Interface v2 - Amazon DynamoDB

Trabalhar com a marcação de recursos do DynamoDB usando a AWS Command Line Interface v2

O exemplo de código a seguir mostra como gerenciar tags para recursos do DynamoDB.

  • Crie uma tabela com tags.

  • Listar as etiquetas de um recurso.

  • Adicione tags a um recurso.

  • Remova as tags de um recurso.

  • Filtre tabelas por tags.

Bash
AWS CLI com script Bash

Crie uma tabela com tags.

# Create a table with tags aws dynamodb create-table \ --table-name TaggedTable \ --attribute-definitions \ AttributeName=ID,AttributeType=S \ --key-schema \ AttributeName=ID,KeyType=HASH \ --billing-mode PAY_PER_REQUEST \ --tags \ Key=Environment,Value=Production \ Key=Project,Value=Analytics \ Key=Owner,Value=DataTeam

Listar as etiquetas de um recurso.

# Get the table ARN TABLE_ARN=$(aws dynamodb describe-table \ --table-name TaggedTable \ --query "Table.TableArn" \ --output text) # List tags for the table aws dynamodb list-tags-of-resource \ --resource-arn $TABLE_ARN

Adicione tags a um recurso.

# Add tags to an existing table aws dynamodb tag-resource \ --resource-arn $TABLE_ARN \ --tags \ Key=CostCenter,Value=12345 \ Key=BackupSchedule,Value=Daily

Remova as tags de um recurso.

# Remove tags from a table aws dynamodb untag-resource \ --resource-arn $TABLE_ARN \ --tag-keys Owner BackupSchedule

Filtre tabelas por tags.

# Create another table with different tags aws dynamodb create-table \ --table-name AnotherTaggedTable \ --attribute-definitions \ AttributeName=ID,AttributeType=S \ --key-schema \ AttributeName=ID,KeyType=HASH \ --billing-mode PAY_PER_REQUEST \ --tags \ Key=Environment,Value=Development \ Key=Project,Value=Testing # Wait for table to become active aws dynamodb wait table-exists --table-name AnotherTaggedTable # List all tables echo "All tables:" aws dynamodb list-tables # Get ARNs for all tables echo -e "\nFiltering tables by Environment=Production tag:" TABLE_ARNS=$(aws dynamodb list-tables --query "TableNames[*]" --output text | xargs -I {} aws dynamodb describe-table --table-name {} --query "Table.TableArn" --output text) # Find tables with specific tag for ARN in $TABLE_ARNS; do TABLE_NAME=$(echo $ARN | awk -F/ '{print $2}') TAGS=$(aws dynamodb list-tags-of-resource --resource-arn $ARN --query "Tags[?Key=='Environment' && Value=='Production']" --output text) if [ ! -z "$TAGS" ]; then echo "Table with Production tag: $TABLE_NAME" fi done

Para ver uma lista completa dos guias de desenvolvedor e exemplos de código do SDK da AWS, consulte Usar o DynamoDB com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.