- 在AWS CDK项目中安装boto3库
pip install boto3
- 在AWS CDK项目中导入boto3和AWS SDK for Python(即aws_cdk.aws_dynamodb)模块
import boto3
from aws_cdk import (
aws_dynamodb as dynamodb,
core,
)
- 添加以下代码以为一个或多个全局二级索引添加dynamodb:Query权限。将“table”替换为要为其添加权限的表名称,“index_1”和“index_2”替换为要为这些全局二级索引添加权限的索引名称。
table = dynamodb.Table.from_table_name(
self,
"existing_table",
"table_name"
)
# Add Query permissions for an existing GSI named 'index_1'
boto3.client("dynamodb").update_table(
TableName=table.table_name,
GlobalSecondaryIndexUpdates=[
{
"Create": {
"IndexName": "index_1",
"KeySchema": [
{
"AttributeName": "partition_key",
"KeyType": "HASH",
},
{
"AttributeName": "sort_key",
"KeyType": "RANGE",
},
],
"Projection": {
"ProjectionType": "ALL",
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
},
},
{
"Update": {
"IndexName": "index_1",
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
},
},
],
)
# Add Query permissions for an existing GSI named 'index_2'
boto3.client("dynamodb").update_table(
TableName=table.table_name,
GlobalSecondaryIndexUpdates=[
{
"Create": {
"IndexName": "index_2",
"KeySchema": [
{
"AttributeName": "partition_key",
"KeyType": "HASH",
},