cfn

package
v1.51.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 10 Imported by: 83

Documentation

Overview

Package cfn provides helpers for implementing AWS CloudFormation custom resources.

CloudFormation custom resources allow you to write custom provisioning logic that CloudFormation runs when you create, update, or delete stacks. This package handles the response protocol, making it easier to implement custom resource handlers.

The LambdaWrap helper catches errors and ensures proper responses are sent to CloudFormation's pre-signed URL, preventing stack operations from hanging.

See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

Example

CloudFormation custom resources require a different response handling due to the way stacks execute. The cfn.LambdaWrap helper catches all errors and ensures the correct response is sent to the pre-signed URL that comes with the event.

This example safely 'Echo' back anything given into the Echo parameter within the Custom Resource call.

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/cfn"
	"github.com/aws/aws-lambda-go/lambda"
)

func main() {
	lambda.Start(cfn.LambdaWrap(func(ctx context.Context, event cfn.Event) (physicalResourceID string, data map[string]interface{}, err error) {
		v, _ := event.ResourceProperties["Echo"].(string)

		data = map[string]interface{}{
			"Echo": v,
		}

		return
	}))
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomResourceFunction

type CustomResourceFunction func(context.Context, Event) (physicalResourceID string, data map[string]interface{}, err error)

CustomResourceFunction is a representation of the customer's Custom Resource function. LambdaWrap will take the returned values and turn them into a response to be sent to CloudFormation.

type CustomResourceLambdaFunction

type CustomResourceLambdaFunction func(context.Context, Event) (reason string, err error)

CustomResourceLambdaFunction is a standard form Lambda for a Custom Resource.

func LambdaWrap

func LambdaWrap(lambdaFunction CustomResourceFunction) (fn CustomResourceLambdaFunction)

LambdaWrap returns a CustomResourceLambdaFunction which is something lambda.Start() will understand. The purpose of doing this is so that Response Handling boiler plate is taken away from the customer and it makes writing a Custom Resource simpler.

func myLambda(ctx context.Context, event cfn.Event) (physicalResourceID string, data map[string]interface{}, err error) {
	physicalResourceID = "arn:...."
	return
}

func main() {
	lambda.Start(cfn.LambdaWrap(myLambda))
}

type Event

type Event struct {
	RequestType           RequestType            `json:"RequestType"`
	RequestID             string                 `json:"RequestId"`
	ResponseURL           string                 `json:"ResponseURL"`
	ResourceType          string                 `json:"ResourceType"`
	PhysicalResourceID    string                 `json:"PhysicalResourceId,omitempty"`
	LogicalResourceID     string                 `json:"LogicalResourceId"`
	StackID               string                 `json:"StackId"`
	ResourceProperties    map[string]interface{} `json:"ResourceProperties"`
	OldResourceProperties map[string]interface{} `json:"OldResourceProperties,omitempty"`
}

Event is a representation of a Custom Resource request

type RequestType

type RequestType string

RequestType represents the types of requests that come from a CloudFormation stack being run

const (
	RequestCreate RequestType = "Create"
	RequestUpdate RequestType = "Update"
	RequestDelete RequestType = "Delete"
)

type Response

type Response struct {
	Status             StatusType             `json:"Status"`
	RequestID          string                 `json:"RequestId"`
	LogicalResourceID  string                 `json:"LogicalResourceId"`
	StackID            string                 `json:"StackId"`
	PhysicalResourceID string                 `json:"PhysicalResourceId"`
	Reason             string                 `json:"Reason,omitempty"`
	NoEcho             bool                   `json:"NoEcho,omitempty"`
	Data               map[string]interface{} `json:"Data,omitempty"`
	// contains filtered or unexported fields
}

Response is a representation of a Custom Resource response expected by CloudFormation.

func NewResponse

func NewResponse(r *Event) *Response

NewResponse creates a Response with the relevant verbatim copied data from a Event

func (*Response) Send

func (r *Response) Send() error

Send will send the Response to the given URL using the default HTTP client

type SNSCustomResourceLambdaFunction added in v1.12.0

type SNSCustomResourceLambdaFunction func(context.Context, events.SNSEvent) (reason string, err error)

SNSCustomResourceLambdaFunction is a standard form Lambda for a Custom Resource that is triggered via a SNS topic.

func LambdaWrapSNS added in v1.12.0

func LambdaWrapSNS(lambdaFunction CustomResourceFunction) SNSCustomResourceLambdaFunction

LambdaWrapSNS wraps a Lambda handler with support for SNS-based custom resources. Usage and purpose otherwise same as LambdaWrap().

type StatusType

type StatusType string

StatusType represents a CloudFormation response status

const (
	StatusSuccess StatusType = "SUCCESS"
	StatusFailed  StatusType = "FAILED"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL