Search code examples
amazon-web-servicesaws-amplify

How to tag sandbox resources in Amplify Gen 2 (AWS)?


We use new Amplify Gen2 at work, and we have a team of developers where each one creates their sandboxes for particular Amplify project using command like:

npx ampx sandbox --profile aws_viktor_cli --identifier viktor

It works neatly. However, sometimes we need to go to console and check deployed resources, for example DynamoDB tables or AppSync to test queries etc. And because we have many developers we have a hard time to differentiate resources, for example DynamoDB tables from other sandboxes' tables.

I would like to add a tag for my resources but I cannot find how to do it. Right now we filter tables with automatically generated tag like amplify:deployment-type with value sandbox, but we still have too many tables to check.

I checked docs and --help command but honestly couldn't find the solution.


Solution

  • According to the Amplify documentation, you can try to create a local .env file storing the environmental variables which are going to be used in your sandbox.

    You can create a file in your project root folder with the extension .env.local or duplicate your .env file if you have already one, and rename the extension. Then, you should write a new variable with a custom name and value, for example:

    SANDBOX_TAG=MyTag
    

    After that, you should modify your backend file in order to use your new variable as a tag in your deployment:

    import { Tags } from "aws-cdk-lib";
    import { defineBackend } from "@aws-amplify/backend";
    
    const backend = defineBackend({/*whatever you are using in your backend*/});
    const tags = Tags.of(backend.stack);
    tags.add("SANDBOX_TAG", process.env.SANDBOX_TAG /*as string => if you are using typescript*/);
    

    Now that your files are setup, you should run this command in your CLI for deploying your sandbox using your local .env file:

    npx dotenvx run --env-file=.env.local -- ampx sandbox --profile aws_viktor_cli --identifier viktor
    

    Please note that you might need to install dotenvx in your session:

    npm install @dotenvx/dotenvx --save
    

    Also note that your tag will only deploy in your sandbox. Hence, you need to tell your team to use their own local .env file in order to differentiate their deployed resources.

    Please feel free to ask if you have any question.