Giter VIP home page Giter VIP logo

comformation's Introduction

AWS CloudFormation

Comformation.NET

NuGet Build status

Compose AWS CloudFormation templates with .NET

This library helps you to compose AWS CloudFormation templates with C#.
It supports ALL resources available in AWS CloudFormation templates.
Supports Intrinsic Functions.
All properties are strong typed. No open dynamic or object types. Allowed property types validated in compile time.
Based on official AWS CloudFormation templates specification.

Show me the code

This is a sample of Comformation library usage.
The generated template plays arround mappings, input and output parameters.
The template creates S3 bucket whose name generated using combination of several intrinsic functions.
For a full code sample see CreateStackTest.cs

Install Nuget package

Install-Package Comformation

then:

var template = new Template
{
    Description = "Test comformation",
    Parameters = new Parameters
    {
        new Parameter
        {
            LogicalId = "Param1",
            Type = ParameterType.String,
            Default = "Value1",
            Description = "Parameter 1",
        },
        new Parameter
        {
            LogicalId = "Param2",
            Type = ParameterType.Number,
            AllowedValues = new object [] { 1, 2 },
            Description = "Parameter 2",
        }
    },
    Resources = new Resources
    {
        new BucketResource
        {
            LogicalId = "MyBucket",
            Properties =
            {
                // resource name build using intrinsic functions and based on input parameters and mapping
                BucketName = Fn.Sub($"test-bucket-${{sufix}}-{Guid.NewGuid()}",
                    new Sub.Item("sufix", Fn.FindInMap("BucketSufix", Fn.Ref(PseudoParameter.Region), Fn.Ref("Param2")))),
                Tags = new List<Tag>
                {
                    new Tag
                    {
                        Key = "Project", Value = "Comformation"
                    }
                }
            }
        }
    },
    Mappings = new Mappings
    {
        new Mapping
        {
            Name = "BucketSufix",
            Regions =
            {
                new RegionMap
                {
                    Region = RegionEndpoint.EUWest1,
                    Map = { { "1", "ireland-1" }, {"2", "ireland-2"} }
                },
                new RegionMap
                {
                    Region = RegionEndpoint.EUWest2,
                    Map = { { "1", "london-1" }, { "2", "london-2"} }
                }
            }
        }
    },
    Outputs = new Outputs
    {
        // output based on input parameter
        new Output
        {
            Description = "Input Parameter 1",
            LogicalId = "Input1",
            Value = Fn.Ref("Param1")
        },
        // output based on input parameter
        new Output
        {
            Description = "Input Parameter 2",
            LogicalId = "Input2",
            Value = Fn.Ref("Param2")
        },
        // output based on pseudo parameter - region where stack run
        new Output
        {
            LogicalId = "Region",
            Description = "Region",
            Value = Fn.Ref(PseudoParameter.Region)
        },
        // output based on pseudo parameter - created stack id
        new Output
        {
            LogicalId = "StackId",
            Description = "Stack Id",
            Value = Fn.Ref(PseudoParameter.StackId)
        },
        // output based on pseudo parameter - created stack name
        new Output
        {
            LogicalId = "StackName",
            Description = "Stack Name",
            Value = Fn.Ref(PseudoParameter.StackName)
        },
        // output based on created resource
        new Output
        {
            LogicalId = "CreatedBucket",
            Description = "Name of created bucket",
            Value = Fn.Ref("MyBucket")
        },
        // output based on input param and mapping
        new Output
        {
            LogicalId = "Sufix",
            Description = "Bucket name sufix",
            Value = Fn.FindInMap("BucketSufix", Fn.Ref(PseudoParameter.Region), Fn.Ref("Param2"))
        }
    }
};

var request = new CreateStackRequest
{
    StackName = "ComformationTestSimpleTemplate",
    TemplateBody = template.ToString(),
    Parameters = new List<Amazon.CloudFormation.Model.Parameter>
    {
        new Amazon.CloudFormation.Model.Parameter
        {
            ParameterKey = "Param2",
            ParameterValue = "2"
        }
    },
};

// Create stack
var cloudformation = new AmazonCloudFormationClient(RegionEndpoint.EUWest1);
var createResponse = await cloudformation.CreateStackAsync(request);

comformation's People

Contributors

gitter-badger avatar stanb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

comformation's Issues

s3 PolicyDocument

Hi,

Many thanks for this project, it's already proving to be very useful.

May I ask for help with regard to converting the below YML to Comformation code, please?

I'm stuck on how exactly I should be using the Union object for the PolicyDocument.

Many thanks,

Luke.

`

DeployBucketPolicy: 

Type: AWS::S3::BucketPolicy

Properties: 

Bucket: !Ref DeployBucket

PolicyDocument: 

  Statement: 

    - 

      Sid: PublicReadGetObject

      Effect: Allow

      Principal: "*"

      Action: 

        - s3:GetObject            

      Resource: !Join [ "", [ "arn:aws:s3:::", !Ref DeployBucket, "/*" ] ]

`

ALB Subnets Question

Hi, Thanks for publishing this library - it would have been super handy when I started writing CF from hand a couple of years ago ๐Ÿ˜‚.

I'm looking at converting those templates into c#, but have come up with a challenge with subnets.

I have a template where the subnets are passed as a parameter:

Parameters:

    ALBSubnets:
        Description: Choose which subnets the Application Load Balancer should be deployed to
        Type: List<AWS::EC2::Subnet::Id>

Then used to in a LoadBalancer

    LoadBalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
            Subnets: !Ref ALBSubnets

The input value is actually a reference to output of another stack:

  WebsiteAndALB:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://xxxx.yaml
      Parameters:
        ALBSubnets: 
          Fn::ImportValue: !Sub "${NetworkStack}-PublicSubnets"

Question is:

  • Can I use an equivalent of ::ImportValue or something for this?
  • If not, could I use an array/list of strings? I can't quite work out the syntax.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.