Use goformation.Open(filename) to parse an existing JSON or YAML CloudFormation/SAM template into a Go template object. Once opened, you can interact with the resources using typed methods:
- Use
GetAllServerlessFunctionResources() to retrieve all AWS::Serverless::Function resources. - Use
GetServerlessFunctionWithName(name) to find a specific function by its logical ID.
package main
import (
"log"
"github.com/awslabs/goformation/v4"
)
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)
}