Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to rename DynamoDB tables in Gen 2? #2991

Open
AlexThomas90210 opened this issue Oct 29, 2024 · 5 comments
Open

How to rename DynamoDB tables in Gen 2? #2991

AlexThomas90210 opened this issue Oct 29, 2024 · 5 comments
Assignees
Labels

Comments

@AlexThomas90210
Copy link

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`;
@AlexThomas90210 AlexThomas90210 added pending-triage question Further information is requested labels Oct 29, 2024
@ykethan
Copy link
Member

ykethan commented Oct 30, 2024

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-cli Oct 30, 2024
@dpilch
Copy link
Member

dpilch commented Oct 30, 2024

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.

  1. The CfnResource property is private on our wrapper class. You can ignore this with // @ts-expect-error but 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.

  1. 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 dpilch added feature-request New feature or request and removed question Further information is requested pending-triage labels Oct 30, 2024
@AlexThomas90210
Copy link
Author

@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!

@cfernandezruns
Copy link

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?

@dpilch
Copy link
Member

dpilch commented Dec 18, 2024

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/*",
  ]
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants