DEV Community

hirooka kazuya
hirooka kazuya

Posted on

dev diary 20251108

reset the project

I tried to set AWS Amplify Gen2 on existing project with pnpm, but the error sometimes occured in the process and finally decided to reset the project. I gonna start with new project set up with npm.

1.Recreate npm Project / Project Setup
npx create-next-app@latest

2.Install Amplify Backend and Frontend SDKs in the Next.js Project Root Directory
npm install @aws-amplify/backend @aws-amplify/data-schema aws-amplify

3.Create Directories and Files (The commands below do not run the content. Create the folders and files manually as follows.)
mkdir amplify
mkdir amplify/data
touch amplify/backend.ts
touch amplify/data/resource.ts

4.Open the created amplify/backend.ts file and add the following content:

// amplify/backend.ts
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource'; 

/**
 * The Amplify backend definition.
 */
defineBackend({
  data,
  // 認証 (Auth) や関数 (Functions) など、他のリソースが必要になったらここに追加します。
});
Enter fullscreen mode Exit fullscreen mode

5.Open the created amplify/data/resource.ts file and add the following content:

// amplify/data/resource.ts
import { type ClientSchema, a, defineData } from '@aws-amplify/backend';

// 1. データモデルを定義 (シンプルなTodoモデルの例)
const schema = a.schema({
  Todo: a
    .model({
      content: a.string().required(),
      isDone: a.boolean().default(false),
    })
    .authorization([a.allow.owner()]), // データのオーナーのみが操作できる設定の例
});

// 2. defineDataでデータソースをPostgreSQLに設定
export const data = defineData({
  schema,
  authorization: {
    defaultAuthorization: {
      mode: 'apiKey', // 開発初期にはAPIキーを使用
    },
  },
  engine: {
    // ⭐️ データソースとして RDS (PostgreSQL) を指定
    database: {
      instanceType: 'db.t3.medium', // RDSインスタンスのサイズ (要件に応じて変更可能)
      engine: 'postgres', // データベースエンジン
    },
  },
});

export type Schema = ClientSchema<typeof schema>;
Enter fullscreen mode Exit fullscreen mode

6.install AWS CLI on https://awscli.amazonaws.com/AWSCLIV2.msi

7.confirm AWS CLI
aws --version

8.Set up authentication credentials
'aws configure'

AWS Access Key ID: あなたのアクセスキー
AWS Secret Access Key: あなたのシークレットキ
Default region name: 例 ap-northeast-1 (東京)
Default output format: json (Enter でOK)
Enter fullscreen mode Exit fullscreen mode

9.When you run this command (sandbox) in the project's root directory, resources such as a VPC, an RDS (PostgreSQL) instance, an AppSync API, and Lambda Functions will be automatically provisioned/created.
npx @aws-amplify/backend-cli sandbox

10.open aws account automatically and click 'initialize'
wait for a while.
on project root directory, the file'amplify_outputs.json' are made. the folder `.amplify' isn't built and it's no problem.

to be continued tomorrow.

Top comments (0)