사용을 위한 코드 예제 AWS CloudFormationAWS SDKs - AWS SDK코드 예제

AWS 문서 AWS SDK SDK 예제 GitHub 리포지토리에 더 많은 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

사용을 위한 코드 예제 AWS CloudFormationAWS SDKs

다음 코드 예제는 AWS 소프트웨어 개발 키트 (SDK) AWS CloudFormation 와 함께 사용하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 호출하는 방법을 보여 주며 관련 시나리오와 교차 서비스 예시에서 컨텍스트에 맞는 작업을 볼 수 있습니다.

교차 서비스 예시는 여러 AWS 서비스전반에서 작동하는 샘플 애플리케이션입니다.

추가 리소스
  • AWS CloudFormation 사용 설명서 — 에 대한 추가 정보 AWS CloudFormation.

  • AWS CloudFormation API참조 — 사용 가능한 모든 AWS CloudFormation 작업에 대한 세부 정보

  • AWS 개발자 센터 — 카테고리 또는 전체 텍스트 검색별로 필터링할 수 있는 코드 예제입니다.

  • AWS SDK예 — 선호하는 언어로 작성된 전체 코드가 포함된 GitHub 리포지토리. 코드 설정 및 실행을 위한 지침이 포함되어 있습니다.

시작하기

다음 코드 예제에서는 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; } } }