How to rename DynamoDB tables in Gen 2?
Amplify CLI Version
Gen 2
Question
I really like Amplify but I find the resource name excruciatingly annoying, specially when deploying multiple branches/sandboxes in one AWS Account.
I am trying to rename core resources, such as userpool, lambda function etc, and I want to do the same with DynamoDB table but I don't seem to be able to get the Cloudformation reference because its in a nested stack, is it possible?
// Rename resources
backend.auth.resources.cfnResources.cfnUserPool.userPoolName = `${namePrefix}-userpool`;
backend.auth.resources.cfnResources.cfnIdentityPool.identityPoolName = `${namePrefix}-identitypool`;
// I want to rename here
// backend.data.resources.cfnResources.cfnTables["Example"].tableName = `${namePrefix}-example-table`;
backend.data.resources.cfnResources.cfnGraphqlApi.name = `${namePrefix}-gqlapi`;
backend.stripeFunction.resources.cfnResources.cfnFunction.functionName = `${namePrefix}-payments-handler`;
backend.stripeWebhookCustomResource.resources.cfnResources.cfnFunction.functionName = `${namePrefix}-stripe-webhook-handler`;
Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂
It is not possible to change the DynamoDB table names currently.
The DynamoDB tables are stored in backend.data.resources.cfnResources.amplifyDynamoDbTables["TableName"]. However you will still run into a couple issues with changing the table name.
- The CfnResource property is private on our wrapper class. You can ignore this with
// @ts-expect-errorbut we cannot guarantee that there will be no breaking changes to a private property.
backend.data.resources.cfnResources.amplifyDynamoDbTables.Todo
// @ts-expect-error
.resource // private property
.addPropertyOverride("tableName", "MyCustomTableName");
I don't know why resource is private in this case and it may be a mistake in the implementation. I will need to check with the team on this.
- Even with the above workaround you will not be able to set the table name because the policy for our custom resource that creates the table is restricted to a certain naming pattern.
@dpilch Thanks for the quick response, I appreciate it.
I guess I am making a feature request, to allow us to provide clean, user friendly names to not mess up the UI in the AWS Console and give a predictable outcome for the table names.
For example I have a project with 1 sandbox env, dev and prod with 7 models. The tables in the account look really messy and hard to understand/easy to make a mistake when selecting tables to perform actions on/inspect configuration etc
Other than that keep up the great work!
Any update on a solution for providing a name for Amplify generated tables? It'd be much better to allow user-defined, meaningful table names instead of relying on a random UUID table name.
I see that this PR was closed... is there another solution available?
We discovered some issues in the implementation and are assessing some alternatives. I have a workaround, but it does have some issues. The resource property on the table is private so it isn't technically beholden to semantic version breaking changes. That being said I don't anticipate this changing anytime soon.
The workaround uses the private property and adds the necessary policy changes. Using the workaround would be at your own risk due to the private property.
import { Role, PolicyStatement, Effect } from 'aws-cdk-lib/aws-iam';
export const backend = defineBackend({
auth,
data,
});
backend.data.resources.cfnResources.amplifyDynamoDbTables.Todo
// @ts-expect-error
.resource // private property
.addPropertyOverride("tableName", "MyCustomTableName");
// update Amplify DDB table manager policy to add custom table name
const policy = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
'dynamodb:CreateTable',
'dynamodb:UpdateTable',
'dynamodb:DeleteTable',
'dynamodb:DescribeTable',
'dynamodb:DescribeContinuousBackups',
'dynamodb:DescribeTimeToLive',
'dynamodb:UpdateContinuousBackups',
'dynamodb:UpdateTimeToLive',
'dynamodb:TagResource',
'dynamodb:UntagResource',
'dynamodb:ListTagsOfResource',
],
resources: [
Fn.sub('arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tableName}', {
tableName,
}),
],
});
const onEventRole = backend
.data
.resources
.nestedStacks
.AmplifyTableManager
.node
.findChild('AmplifyManagedTableOnEventRole') as Role;
const isCompleteRole = backend
.data
.resources
.nestedStacks
.AmplifyTableManager
.node
.findChild('AmplifyManagedTableIsCompleteRole') as Role;
onEventRole.addToPolicy(policy);
isCompleteRole.addToPolicy(policy);
backend.data.resources.cfnResources.cfnRoles.TodoIAMRole.addPropertyOverride(
"Policies.0.PolicyDocument.Statement.0.Resource",
[
"arn:aws:dynamodb:us-west-2:<aws-account-id>:table/MyCustomTableName",
"arn:aws:dynamodb:us-west-2:<aws-account-id>:table/MyCustomTableName/*",
]
);
Any further update on this. Me and my team really need this feature as soon as possible. When we create a dynamodb table all the tables end like this Appointment-<UNIQUE_ID>-NONE. In our case instead of NONE, we will like to show env name to distinguish different types of ENV. Please les us know if we can at least remove the NONE, and have env variable in Amplify GEN 2
Also we still wanted to keep the same name but way to disniquish by env. We should be still able to do the same as here:
export const updateAppointment = async (appointment: Appointment) => {
const { errors, data: appointmentResponse } = await client.models.Appointment.update(appointment)
if (errors) {
return null
}
return appointmentResponse
}
NONE is only used in sandbox. In your other branches it will be replaced with the branch name.
In Gen 2 it doesn’t, I have the tables in prod and they all have NONE at end. I put some tag to differentiate but still it’s pain when I’m looking from root user.
There are so many other thread that mentioned this but seems like there is no solution on this.
Auto-defined tags give some clues regarding the table.
You can filter them too.
I hope this helps!
I got that, but wouldn' t be nice instead of showing NONE at end, it show the ENV they setup or have PROD or DEV etc.
Hey was there any update to this? as I am seeing this behavior aswell.
Hello, we're also expecting to have the env name instead of NONE ad in gen1. It's faster to find the right ressource.
Also we're using Dynobase to explore DynamoDB tables and there is no way to find the right table from there with the current naming.
+1
We are also waiting for a resolution to this bug! Please fix it!
This is very important!!!! Imagine when you're working at 2:00 AM, where speed is very important, under pressure, mentally exhausted, and you're doing a lot of tracking, select-copy-paste, test, select-copy-paste, test ..., this small "word", the environment name becomes your beacon/lighthouse, to avoid mistakes and work with speed!
+1 desperate for this please. My resources look like an absolute mess, so hard to identify what is what