

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 [AWS](https://github.com/awsdocs/aws-doc-sdk-examples)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS SDKs CloudFormation を使用するためのコード例
<a name="cloudformation_code_examples"></a>

次のコード例は、 AWS Software Development Kit (SDK) AWS CloudFormation で を使用する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

*シナリオ*は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

**その他のリソース**
+  **[CloudFormation ユーザーガイド](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)** – 詳細については、「」を参照してください CloudFormation。
+ **[CloudFormation API リファレンス](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/Welcome.html)** – 使用可能なすべての CloudFormation アクションに関する詳細。
+ **[AWS デベロッパーセンター](https://aws.amazon.com/developer/code-examples/?awsf.sdk-code-examples-product=product%23cloudformation)** – カテゴリまたは全文検索でフィルタリングできるコード例。
+ **[AWS SDK の例](https://github.com/awsdocs/aws-doc-sdk-examples)** – 完全なコードを優先言語で含む GitHub リポジトリ。コードの設定と実行に関する説明が記載されています。

**Contents**
+ [基本](cloudformation_code_examples_basics.md)
  + [こんにち CloudFormationは](cloudformation_example_cloudformation_Hello_section.md)
  + [アクション](cloudformation_code_examples_actions.md)
    + [`CancelUpdateStack`](cloudformation_example_cloudformation_CancelUpdateStack_section.md)
    + [`ContinueUpdateRollback`](cloudformation_example_cloudformation_ContinueUpdateRollback_section.md)
    + [`CreateStack`](cloudformation_example_cloudformation_CreateStack_section.md)
    + [`DeleteStack`](cloudformation_example_cloudformation_DeleteStack_section.md)
    + [`DescribeStackEvents`](cloudformation_example_cloudformation_DescribeStackEvents_section.md)
    + [`DescribeStackResource`](cloudformation_example_cloudformation_DescribeStackResource_section.md)
    + [`DescribeStackResources`](cloudformation_example_cloudformation_DescribeStackResources_section.md)
    + [`DescribeStacks`](cloudformation_example_cloudformation_DescribeStacks_section.md)
    + [`EstimateTemplateCost`](cloudformation_example_cloudformation_EstimateTemplateCost_section.md)
    + [`GetTemplate`](cloudformation_example_cloudformation_GetTemplate_section.md)
    + [`ListStackResources`](cloudformation_example_cloudformation_ListStackResources_section.md)
    + [`ListStacks`](cloudformation_example_cloudformation_ListStacks_section.md)
    + [`UpdateStack`](cloudformation_example_cloudformation_UpdateStack_section.md)
    + [`ValidateTemplate`](cloudformation_example_cloudformation_ValidateTemplate_section.md)
+ [シナリオ](cloudformation_code_examples_scenarios.md)
  + [COVID-19 データを追跡する REST API を作成する](cloudformation_example_cross_ApiGatewayDataTracker_section.md)

# AWS SDKs CloudFormation を使用するための基本的な例
<a name="cloudformation_code_examples_basics"></a>

次のコード例は、 SDKs AWS CloudFormation で AWS の基本を使用する方法を示しています。

**Contents**
+ [こんにち CloudFormationは](cloudformation_example_cloudformation_Hello_section.md)
+ [アクション](cloudformation_code_examples_actions.md)
  + [`CancelUpdateStack`](cloudformation_example_cloudformation_CancelUpdateStack_section.md)
  + [`ContinueUpdateRollback`](cloudformation_example_cloudformation_ContinueUpdateRollback_section.md)
  + [`CreateStack`](cloudformation_example_cloudformation_CreateStack_section.md)
  + [`DeleteStack`](cloudformation_example_cloudformation_DeleteStack_section.md)
  + [`DescribeStackEvents`](cloudformation_example_cloudformation_DescribeStackEvents_section.md)
  + [`DescribeStackResource`](cloudformation_example_cloudformation_DescribeStackResource_section.md)
  + [`DescribeStackResources`](cloudformation_example_cloudformation_DescribeStackResources_section.md)
  + [`DescribeStacks`](cloudformation_example_cloudformation_DescribeStacks_section.md)
  + [`EstimateTemplateCost`](cloudformation_example_cloudformation_EstimateTemplateCost_section.md)
  + [`GetTemplate`](cloudformation_example_cloudformation_GetTemplate_section.md)
  + [`ListStackResources`](cloudformation_example_cloudformation_ListStackResources_section.md)
  + [`ListStacks`](cloudformation_example_cloudformation_ListStacks_section.md)
  + [`UpdateStack`](cloudformation_example_cloudformation_UpdateStack_section.md)
  + [`ValidateTemplate`](cloudformation_example_cloudformation_ValidateTemplate_section.md)

# こんにち CloudFormationは
<a name="cloudformation_example_cloudformation_Hello_section"></a>

次のコード例は、 CloudFormationの使用を開始する方法を示しています。

------
#### [ .NET ]

**SDK for .NET (v4)**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv4/CloudFormation#code-examples)での設定と実行の方法を確認してください。

```
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
using Amazon.Runtime;

namespace CloudFormationActions;

public static class HelloCloudFormation
{
    public static IAmazonCloudFormation _amazonCloudFormation = null!;

    static async Task Main(string[] args)
    {
        // Create the CloudFormation client
        _amazonCloudFormation = new AmazonCloudFormationClient();
        Console.WriteLine($"\nIn Region: {_amazonCloudFormation.Config.RegionEndpoint}");

        // List the resources for each stack
        await ListResources();
    }

    /// <summary>
    /// Method to list stack resources and other information.
    /// </summary>
    /// <returns>True if successful.</returns>
    public static async Task<bool> ListResources()
    {
        try
        {
            Console.WriteLine("Getting CloudFormation stack information...");

            // Get all stacks using the stack paginator.
            var paginatorForDescribeStacks =
                _amazonCloudFormation.Paginators.DescribeStacks(
                    new DescribeStacksRequest());
            if (paginatorForDescribeStacks.Stacks != null)
            {
                await foreach (Stack stack in paginatorForDescribeStacks.Stacks)
                {
                    // Basic information for each stack
                    Console.WriteLine(
                        "\n------------------------------------------------");
                    Console.WriteLine($"\nStack: {stack.StackName}");
                    Console.WriteLine($"  Status: {stack.StackStatus.Value}");
                    Console.WriteLine($"  Created: {stack.CreationTime}");

                    // The tags of each stack (etc.)
                    if (stack.Tags != null && stack.Tags.Count > 0)
                    {
                        Console.WriteLine("  Tags:");
                        foreach (Tag tag in stack.Tags)
                            Console.WriteLine($"    {tag.Key}, {tag.Value}");
                    }

                    // The resources of each stack
                    DescribeStackResourcesResponse responseDescribeResources =
                        await _amazonCloudFormation.DescribeStackResourcesAsync(
                            new DescribeStackResourcesRequest
                            {
                                StackName = stack.StackName
                            });
                    if (responseDescribeResources.StackResources != null && responseDescribeResources.StackResources.Count > 0)
                    {
                        Console.WriteLine("  Resources:");
                        foreach (StackResource resource in responseDescribeResources
                                     .StackResources)
                            Console.WriteLine(
                                $"    {resource.LogicalResourceId}: {resource.ResourceStatus}");
                    }
                }
            }

            Console.WriteLine("\n------------------------------------------------");
            return true;
        }
        catch (AmazonCloudFormationException ex)
        {
            Console.WriteLine("Unable to get stack information:\n" + ex.Message);
            return false;
        }
        catch (AmazonServiceException ex)
        {
            if (ex.Message.Contains("Unable to get IAM security credentials"))
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("If you are usnig SSO, be sure to install" +
                                  " the AWSSDK.SSO and AWSSDK.SSOOIDC packages.");
            }
            else
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            return false;
        }
        catch (ArgumentNullException ex)
        {
            if (ex.Message.Contains("Options property cannot be empty: ClientName"))
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("If you are using SSO, have you logged in?");
            }
            else
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            return false;
        }
    }
```
+  API の詳細については、「*AWS SDK for .NET API リファレンス*」の「[DescribeStackResources](https://docs.aws.amazon.com/goto/DotNetSDKV4/cloudformation-2010-05-15/DescribeStackResources)」を参照してください。

------

# AWS SDKs CloudFormation を使用するためのアクション
<a name="cloudformation_code_examples_actions"></a>

次のコード例は、 AWS SDKs で個々の CloudFormation アクションを実行する方法を示しています。それぞれの例には、GitHub へのリンクがあり、そこにはコードの設定と実行に関する説明が記載されています。

これらの抜粋は CloudFormation API を呼び出し、コンテキスト内で実行する必要がある大規模なプログラムからのコード抜粋です。アクションは [AWS SDKs CloudFormation を使用するシナリオ](cloudformation_code_examples_scenarios.md) のコンテキスト内で確認できます。

 以下の例には、最も一般的に使用されるアクションのみ含まれています。詳細な一覧については、「[AWS CloudFormation API リファレンス](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/Welcome.html)」を参照してください。

**Topics**
+ [`CancelUpdateStack`](cloudformation_example_cloudformation_CancelUpdateStack_section.md)
+ [`ContinueUpdateRollback`](cloudformation_example_cloudformation_ContinueUpdateRollback_section.md)
+ [`CreateStack`](cloudformation_example_cloudformation_CreateStack_section.md)
+ [`DeleteStack`](cloudformation_example_cloudformation_DeleteStack_section.md)
+ [`DescribeStackEvents`](cloudformation_example_cloudformation_DescribeStackEvents_section.md)
+ [`DescribeStackResource`](cloudformation_example_cloudformation_DescribeStackResource_section.md)
+ [`DescribeStackResources`](cloudformation_example_cloudformation_DescribeStackResources_section.md)
+ [`DescribeStacks`](cloudformation_example_cloudformation_DescribeStacks_section.md)
+ [`EstimateTemplateCost`](cloudformation_example_cloudformation_EstimateTemplateCost_section.md)
+ [`GetTemplate`](cloudformation_example_cloudformation_GetTemplate_section.md)
+ [`ListStackResources`](cloudformation_example_cloudformation_ListStackResources_section.md)
+ [`ListStacks`](cloudformation_example_cloudformation_ListStacks_section.md)
+ [`UpdateStack`](cloudformation_example_cloudformation_UpdateStack_section.md)
+ [`ValidateTemplate`](cloudformation_example_cloudformation_ValidateTemplate_section.md)

# CLI で `CancelUpdateStack` を使用する
<a name="cloudformation_example_cloudformation_CancelUpdateStack_section"></a>

次のサンプルコードは、`CancelUpdateStack` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**進行中のスタック更新をキャンセルする方法**  
以下の `cancel-update-stack` コマンドは、`myteststack` スタックで行われているスタック更新をキャンセルします。  

```
aws cloudformation cancel-update-stack --stack-name myteststack
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CancelUpdateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/cancel-update-stack.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックで行われている更新をキャンセルします。**  

```
Stop-CFNUpdateStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[CancelUpdateStack](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックで行われている更新をキャンセルします。**  

```
Stop-CFNUpdateStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[CancelUpdateStack](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `ContinueUpdateRollback` を使用する
<a name="cloudformation_example_cloudformation_ContinueUpdateRollback_section"></a>

次のサンプルコードは、`ContinueUpdateRollback` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**更新のロールバックを再試行する**  
以下の `continue-update-rollback` の例は、前回失敗したスタック更新からのロールバック操作を再開します。  

```
aws cloudformation continue-update-rollback \
    --stack-name my-stack
```
このコマンドでは何も出力されません。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ContinueUpdateRollback](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/continue-update-rollback.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックのロールバックを続行します。このスタックの状態は「UPDATE\$1ROLLBACK\$1FAILED」になっている必要があります。続行されたロールバックが成功すると、スタックの状態が「UPDATE\$1ROLLBACK\$1COMPLETE」になります。**  

```
Resume-CFNUpdateRollback -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[ContinueUpdateRollback](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックのロールバックを続行します。このスタックの状態は「UPDATE\$1ROLLBACK\$1FAILED」になっている必要があります。続行されたロールバックが成功すると、スタックの状態が「UPDATE\$1ROLLBACK\$1COMPLETE」になります。**  

```
Resume-CFNUpdateRollback -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[ContinueUpdateRollback](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `CreateStack` を使用する
<a name="cloudformation_example_cloudformation_CreateStack_section"></a>

次のサンプルコードは、`CreateStack` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation スタックを作成するには**  
次の `create-stacks` コマンドは、`sampletemplate.json` テンプレートを使用して「`myteststack`」という名称のスタックを作成します。  

```
aws cloudformation create-stack --stack-name myteststack --template-body file://sampletemplate.json --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2
```
出力:  

```
{
    "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896"
}
```
詳細については、「AWS CloudFormation ユーザーガイド」の「スタック」を参照してください。**  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CreateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/create-stack.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (「PK1」と「PK2」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」と「PV2」はこれらのパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗しても、スタックはロールバックされません。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) `
             -DisableRollback $true
```
**例 2: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (「PK1」と「PK2」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」と「PV2」はこれらのパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "PK1"
$p1.ParameterValue = "PV1"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "PK2"
$p2.ParameterValue = "PV2"

New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( $p1, $p2 ) `
             -OnFailure "ROLLBACK"
```
**例 3: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、Amazon S3 URL から取得されます (「PK1」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」はパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます (-DisableRollback \$1false を指定する場合と同じです)**。  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 4: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、Amazon S3 URL から取得されます (「PK1」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」はパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます (-DisableRollback \$1false を指定する場合と同じです)。指定された通知 AEN は、公開されたスタック関連のイベントを受け取ります。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } `
             -NotificationARN @( "arn1", "arn2" )
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[CreateStack](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (「PK1」と「PK2」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」と「PV2」はこれらのパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗しても、スタックはロールバックされません。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) `
             -DisableRollback $true
```
**例 2: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (「PK1」と「PK2」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」と「PV2」はこれらのパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "PK1"
$p1.ParameterValue = "PV1"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "PK2"
$p2.ParameterValue = "PV2"

New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( $p1, $p2 ) `
             -OnFailure "ROLLBACK"
```
**例 3: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、Amazon S3 URL から取得されます (「PK1」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」はパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます (-DisableRollback \$1false を指定する場合と同じです)**。  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 4: 指定された名前を使用して新しいスタックを作成します。テンプレートは、カスタマイゼーションパラメータを使用して、Amazon S3 URL から取得されます (「PK1」はテンプレートコンテンツで宣言されたパラメータの名前を表し、「PV1」はパラメータの値を表します)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。スタックの作成に失敗すると、スタックがロールバックされます (-DisableRollback \$1false を指定する場合と同じです)。指定された通知 AEN は、公開されたスタック関連のイベントを受け取ります。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } `
             -NotificationARN @( "arn1", "arn2" )
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[CreateStack](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `DeleteStack` を使用する
<a name="cloudformation_example_cloudformation_DeleteStack_section"></a>

次のサンプルコードは、`DeleteStack` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**スタックを削除する方法**  
以下の `delete-stack` 例は、指定されたスタックを削除します。  

```
aws cloudformation delete-stack \
    --stack-name my-stack
```
このコマンドでは何も出力されません。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DeleteStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/delete-stack.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックを削除します。**  

```
Remove-CFNStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[DeleteStack](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックを削除します。**  

```
Remove-CFNStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[DeleteStack](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `DescribeStackEvents` を使用する
<a name="cloudformation_example_cloudformation_DescribeStackEvents_section"></a>

次のサンプルコードは、`DescribeStackEvents` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**スタックイベントを記述する方法**  
次の `describe-stack-events` の例は、指定されたスタックの最新イベントを 2 件表示します。  

```
aws cloudformation describe-stack-events \
    --stack-name my-stack \
    --max-items 2

{
    "StackEvents": [
        {
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "EventId": "4e1516d0-e4d6-xmpl-b94f-0a51958a168c",
            "StackName": "my-stack",
            "LogicalResourceId": "my-stack",
            "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2019-10-02T05:34:29.556Z",
            "ResourceStatus": "UPDATE_COMPLETE"
        },
        {
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "EventId": "4dd3c810-e4d6-xmpl-bade-0aaf8b31ab7a",
            "StackName": "my-stack",
            "LogicalResourceId": "my-stack",
            "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2019-10-02T05:34:29.127Z",
            "ResourceStatus": "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS"
        }
    ],
    "NextToken": "eyJOZXh0VG9XMPLiOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAyfQ=="
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DescribeStackEvents](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-events.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックに関するすべてのスタック関連イベントを返します。**  

```
Get-CFNStackEvent -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[DescribeStackEvents](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックに関するすべてのスタック関連イベントを返します。**  

```
Get-CFNStackEvent -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[DescribeStackEvents](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `DescribeStackResource` を使用する
<a name="cloudformation_example_cloudformation_DescribeStackResource_section"></a>

次のサンプルコードは、`DescribeStackResource` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**リソースに関する情報を取得する方法**  
次の `describe-stack-resource` の例は、指定されたスタック内にある「`MyFunction`」という名称のリソースに関する詳細情報を表示します。  

```
aws cloudformation describe-stack-resource \
    --stack-name MyStack \
    --logical-resource-id MyFunction
```
出力:  

```
{
    "StackResourceDetail": {
        "StackName": "MyStack",
        "StackId": "arn:aws:cloudformation:us-east-2:123456789012:stack/MyStack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
        "LogicalResourceId": "MyFunction",
        "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
        "ResourceType": "AWS::Lambda::Function",
        "LastUpdatedTimestamp": "2019-10-02T05:34:27.989Z",
        "ResourceStatus": "UPDATE_COMPLETE",
        "Metadata": "{}",
        "DriftInformation": {
            "StackResourceDriftStatus": "IN_SYNC"
        }
    }
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DescribeStackResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-resource.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 論理 ID「MyDBInstance」を使用して、指定されたスタックに関連付けられているテンプレートで識別されたリソースの説明を返します。**  

```
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[DescribeStackResource](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 論理 ID「MyDBInstance」を使用して、指定されたスタックに関連付けられているテンプレートで識別されたリソースの説明を返します。**  

```
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[DescribeStackResource](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `DescribeStackResources` を使用する
<a name="cloudformation_example_cloudformation_DescribeStackResources_section"></a>

次のサンプルコードは、`DescribeStackResources` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**リソースに関する情報を取得する方法**  
次の `describe-stack-resources` 例は、指定されたスタック内にあるリソースの詳細情報を表示します。  

```
aws cloudformation describe-stack-resources \
    --stack-name my-stack
```
出力:  

```
{
    "StackResources": [
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "bucket",
            "PhysicalResourceId": "my-stack-bucket-1vc62xmplgguf",
            "ResourceType": "AWS::S3::Bucket",
            "Timestamp": "2019-10-02T04:34:11.345Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "function",
            "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
            "ResourceType": "AWS::Lambda::Function",
            "Timestamp": "2019-10-02T05:34:27.989Z",
            "ResourceStatus": "UPDATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "functionRole",
            "PhysicalResourceId": "my-functionRole-HIZXMPLEOM9E",
            "ResourceType": "AWS::IAM::Role",
            "Timestamp": "2019-10-02T04:34:06.350Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        }
    ]
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DescribeStackResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-resources.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックに関連付けられた最大 100 個の AWS リソースのリソースの説明を返します。スタックに関連付けられているすべてのリソースの詳細情報を取得するには、Get-CFNStackResourceSummary を使用します。これは、結果の手動ページングもサポートしています。**  

```
Get-CFNStackResourceList -StackName "myStack"
```
**例 2: 論理 ID「Ec2Instance」を使用して、指定されたスタックに関連付けられているテンプレートで識別された Amazon EC2 インスタンスの説明を返します。**  

```
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"
```
**例 3: インスタンス ID「i-123456」で識別される Amazon EC2 インスタンスが含まれるスタックに関連付けられた、最大 100 個のリソースの説明を返します。スタックに関連付けられているすべてのリソースの詳細情報を取得するには、Get-CFNStackResourceSummary を使用します。これは、結果の手動ページングもサポートしています。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456"
```
**例 4: スタックのテンプレートで論理 ID「Ec2Instance」によって識別される Amazon EC2 インスタンスの説明を返します。スタックは、スタックに含まれるリソースの物理リソース ID を使用して識別され、この場合はインスタンス ID が「i-123456」の Amazon EC2 インスタンスも含まれます。テンプレートのコンテンツによっては、スタックの識別に異なる物理リソース (Amazon S3 バケットなど) を使用することもできます。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[DescribeStackResources](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックに関連付けられた最大 100 個の AWS リソースのリソースの説明を返します。スタックに関連付けられているすべてのリソースの詳細情報を取得するには、Get-CFNStackResourceSummary を使用します。これは、結果の手動ページングもサポートしています。**  

```
Get-CFNStackResourceList -StackName "myStack"
```
**例 2: 論理 ID「Ec2Instance」を使用して、指定されたスタックに関連付けられているテンプレートで識別された Amazon EC2 インスタンスの説明を返します。**  

```
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"
```
**例 3: インスタンス ID「i-123456」で識別される Amazon EC2 インスタンスが含まれるスタックに関連付けられた、最大 100 個のリソースの説明を返します。スタックに関連付けられているすべてのリソースの詳細情報を取得するには、Get-CFNStackResourceSummary を使用します。これは、結果の手動ページングもサポートしています。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456"
```
**例 4: スタックのテンプレートで論理 ID「Ec2Instance」によって識別される Amazon EC2 インスタンスの説明を返します。スタックは、スタックに含まれるリソースの物理リソース ID を使用して識別され、この場合はインスタンス ID が「i-123456」の Amazon EC2 インスタンスも含まれます。テンプレートのコンテンツによっては、スタックの識別に異なる物理リソース (Amazon S3 バケットなど) を使用することもできます。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[DescribeStackResources](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# AWS SDK または CLI `DescribeStacks`で を使用する
<a name="cloudformation_example_cloudformation_DescribeStacks_section"></a>

次のサンプルコードは、`DescribeStacks` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation スタックを記述するには**  
以下の `describe-stacks` 例は、`myteststack` スタックの概要情報を表示します。  

```
aws cloudformation describe-stacks --stack-name myteststack
```
出力:  

```
{
    "Stacks":  [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896",
            "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
            "Tags": [],
            "Outputs": [
                {
                    "Description": "Name of S3 bucket to hold website content",
                    "OutputKey": "BucketName",
                    "OutputValue": "myteststack-s3bucket-jssofi1zie2w"
                }
            ],
            "StackStatusReason": null,
            "CreationTime": "2013-08-23T01:02:15.422Z",
            "Capabilities": [],
            "StackName": "myteststack",
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        }
    ]
}
```
詳細については、「*AWS CloudFormation ユーザーガイド*」の「スタック」を参照してください。  
+  API の詳細については、「AWS CLI コマンドリファレンス」の「[DescribeStacks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stacks.html)」を参照してください。

------
#### [ Go ]

**SDK for Go V2**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/workflows/user_pools_and_lambda_triggers#code-examples)での設定と実行の方法を確認してください。

```
import (
	"context"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/cloudformation"
)

// StackOutputs defines a map of outputs from a specific stack.
type StackOutputs map[string]string

type CloudFormationActions struct {
	CfnClient *cloudformation.Client
}

// GetOutputs gets the outputs from a CloudFormation stack and puts them into a structured format.
func (actor CloudFormationActions) GetOutputs(ctx context.Context, stackName string) StackOutputs {
	output, err := actor.CfnClient.DescribeStacks(ctx, &cloudformation.DescribeStacksInput{
		StackName: aws.String(stackName),
	})
	if err != nil || len(output.Stacks) == 0 {
		log.Panicf("Couldn't find a CloudFormation stack named %v. Here's why: %v\n", stackName, err)
	}
	stackOutputs := StackOutputs{}
	for _, out := range output.Stacks[0].Outputs {
		stackOutputs[*out.OutputKey] = *out.OutputValue
	}
	return stackOutputs
}
```
+  API の詳細については、「*AWS SDK for Go API リファレンス*」の「[DescribeStacks](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/cloudformation#Client.DescribeStacks)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: ユーザーのスタックのすべてを記述するスタックインスタンスのコレクションを返します。**  

```
Get-CFNStack
```
**例 2: 指定されたスタックを記述するスタックインスタンスを返します。**  

```
Get-CFNStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[DescribeStacks](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: ユーザーのスタックのすべてを記述するスタックインスタンスのコレクションを返します。**  

```
Get-CFNStack
```
**例 2: 指定されたスタックを記述するスタックインスタンスを返します。**  

```
Get-CFNStack -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[DescribeStacks](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `EstimateTemplateCost` を使用する
<a name="cloudformation_example_cloudformation_EstimateTemplateCost_section"></a>

次のサンプルコードは、`EstimateTemplateCost` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**テンプレートコストを見積もる方法**  
以下の `estimate-template-cost` の例は、現在のフォルダ内にある「`template.yaml`」という名称のテンプレートに関するコスト見積もりを生成します。  

```
aws cloudformation estimate-template-cost \
    --template-body file://template.yaml
```
出力:  

```
{
    "Url": "http://calculator.s3.amazonaws.com/calc5.html?key=cloudformation/7870825a-xmpl-4def-92e7-c4f8dd360cca"
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[EstimateTemplateCost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/estimate-template-cost.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは指定された Amazon S3 URL から取得され、単一のカスタマイゼーションパラメータが適用されます。パラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Measure-CFNTemplateCost -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                        -Region us-west-1 `
                        -Parameter @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" }
```
**例 2: テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは提供されたコンテンツから解析され、カスタマイゼーションパラメータが適用されます (この例は、テンプレートコンテンツが「KeyName」と「InstanceType」の 2 つのパラメータを宣言していることを前提としています)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" `
                        -Parameter @( @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" },`
                                      @{ ParameterKey="InstanceType"; ParameterValue="m1.large" })
```
**例 3: New-Object を使用してテンプレートパラメータのセットを構築し、テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (この例は、テンプレートコンテンツが「KeyName」と「InstanceType」の 2 つのパラメータを宣言していることを前提としています)。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "KeyName"
$p1.ParameterValue = "myKeyPairName"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "InstanceType"
$p2.ParameterValue = "m1.large"

Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" -Parameter @( $p1, $p2 )
```
+  API の詳細については、「*AWS Tools for PowerShell コマンドレットリファレンス (V4)*」の「[EstimateTemplateCost](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは指定された Amazon S3 URL から取得され、単一のカスタマイゼーションパラメータが適用されます。パラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Measure-CFNTemplateCost -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                        -Region us-west-1 `
                        -Parameter @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" }
```
**例 2: テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは提供されたコンテンツから解析され、カスタマイゼーションパラメータが適用されます (この例は、テンプレートコンテンツが「KeyName」と「InstanceType」の 2 つのパラメータを宣言していることを前提としています)。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" `
                        -Parameter @( @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" },`
                                      @{ ParameterKey="InstanceType"; ParameterValue="m1.large" })
```
**例 3: New-Object を使用してテンプレートパラメータのセットを構築し、テンプレートの実行に必要なリソースを記述するクエリ文字列を含む AWS Simple Monthly Calculator URL を返します。テンプレートは、カスタマイゼーションパラメータを使用して、提供されたコンテンツから解析されます (この例は、テンプレートコンテンツが「KeyName」と「InstanceType」の 2 つのパラメータを宣言していることを前提としています)。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "KeyName"
$p1.ParameterValue = "myKeyPairName"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "InstanceType"
$p2.ParameterValue = "m1.large"

Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" -Parameter @( $p1, $p2 )
```
+  API の詳細については、「*AWS Tools for PowerShell コマンドレットリファレンス (V5)*」の「[EstimateTemplateCost](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `GetTemplate` を使用する
<a name="cloudformation_example_cloudformation_GetTemplate_section"></a>

次のサンプルコードは、`GetTemplate` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation スタックのテンプレート本文を表示するには**  
以下の `get-template` の例は、`myteststack` スタックのテンプレートを示しています。  

```
aws cloudformation get-template --stack-name myteststack
```
出力:  

```
{
    "TemplateBody": {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Outputs": {
            "BucketName": {
                "Description": "Name of S3 bucket to hold website content",
                "Value": {
                    "Ref": "S3Bucket"
                }
            }
        },
        "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
        "Resources": {
            "S3Bucket": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": "PublicRead"
                }
            }
        }
    }
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[GetTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/get-template.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックに関連付けられているテンプレートを返します。**  

```
Get-CFNTemplate -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[GetTemplate](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックに関連付けられているテンプレートを返します。**  

```
Get-CFNTemplate -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[GetTemplate](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `ListStackResources` を使用する
<a name="cloudformation_example_cloudformation_ListStackResources_section"></a>

次のサンプルコードは、`ListStackResources` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**スタック内のリソースを一覧表示する方法**  
以下のコマンドは、指定されたスタック内にあるリソースを一覧表示します。  

```
aws cloudformation list-stack-resources \
    --stack-name my-stack
```
出力:  

```
{
    "StackResourceSummaries": [
        {
            "LogicalResourceId": "bucket",
            "PhysicalResourceId": "my-stack-bucket-1vc62xmplgguf",
            "ResourceType": "AWS::S3::Bucket",
            "LastUpdatedTimestamp": "2019-10-02T04:34:11.345Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "LogicalResourceId": "function",
            "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
            "ResourceType": "AWS::Lambda::Function",
            "LastUpdatedTimestamp": "2019-10-02T05:34:27.989Z",
            "ResourceStatus": "UPDATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "LogicalResourceId": "functionRole",
            "PhysicalResourceId": "my-functionRole-HIZXMPLEOM9E",
            "ResourceType": "AWS::IAM::Role",
            "LastUpdatedTimestamp": "2019-10-02T04:34:06.350Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        }
    ]
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListStackResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/list-stack-resources.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたスタックに関連付けられているすべてのリソースの説明を返します。**  

```
Get-CFNStackResourceSummary -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[ListStackResources](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたスタックに関連付けられているすべてのリソースの説明を返します。**  

```
Get-CFNStackResourceSummary -StackName "myStack"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[ListStackResources](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `ListStacks` を使用する
<a name="cloudformation_example_cloudformation_ListStacks_section"></a>

次のサンプルコードは、`ListStacks` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation スタックを一覧表示するには**  
以下の `list-stacks` コマンドは、ステータスが `CREATE_COMPLETE` になっているすべてのスタックの概要を表示します。  

```
aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE
```
出力:  

```
[
    {
        "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896",
        "TemplateDescription": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
        "StackStatusReason": null,
        "CreationTime": "2013-08-26T03:27:10.190Z",
        "StackName": "myteststack",
        "StackStatus": "CREATE_COMPLETE"
    }
]
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListStacks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/list-stacks.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: すべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary
```
**例 2: 現在作成中のすべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary -StackStatusFilter "CREATE_IN_PROGRESS"
```
**例 3: 現在作成中または更新中のすべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary -StackStatusFilter @("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS")
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[ListStacks](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: すべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary
```
**例 2: 現在作成中のすべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary -StackStatusFilter "CREATE_IN_PROGRESS"
```
**例 3: 現在作成中または更新中のすべてのスタックの概要情報を返します。**  

```
Get-CFNStackSummary -StackStatusFilter @("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS")
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[ListStacks](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `UpdateStack` を使用する
<a name="cloudformation_example_cloudformation_UpdateStack_section"></a>

次のサンプルコードは、`UpdateStack` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation スタックを更新するには**  
以下の `update-stack` コマンドは、`mystack` スタックのテンプレートと入力パラメータを更新します。  

```
aws cloudformation update-stack --stack-name mystack --template-url https://s3.amazonaws.com/sample/updated.template --parameters ParameterKey=KeyPairName,ParameterValue=SampleKeyPair ParameterKey=SubnetIDs,ParameterValue=SampleSubnetID1\\,SampleSubnetID2
```
以下の `update-stack` コマンドは、`mystack` スタックの `SubnetIDs` パラメータ値のみを更新します。パラメータ値を指定しない場合は、テンプレートで指定されているデフォルト値が使用されます。  

```
aws cloudformation update-stack --stack-name mystack --template-url https://s3.amazonaws.com/sample/updated.template --parameters ParameterKey=KeyPairName,UsePreviousValue=true ParameterKey=SubnetIDs,ParameterValue=SampleSubnetID1\\,UpdatedSampleSubnetID2
```
以下の `update-stack` コマンドは、`mystack` スタックに 2 つのスタック通知トピックを追加します。  

```
aws cloudformation update-stack --stack-name mystack --use-previous-template --notification-arns "arn:aws:sns:use-east-1:123456789012:mytopic1" "arn:aws:sns:us-east-1:123456789012:mytopic2"
```
詳細については、「*AWS CloudFormation ユーザーガイド*」の「[AWS CloudFormation スタックの更新](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[UpdateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/update-stack.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」はテンプレートで宣言されているパラメータの名前を表し、「PV1」はその値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 2: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**例 3: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」はテンプレートで宣言されているパラメータの名前を表し、「PV2」はその値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" -TemplateBody "{Template Content Here}" -Parameters @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 4: 指定されたテンプレート (Amazon S3 から取得されたもの) とカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**例 5: 指定されたテンプレート (Amazon S3 から取得されたもの) とカスタマイゼーションパラメータでスタック「MyStack」(この例では IAM リソースが含まれていることを想定) を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。IAM リソースが含まれるスタックでは、-Capabilities「CAPABILITY\$1IAM」パラメータを指定する必要があります。指定されていない場合は、更新が「InsufficientCapabilities」エラーで失敗します。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } ) `
                -Capabilities "CAPABILITY_IAM"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[UpdateStack](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」はテンプレートで宣言されているパラメータの名前を表し、「PV1」はその値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 2: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**例 3: 指定されたテンプレートとカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」はテンプレートで宣言されているパラメータの名前を表し、「PV2」はその値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" -TemplateBody "{Template Content Here}" -Parameters @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**例 4: 指定されたテンプレート (Amazon S3 から取得されたもの) とカスタマイゼーションパラメータでスタック「MyStack」を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**例 5: 指定されたテンプレート (Amazon S3 から取得されたもの) とカスタマイゼーションパラメータでスタック「MyStack」(この例では IAM リソースが含まれていることを想定) を更新します。「PK1」と「PK2」はテンプレートで宣言されているパラメータの名前を表し、「PV1」と「PV2」はそれらの要求された値を表します。カスタマイゼーションパラメータは、「ParameterKey」と「ParameterValue」の代わりに、「Key」と「Value」を使用して指定することもできます。IAM リソースが含まれるスタックでは、-Capabilities「CAPABILITY\$1IAM」パラメータを指定する必要があります。指定されていない場合は、更新が「InsufficientCapabilities」エラーで失敗します。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } ) `
                -Capabilities "CAPABILITY_IAM"
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[UpdateStack](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# CLI で `ValidateTemplate` を使用する
<a name="cloudformation_example_cloudformation_ValidateTemplate_section"></a>

次のサンプルコードは、`ValidateTemplate` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
** AWS CloudFormation テンプレートを検証するには**  
以下の `validate-template` コマンドは、`sampletemplate.json` テンプレートを検証します。  

```
aws cloudformation validate-template --template-body file://sampletemplate.json
```
出力:  

```
{
    "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
    "Parameters": [],
    "Capabilities": []
}
```
詳細については、 AWS 「 CloudFormation *AWS ユーザーガイド」のCloudFormation *テンプレートの使用」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ValidateTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/validate-template.html)」を参照してください。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**例 1: 指定されたテンプレートコンテンツを検証します。出力は、テンプレートの機能、説明、パラメータを詳しく説明します。**  

```
Test-CFNTemplate -TemplateBody "{TEMPLATE CONTENT HERE}"
```
**例 2: Amazon S3 URL 経由でアクセスされる、指定されたテンプレートを検証します。出力は、テンプレートの機能、説明、パラメータを詳しく説明します。**  

```
Test-CFNTemplate -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V4)*」の「[ValidateTemplate](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: 指定されたテンプレートコンテンツを検証します。出力は、テンプレートの機能、説明、パラメータを詳しく説明します。**  

```
Test-CFNTemplate -TemplateBody "{TEMPLATE CONTENT HERE}"
```
**例 2: Amazon S3 URL 経由でアクセスされる、指定されたテンプレートを検証します。出力は、テンプレートの機能、説明、パラメータを詳しく説明します。**  

```
Test-CFNTemplate -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template
```
+  API の詳細については、「*AWS Tools for PowerShell Cmdlet Reference (V5)*」の「[ValidateTemplate](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

------

# AWS SDKs CloudFormation を使用するシナリオ
<a name="cloudformation_code_examples_scenarios"></a>

次のコード例は、 AWS SDKs を使用して で CloudFormation 一般的なシナリオを実装する方法を示しています。これらのシナリオでは、 内で複数の関数を呼び出す CloudFormation か、他の関数と組み合わせて特定のタスクを実行する方法を示します AWS のサービス。各シナリオには、完全なソースコードへのリンクが含まれており、そこからコードの設定方法と実行方法に関する手順を確認できます。

シナリオは、サービスアクションをコンテキストで理解するのに役立つ中級レベルの経験を対象としています。

**Topics**
+ [COVID-19 データを追跡する REST API を作成する](cloudformation_example_cross_ApiGatewayDataTracker_section.md)

# COVID-19 データを追跡する API Gateway REST API を作成する
<a name="cloudformation_example_cross_ApiGatewayDataTracker_section"></a>

次のコード例は、架空のデータを使用して、米国の COVID-19 の日常的なケースを追跡するシステムをシミュレートする REST API を作成する方法を示しています。

------
#### [ Python ]

**SDK for Python (Boto3)**  
 で AWS Chalice を使用して AWS SDK for Python (Boto3) 、Amazon API Gateway AWS Lambda、および Amazon DynamoDB を使用するサーバーレス REST API を作成する方法を示します。REST API は、架空のデータを使用して、米国の COVID-19 の日常的なケースを追跡するシステムをシミュレートします。以下ではその方法を説明しています。  
+  AWS Chalice を使用して、API Gateway 経由で送信される REST リクエストを処理するために呼び出される Lambda 関数のルートを定義します。
+ Lambda 関数を使用して、DynamoDB テーブルにデータを取得して保存し、REST リクエストを処理します。
+  AWS CloudFormation テンプレートでテーブル構造とセキュリティロールリソースを定義します。
+  AWS Chalice と CloudFormation を使用して、必要なすべてのリソースをパッケージ化してデプロイします。
+ CloudFormation を使用して、作成されたすべてのリソースをクリーンアップします。
 完全なソースコードとセットアップおよび実行の手順については、[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/cross_service/apigateway_covid-19_tracker) で完全な例を参照してください。  

**この例で使用されているサービス**
+ API ゲートウェイ
+ CloudFormation
+ DynamoDB
+ Lambda

------