在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:awslabs/goformation开源软件地址:https://github.com/awslabs/goformation开源编程语言:Go 100.0%开源软件介绍:AWS GoFormation
Main features
InstallationAs with other Go libraries, GoFormation can be installed with
UsageMarshalling CloudFormation/SAM described with Go structs, into YAML/JSONBelow is an example of building a CloudFormation template programmatically, then outputting the resulting JSON package main
import (
"fmt"
"strconv"
"time"
"github.com/awslabs/goformation/v6/cloudformation"
"github.com/awslabs/goformation/v6/cloudformation/sns"
)
func main() {
// Create a new CloudFormation template
template := cloudformation.NewTemplate()
// Create an Amazon SNS topic, with a unique name based off the current timestamp
template.Resources["MyTopic"] = &sns.Topic{
TopicName: cloudformation.String("my-topic-" + strconv.FormatInt(time.Now().Unix(), 10)),
}
// Create a subscription, connected to our topic, that forwards notifications to an email address
template.Resources["MyTopicSubscription"] = &sns.Subscription{
TopicArn: cloudformation.Ref("MyTopic"),
Protocol: "email",
Endpoint: cloudformation.String("[email protected]"),
}
// 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)
} else {
fmt.Printf("%s\n", string(y))
}
} Would output the following JSON template: {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"MyTopic": {
"Properties": {
"TopicName": "my-topic-1536878058"
},
"Type": "AWS::SNS::Topic"
},
"MyTopicSubscription": {
"Properties": {
"Endpoint": "[email protected]",
"Protocol": "email",
"TopicArn": {
"Ref": "MyTopic"
}
},
"Type": "AWS::SNS::Subscription"
}
}
} ...and the following YAML template: AWSTemplateFormatVersion: 2010-09-09
Resources:
MyTopic:
Properties:
TopicName: my-topic-1536878058
Type: AWS::SNS::Topic
MyTopicSubscription:
Properties:
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:
Unmarshalling CloudFormation YAML/JSON into Go structsGoFormation also works the other way - parsing JSON/YAML CloudFormation/SAM templates into Go structs. package main
import (
"log"
"github.com/awslabs/goformation/v6"
)
func main() {
// Open a template from file (can be JSON or YAML)
template, err := goformation.Open("template.yaml")
if err != nil {
log.Fatalf("There was an error processing the template: %s", err)
}
// You can extract all resources of a certain type
// Each AWS CloudFormation resource is a strongly typed struct
functions := template.GetAllServerlessFunctionResources()
for name, function := range functions {
// E.g. Found a AWS::Serverless::Function named GetHelloWorld (runtime: nodejs6.10)
log.Printf("Found a %s named %s (runtime: %s)\n", function.AWSCloudFormationType(), name, function.Runtime)
}
// You can also search for specific resources by their logicalId
search := "GetHelloWorld"
function, err := template.GetServerlessFunctionWithName(search)
if err != nil {
log.Fatalf("Function not found")
}
// E.g. Found a AWS::Serverless::Function named GetHelloWorld (runtime: nodejs6.10)
log.Printf("Found a %s named %s (runtime: %s)\n", function.AWSCloudFormationType(), search, function.Runtime)
} Updating CloudFormation / SAM Resources in GoFormationAWS GoFormation contains automatically generated Go structs for every CloudFormation/SAM resource, located in the cloudformation/ directory. These can be generated, from the latest AWS CloudFormation Resource Specification published for
The GoFormation build pipeline automatically checks for any updated AWS CloudFormation resources on a daily basis, and creates a pull request against this repository if any are found. AdvancedAWS CloudFormation Intrinsic FunctionsThe following AWS CloudFormation Intrinsic Functions are supported in GoFormation:
Any unsupported intrinsic functions will return Resolving References (Ref)When converting a YAML/JSON template to go, the intrinsic 'Ref' function as implemented will resolve all of the pseudo parameters such as 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 VersioningThis library is automatically versioned and tagged using semantic-release. ContributingContributions and feedback are welcome! Proposals and pull requests will be considered and responded to. For more information, see the CONTRIBUTING file. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论