This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from PaulMaddox/master
Support for CloudFormation intrinsic functions (Go->YAML/JSON)
- Loading branch information
Showing
316 changed files
with
1,095 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,17 @@ | |
[![Build Status](https://travis-ci.org/awslabs/goformation.svg?branch=0.1.0)](https://travis-ci.org/awslabs/goformation) [![GoDoc Reference](https://godoc.org/gopkg.in/awslabs/goformation.v1?status.svg)](http://godoc.org/github.com/awslabs/goformation) ![Apache-2.0](https://img.shields.io/badge/Licence-Apache%202.0-blue.svg) | ||
|
||
`GoFormation` is a Go library for working with AWS CloudFormation / AWS Serverless Application Model (SAM) templates. | ||
- [AWS GoFormation](#aws-goformation) | ||
- [Main features](#main-features) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Marshalling CloudFormation/SAM described with Go structs, into YAML/JSON](#marshalling-cloudformationsam-described-with-go-structs-into-yamljson) | ||
- [Unmarshalling CloudFormation YAML/JSON into Go structs](#unmarshalling-cloudformation-yamljson-into-go-structs) | ||
- [Updating CloudFormation / SAM Resources in GoFormation](#updating-cloudformation-sam-resources-in-goformation) | ||
- [Advanced](#advanced) | ||
- [AWS CloudFormation Intrinsic Functions](#aws-cloudformation-intrinsic-functions) | ||
- [Resolving References (Ref)](#resolving-references-ref) | ||
- [Contributing](#contributing) | ||
- [Main features](#main-features) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Marshalling CloudFormation/SAM described with Go structs, into YAML/JSON](#marshalling-cloudformationsam-described-with-go-structs-into-yamljson) | ||
- [Unmarshalling CloudFormation YAML/JSON into Go structs](#unmarshalling-cloudformation-yamljson-into-go-structs) | ||
- [Updating CloudFormation / SAM Resources in GoFormation](#updating-cloudformation--sam-resources-in-goformation) | ||
- [Advanced](#advanced) | ||
- [AWS CloudFormation Intrinsic Functions](#aws-cloudformation-intrinsic-functions) | ||
- [Resolving References (Ref)](#resolving-references-ref) | ||
- [Versioning](#versioning) | ||
- [Contributing](#contributing) | ||
|
||
## Main features | ||
|
||
|
@@ -41,6 +41,8 @@ package main | |
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/awslabs/goformation/cloudformation" | ||
) | ||
|
@@ -50,31 +52,27 @@ func main() { | |
// Create a new CloudFormation template | ||
template := cloudformation.NewTemplate() | ||
|
||
// An an example SNS Topic | ||
template.Resources["MySNSTopic"] = &cloudformation.AWSSNSTopic{ | ||
DisplayName: "test-sns-topic-display-name", | ||
TopicName: "test-sns-topic-name", | ||
Subscription: []cloudformation.AWSSNSTopic_Subscription{ | ||
cloudformation.AWSSNSTopic_Subscription{ | ||
Endpoint: "test-sns-topic-subscription-endpoint", | ||
Protocol: "test-sns-topic-subscription-protocol", | ||
}, | ||
}, | ||
// Create an Amazon SNS topic, with a unique name based off the current timestamp | ||
template.Resources["MyTopic"] = &cloudformation.AWSSNSTopic{ | ||
TopicName: "my-topic-" + strconv.FormatInt(time.Now().Unix(), 10), | ||
} | ||
|
||
// ...and a Route 53 Hosted Zone too | ||
template.Resources["MyRoute53HostedZone"] = &cloudformation.AWSRoute53HostedZone{ | ||
Name: "example.com", | ||
// Create a subscription, connected to our topic, that forwards notifications to an email address | ||
template.Resources["MyTopicSubscription"] = &cloudformation.AWSSNSSubscription{ | ||
TopicArn: cloudformation.Ref("MyTopic"), | ||
Protocol: "email", | ||
Endpoint: "[email protected]", | ||
} | ||
|
||
// Let's see the JSON | ||
// Let's see the JSON AWS CloudFormation template | ||
j, err := template.JSON() | ||
if err != nil { | ||
fmt.Printf("Failed to generate JSON: %s\n", err) | ||
} else { | ||
fmt.Printf("%s\n", string(j)) | ||
} | ||
|
||
// and also the YAML AWS CloudFormation template | ||
y, err := template.YAML() | ||
if err != nil { | ||
fmt.Printf("Failed to generate YAML: %s\n", err) | ||
|
@@ -91,24 +89,21 @@ Would output the following JSON template: | |
{ | ||
"AWSTemplateFormatVersion": "2010-09-09", | ||
"Resources": { | ||
"MyRoute53HostedZone": { | ||
"Type": "AWS::Route53::HostedZone", | ||
"MyTopic": { | ||
"Properties": { | ||
"Name": "example.com" | ||
} | ||
"TopicName": "my-topic-1536878058" | ||
}, | ||
"Type": "AWS::SNS::Topic" | ||
}, | ||
"MySNSTopic": { | ||
"Type": "AWS::SNS::Topic", | ||
"MyTopicSubscription": { | ||
"Properties": { | ||
"DisplayName": "test-sns-topic-display-name", | ||
"Subscription": [ | ||
{ | ||
"Endpoint": "test-sns-topic-subscription-endpoint", | ||
"Protocol": "test-sns-topic-subscription-protocol" | ||
} | ||
], | ||
"TopicName": "test-sns-topic-name" | ||
} | ||
"Endpoint": "[email protected]", | ||
"Protocol": "email", | ||
"TopicArn": { | ||
"Ref": "MyTopic" | ||
} | ||
}, | ||
"Type": "AWS::SNS::Subscription" | ||
} | ||
} | ||
} | ||
|
@@ -119,20 +114,32 @@ Would output the following JSON template: | |
```yaml | ||
AWSTemplateFormatVersion: 2010-09-09 | ||
Resources: | ||
MyRoute53HostedZone: | ||
Type: AWS::Route53::HostedZone | ||
MyTopic: | ||
Properties: | ||
Name: example.com | ||
MySNSTopic: | ||
TopicName: my-topic-1536878058 | ||
Type: AWS::SNS::Topic | ||
MyTopicSubscription: | ||
Properties: | ||
DisplayName: test-sns-topic-display-name | ||
Subscription: | ||
- Endpoint: test-sns-topic-subscription-endpoint | ||
Protocol: test-sns-topic-subscription-protocol | ||
TopicName: test-sns-topic-name | ||
Endpoint: [email protected] | ||
Protocol: email | ||
TopicArn: | ||
Ref: MyTopic | ||
Type: AWS::SNS::Subscription | ||
``` | ||
When creating templates, you can use the following convenience functions to use [AWS CloudFormation Intrinsics](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html): | ||
- `cloudformation.Ref(logicalName string)` | ||
- `cloudformation.GetAtt(logicalName string, attribute string)` | ||
- `cloudformation.ImportValue(name string)` | ||
- `cloudformation.Base64(input string)` | ||
- `cloudformation.CIDR(ipBlock, count, cidrBits string)` | ||
- `cloudformation.FindInMap(mapName, topLevelKey, secondLevelKey string)` | ||
- `cloudformation.GetAZs(region string)` | ||
- `cloudformation.Join(delimiter string, values []string)` | ||
- `cloudformation.Select(index string, list []string)` | ||
- `cloudformation.Split(delimiter, source string)` | ||
- `cloudformation.Sub(value string)` | ||
|
||
### Unmarshalling CloudFormation YAML/JSON into Go structs | ||
|
||
|
@@ -218,7 +225,7 @@ Any unsupported intrinsic functions will return `nil`. | |
|
||
#### Resolving References (Ref) | ||
|
||
The intrinsic 'Ref' function as implemented will resolve all of the [pseudo parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) such as `AWS::AccountId` with their default value as listed on [the bottom of this page](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html). | ||
When converting a YAML/JSON template to go, the intrinsic 'Ref' function as implemented will resolve all of the [pseudo parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) such as `AWS::AccountId` with their default value as listed on [the bottom of this page](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html). | ||
|
||
If a reference is not a pseudo parameter, GoFormation will try to resolve it within the AWS CloudFormation template. **Currently, this implementation only searches for `Parameters` with a name that matches the ref, and returns the `Default` if it has one.** | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.