DEV Community

Cover image for AWSSDK.CloudFormation (for AWS CloudFormation)
Ahmed Adel for AWS Community Builders

Posted on • Updated on

AWSSDK.CloudFormation (for AWS CloudFormation)

✦ The Fourth SDK we are going to talk about is AWSSDK.CloudFormation.

First of all, let's have a brief about Amazon CloudFormation in AWS...

➽What is Amazon CloudFormation?

AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS.
☞ You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you.
☞ You don't need to individually create and configure AWS resources and figure out what's dependent on what; CloudFormation handles that.
☞ Read more about Amazon CloudFormation

➽Installing AWSSDK.CloudFormation :

AWSSDK.CloudFormation is installed mainly from Nuget
☞There is 3 ways to install AWSSDK.CloudFormation, they are the same as installing AWSSDK.S3 from Part 1 of this series
☞let's use the easiest one, from Package Manager Console by using the Install-Package command.

PM> Install-Package AWSSDK.CloudFormation
Enter fullscreen mode Exit fullscreen mode

🌟 Second step is to connect to our AWS account using __ Access keys (Access Key ID and Secret Access Key)__, this was explained before briefly in the first article under (Get AWS Access keys)

✦ The AWS SDK for .NET provides APIs for AWS CloudFormation clients. The APIs enable you to work with AWS CloudFormation features such as templates and stacks.
✦ The example uses the low-level API. The application takes no arguments, but simply gathers information for all stacks that are accessible to the user's credentials and then displays information
about those stacks.

using System;
using System.Threading.Tasks;
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;

namespace CFNListResources
    class Program
        static async Task Main(string[] args)
            // Create the CloudFormation client
            var cfnClient = new AmazonCloudFormationClient();

            // List the resources for each stack
            await ListResources(cfnClient, await cfnClient.DescribeStacksAsync());

        // Method to list stack resources and other information
        private static async Task ListResources(
          IAmazonCloudFormation cfnClient, DescribeStacksResponse responseDescribeStacks)
            Console.WriteLine("Getting CloudFormation stack information...");

            foreach (Stack stack in responseDescribeStacks.Stacks)
                // Basic information for each stack
                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 cfnClient.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}");
Enter fullscreen mode Exit fullscreen mode

References: AWS official Documentation

Top comments (0)