あめがえるのITブログ

頑張りすぎない。ほどほどに頑張るブログ。

AWS AmplifyのBackend(api)を作成してみた


やること

AmplifyのBackendでAPIGatewayとLambdaを作成し、接続する。

前提

  • 事前にAmplifyのアプリケーションが作成されていること
  • 接続元の端末はWindows11を使用してます

実践!

1.amplify add apiコマンド実行
1-1.PowerShellを開く
1-2.amplifyローカルフォルダへ移動し、下記コマンドを実行

> amplify add api

1-3.対話形式で回答する

> amplify add api
? Select from one of the below mentioned services: REST ★RESTAPIを指定
√ Provide a friendly name for your resource to be used as a label for this category in the project: · api9bb60a4c ★デフォルト名を使用するためEnter
√ Provide a path (e.g., /book/{isbn}): · /items ★デフォルト名を使用するためEnter
Only one option for [Choose a Lambda source]. Selecting [Create a new Lambda function]. ★デフォルト名で、[Enter]
? Provide an AWS Lambda function name: amplify6d148998 ★デフォルト名で、[Enter]
? Choose the runtime that you want to use: NodeJS ★Node.jsを選択し、[Enter]
? Choose the function template that you want to use: Hello World ★とりあえず Hello Worldを選択し、[Enter]

✅ Available advanced settings: 
- Resource access permissions
- Scheduled recurring invocation
- Lambda layers configuration
- Environment variables configuration
- Secret values configuration

? Do you want to configure advanced settings? No ★追加設定は不要なので、[No]
? Do you want to edit the local lambda function now? No ★変更不要なので、[No]
✅ Successfully added resource amplify6d148998 locally.

✅ Next steps:
Check out sample function code generated in <project-dir>/amplify/backend/function/amplify6d148998/src
"amplify function build" builds all of your functions currently in the project
"amplify mock function <functionName>" runs your function locally
To access AWS resources outside of this Amplify app, edit the C:\amplify\amplify\backend\function\amplify6d148998\custom-policies.json
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
✅ Succesfully added the Lambda function locally
√ Restrict API access? (Y/n) · no ★APIアクセスを制限はしないので、[No]
√ Do you want to add another path? (y/N) · no ★追加パスは設定しないので、[No]
✅ Successfully added resource api9bb60a4c locally

✅ Some next steps:
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

1-4.下記を実行

amplify push
> amplify push
√ Successfully pulled backend environment dev from the cloud.

    Current Environment: dev

┌──────────┬─────────────────┬───────────┬───────────────────┐
│ Category │ Resource name   │ Operation │ Provider plugin   │
├──────────┼─────────────────┼───────────┼───────────────────┤
│ Function │ amplify6d148998 │ Create    │ awscloudformation │
├──────────┼─────────────────┼───────────┼───────────────────┤
│ Api      │ api9bb60a4c     │ Create    │ awscloudformation │
└──────────┴─────────────────┴───────────┴───────────────────┘
√ Are you sure you want to continue? (Y/n) · yes

Deployment completed.
Deployed root stack amplify [ ======================================== ] 3/3
        amplify-amplify-dev-5fd03      AWS::CloudFormation::Stack     UPDATE_COMPLETE                Sun Jun 09 2024
        functionamplify6d148998        AWS::CloudFormation::Stack     CREATE_COMPLETE                Sun Jun 09 2024
        apiapi9bb60a4c                 AWS::CloudFormation::Stack     CREATE_COMPLETE                Sun Jun 09 2024
Deployed function amplify6d148998 [ ======================================== ] 3/3
        LambdaExecutionRole            AWS::IAM::Role                 CREATE_COMPLETE                Sun Jun 09 2024
        LambdaFunction                 AWS::Lambda::Function          CREATE_COMPLETE                Sun Jun 09 2024
        lambdaexecutionpolicy          AWS::IAM::Policy               CREATE_IN_PROGRESS             Sun Jun 09 2024
Deployed api api9bb60a4c [ ======================================== ] 5/5
        api9bb60a4c                    AWS::ApiGateway::RestApi       CREATE_IN_PROGRESS             Sun Jun 09 2024

Deployment state saved successfully.

REST API endpoint: https://wsj28k4844.execute-api.ap-northeast-1.amazonaws.com/dev


2.作成確認
2-1.APIGateway作成確認
 2-1-1.[AWS] - [APIGateway] - [API]
 2-1-2.APIが作成されていることを確認

2-2.Lambda作成確認
 2-2-1.[AWS] - [Lambda]
 2-2-2.関数が作成されていることを確認

2-3.Amplify確認
 2-3-1.[AWS] - [Amplify] - 該当アプリケーションを選択

 2-3-2.[dev]を選択

 2-3-3.[api]タブを選択。
  ※なにも表示されない。。。正解がわからない。。。


3.接続確認
3-1.amplify pushの最後に表示された[REST API endpoint]にpathを付与してブラウザから接続
※デフォルトpathは/items

https://wsj28k4844.execute-api.ap-northeast-1.amazonaws.com/dev/items

3-2.["Hello from Lambda!"]が表示されることを確認



感想

Amplifyに表示されないのは正解なのか。。。
まだAmplifyさんとは友達になれそうにない。。。