AWS CloudFormation 使用 AWS SDK 的程式碼範例 - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS CloudFormation 使用 AWS SDK 的程式碼範例

下列程式碼範例會示範如何搭 AWS CloudFormation 配 AWS 軟體開發套件 (SDK) 使用。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Cross-service examples (跨服務範例) 是跨多個 AWS 服務執行的應用程式範例。

其他 資源

開始使用

下列程式碼範例會示範如何開始使用 AWS CloudFormation。

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

using Amazon.CloudFormation; using Amazon.CloudFormation.Model; using Amazon.Runtime; namespace CloudFormationActions; public static class HelloCloudFormation { public static IAmazonCloudFormation _amazonCloudFormation; 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()); 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.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.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; } } }