AWS Mobile Blog

Amplify adds support for multiple environments, custom resolvers, larger data models, and IAM roles including MFA

The Amplify Framework is an open source project for building cloud-enabled applications. Today, we are happy to announce the release of new features in the Amplify Framework.

Multiple Environment Support

The Amplify CLI now supports multiple development environments.

This addition enables you and your team to create and test out features in a sandboxed environment that’s completely separate from your production environment. After new features have been created and tested, you can merge these new features into your primary environment and easily tear down your development environments.

Workflow

Let’s take a look at this workflow.

From within an existing project, you can now initialize a new environment:

amplify env add

When you’re ready to merge the new features into your original environment, you can then check out that environment:

amplify env checkout master // (or whatever your original environment name is).

To see all available env options, you can run the following command:

amplify env --help

To learn more about multiple environment support, see the Amplify Framework documentation.

Custom Resolvers

If you’re not familiar with AWS AppSync or GraphQL, a “resolver” is essentially a function that’s responsible for fetching data from a location to fulfill a request. For instance, a resolver might query from a database to get a stored record, or it could just compute a value directly. Resolvers are attached to fields on a “type” in a GraphQL schema. These are executed at runtime, depending on the request that comes from a client.

When you’re developing GraphQL APIs, you often have to customize the resolvers to perform custom logic and implement things like data manipulation, authorization, or fine-grained access control. We prefer present tense where it works, so you’ll see that I changed various instances of “will” to reflect that.

In the past, you either needed to use the AWS AppSync console or edit AWS CloudFormation templates to implement this logic.

Now, you can update your resolvers locally and use the Amplify CLI to push the updates into your account. Here’s how it works.

  1. After creating your AWS AppSync API, you will now have a new empty folder called resolvers created in your Amplify project in the API folder.
  2. To create a custom resolver, create a file (i.e. Query.getTodo.req.vtl) in the resolvers directory of your API project.
  3. The next time you run amplify push or amplify api gql-compile, your resolver template will be used instead of the auto-generated template. You may similarly create a Query.getTodo.res.vtl file to change the behavior of the resolver’s response mapping template.

To learn more about custom resolvers, see the Amplify Framework documentation.

MFA

The new version of the Amplify framework now supports multi-factor authentication (MFA).

You can configure the Amplify CLI to use an IAM role by defining a profile for the role in the shared ~/.aws/config file. This is similar to how the AWS CLI functions.

When you’re prompted during the execution of the amplify init or amplify configure project command, you can now select a configured profile for the role. The Amplify CLI handles the logic to retrieve, cache, and refresh the temporary credentials.

If MFA is enabled, the CLI prompts you to enter the MFA token code when it needs to retrieve or refresh temporary credentials.

To learn more about Amplify CLI MFA support, see the Amplify Framework documentation.

Larger data models

You might want to write a more specific query against a DynamoDB table that was created by @model. For example, assume that you have this schema with two @model types and a pair of @connection directives.

type Todo @model {
  id: ID!
  name: String!
  description: String
  comments: [Todo] @connection(name: "TodoComments")
}
type Comment @model {
  id: ID!
  content: String
  todo: Todo @connection(name: "TodoComments")
}

This schema generates resolvers for Query.getTodo, Query.listTodos, Query.getComment, and Query.listComments at the top level, as well as for Todo.comments and Comment.todo, to implement the @connection.

Under the hood, the transform creates a GlobalSecondaryIndex (GSI) on the Comment table in DynamoDB. However, it doesn’t generate a top-level query field that queries the GSI because you can fetch the comments for a given todo object by using the Query.getTodo.comments query path.

If you want to fetch all comments for a todo object by using a top-level query field (i.e. Query.commentsForTodo), it’s now possible to define additional custom resolvers directly from the local Amplify project, and push them up using the new nested stacks functionality.

To learn more about nested stacks, see the Amplify Framework documentation.

Migration

We’ve also added support for migrating Amplify projects created using older versions of the CLI into the new version of the CLI.

To migrate to the newest version of the CLI, follow these steps:

  1. Install the newest version of the Amplify CLI – npm install -g @aws-amplify/cli
  2. Run the amplify command
  3. You’re asked a series of questions for migration. Follow the prompt for an easy step-by-step migration.