Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 30, 2024

Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. New project should use Amplify Gen 2. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade. Switch to the latest Gen 2 docs →

CloudFormationを使用してカスタムAWSリソースを追加する

Amplify CLIは、AWS CloudFormationを使用してカスタムAWSリソースを追加する機能を提供します。Amplifyプロジェクトでamplify add customコマンドを実行すると、CloudFormationスターターテンプレートと他のAmplify生成リソースを参照するメカニズムが提供されます。

AWS CloudFormationを使用してカスタムAWSリソースを追加するには、次のコマンドを実行します。

amplify add custom
? How do you want to define this custom resource? … (Use arrow keys or type to filter)
AWS CDK
❯ AWS CloudFormation

スケルトンCloudFormationテンプレートは、amplify/backend/custom/<resource-name>ディレクトリに生成されます。追加のAWSリソースは、<resource-name>-cloudformation-template.jsonCloudFormationテンプレートファイルで定義できます。

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"env": {
"Type": "String"
}
},
"Resources": {
"snstopic1234": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": {
"Fn::Join": [ "", [ "sns-topic-1234-", { "Ref": "env" } ] ]
}
}
},
"snstopicemailsub1234": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Protocol": "email",
"TopicArn": {
"Ref": "snstopic1234"
},
"Endpoint": "<your-email-address>"
}
}
},
"Outputs": {}
}

注: Amplifyのマルチ環境ワークフローで正しく動作するようにするために、常にAmplify環境名(envパラメータ)をリソース名に追加してください。

Amplifyバックエンド名の参照

Amplify CLIは、CloudFormationテンプレートの上部にあるenvパラメータを自動的に設定します。

...
"Parameters": {
"env": {
"Type": "String"
}
},
...

パラメータを参照するには、Ref組み込み関数を使用します。

...
"Resources": {
"snstopic1234": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": {
"Fn::Join": [ "", [ "sns-topic-1234-", { "Ref": "env" } ] ]
}
}
}
}
...

Amplify生成リソースの参照

カスタムAWSリソースのCloudFormationテンプレートは、Amplify生成リソースのCloudFormation出力を参照できます。別のリソースの出力を参照するには、次を実行します。

amplify update custom
✔ Do you want to access Amplify generated resources in your custom CloudFormation file? (y/N) · yes
? Select the categories you want this custom resource to have access to:
❯ <category>
? Function has 2 resources in this project. Select the one you would like your custom resource to access
❯ ...

リソースが選択されると、CloudFormationテンプレートの上部にパラメータとしてリストされているリソース出力を参照できます。パラメータを参照するには、Ref組み込み関数を使用します。

...
"Parameters": {
...,
"myFunctionArn": {
"Type": "String"
}
},
"Resources": {
...: {
"Type": ...,
"Properties": {
...: { "Ref": "myFunctionArn"}
}
}
}
...