Page updated Nov 20, 2023

Modify Amplify-generated AWS resources

Amplify GraphQL API uses a variety of auto-generated, underlying AWS services and resources. You can customize these underlying resources to optimize the deployed stack for your specific use case.

In your Amplify app, you can access every underlying resource using CDK "L2" or "L1" constructs. Access the generated resources as L2 constructs via the .resources property on the returned stack or access the generated resources as L1 constructs using the .resources.cfnResources property.

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); const dataResources = backend.resources.data.resources; // Access L2 resources under `.resources` dataResources.tables["Todo"].tableArn; // Access L1 resources under `.resources.cfnResources` Object.values(dataResources.cfnResources.cfnTables).forEach((table) => { table.pointInTimeRecoverySpecification = { pointInTimeRecoveryEnabled: false, }; });
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8const dataResources = backend.resources.data.resources;
9
10// Access L2 resources under `.resources`
11dataResources.tables["Todo"].tableArn;
12
13// Access L1 resources under `.resources.cfnResources`
14Object.values(dataResources.cfnResources.cfnTables).forEach((table) => {
15 table.pointInTimeRecoverySpecification = {
16 pointInTimeRecoveryEnabled: false,
17 };
18});

Customize Amplify-generated AppSync GraphQL API resources

Apply all the customizations on backend.resources.data.resources.graphqlApi or backend.resources.data.resources.cfnResources.cfnGraphqlApi. For example, to enable X-Ray tracing for the AppSync GraphQL API:

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); backend.resources.data.resources.cfnResources.cfnGraphqlApi.xrayEnabled = true;
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8backend.resources.data.resources.cfnResources.cfnGraphqlApi.xrayEnabled = true;

Customize Amplify-generated resources for data models

Apply all the customizations on backend.resources.data.resources.tables["MODEL_NAME"] or backend.resources.data.resources.cfnResources.cfnTables["MODEL_NAME"]. Pass in the model type name into backend.resources.data.resources.cfnResources.cfnTables["MODEL_NAME"] to modify the resources generated for that particular model type. For example, to enable time-to-live on the Todo @model type's DynamoDB table:

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); backend.resources.data.resources.cfnResources.cfnTables['Todo'].timeToLiveSpecification = { attributeName: 'ttl', enabled: true };
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8backend.resources.data.resources.cfnResources.cfnTables['Todo'].timeToLiveSpecification = {
9 attributeName: 'ttl',
10 enabled: true
11};

Example - Configure billing mode on a DynamoDB table

Set the DynamoDB billing mode for the DynamoDB table as either "PROVISIONED" or "PAY_PER_REQUEST".

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); backend.resources.data.resources.cfnResources.cfnTables['Todo'].billingMode = 'PAY_PER_REQUEST';
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8backend.resources.data.resources.cfnResources.cfnTables['Todo'].billingMode = 'PAY_PER_REQUEST';

Example - Configure provisioned throughput for a DynamoDB table

Override the default ProvisionedThroughput provisioned for each model table and its Global Secondary Indexes (GSI). This override is only valid if the "DynamoDBBillingMode" is set to "PROVISIONED".

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); backend.resources.data.resources.cfnResources.cfnTables["Todo"].provisionedThroughput = { readCapacityUnits: 5, writeCapacityUnits: 5, };
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8backend.resources.data.resources.cfnResources.cfnTables["Todo"].provisionedThroughput = {
9 readCapacityUnits: 5,
10 writeCapacityUnits: 5,
11};

Example - Enable point-in-time recovery for a DynamoDB table

Enable/disable DynamoDB point-in-time recovery for each model table.

import { defineBackend } from "@aws-amplify/backend"; import { data } from "./data/resource.js"; const backend = defineBackend({ data, }); backend.resources.data.resources.cfnResources.cfnTables["Todo"].pointInTimeRecoverySpecification = { pointInTimeRecoveryEnabled: true, };
1import { defineBackend } from "@aws-amplify/backend";
2import { data } from "./data/resource.js";
3
4const backend = defineBackend({
5 data,
6});
7
8backend.resources.data.resources.cfnResources.cfnTables["Todo"].pointInTimeRecoverySpecification = {
9 pointInTimeRecoveryEnabled: true,
10};