Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

lifadev/archive_aws-cloudformation-go-customres

Repository files navigation

Powered by Amazon Web Services Created by eawsy

eawsy/aws-cloudformation-go-customres

Author your AWS CloudFormation Custom Resources in Go.

Api Status License Help Social

AWS Lambda lets you run code without provisioning or managing servers. With eawsy/aws-lambda-go-shim, you can author your Lambda function code in Go. This project allows you to create AWS Lambda-backed Custom Resources for AWS CloudFormation in Go.

Quick Hands-On

For step by step instructions on how to author your AWS Lambda function code in Go, see eawsy/aws-lambda-go-shim.

Dependencies

go get -u -d github.com/eawsy/aws-lambda-go-core/...
go get -u -d github.com/eawsy/aws-lambda-go-event/...
go get -u -d github.com/eawsy/aws-cloudformation-go-customres/...

Create

package main

import (
	"encoding/json"
	"fmt"

	"github.com/eawsy/aws-cloudformation-go-customres/service/cloudformation/customres"
	"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
	"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/cloudformationevt"
)

// Handle is the exported handler called by AWS Lambda.
var Handle customres.LambdaHandler

type MyResource struct{}

func (r *MyResource) Create(evt *cloudformationevt.Event, ctx *runtime.Context) (string, interface{}, error) {
	var props map[string]string
	if err := json.Unmarshal(evt.ResourceProperties, &props); err != nil {
		return "", nil, err
	}

	id := customres.NewPhysicalResourceID(evt)

	resp := map[string]string{
		"Message": fmt.Sprintf("Hello, %s!", props["Name"]),
	}

	return id, resp, nil
}

func (r *MyResource) Update(evt *cloudformationevt.Event, ctx *runtime.Context) (string, interface{}, error) {
	return r.Create(evt, ctx)
}

func (r *MyResource) Delete(*cloudformationevt.Event, *runtime.Context) error {
	return nil
}

func init() {
	customres.Register("MyCompany@MyResource", new(MyResource))
	Handle = customres.HandleLambda
}

Build

For step by step instructions on how to author your AWS Lambda function code in Go, see eawsy/aws-lambda-go-shim.

make

Deploy

AWS CLI is used for the sake of simplicity. You are free to use your favorite deployment tool.

aws lambda create-function \
  --role arn:aws:iam::AWS_ACCOUNT_ID:role/lambda_basic_execution \
  --function-name cfn-customres \
  --zip-file fileb://package.zip \
  --runtime python2.7 \
  --handler handler.Handle

Invoke

Resources:
  HelloWorld:
    Type: Custom::MyCompany@MyResource
    Properties:
      ServiceToken: arn:aws:lambda:AWS_REGION:AWS_ACCOUNT_ID:function:cfn-customres
      Name: World2

Outputs:
  Message:
    Value: !GetAtt HelloWorld.Message
aws cloudformation create-stack \
  --template-body file://example.cfn.yaml
  --stack-name <YOUR STACK NAME>

aws cloudformation wait stack-create-complete \
  --stack-name <YOUR STACK NAME>

aws cloudformation describe-stacks \
  --stack-name <YOUR STACK NAME>

# {
# ...
#   "Outputs": [
#     {
#       "OutputKey": "Message", 
#       "OutputValue": "Hello, World!"
#     }
#   ]
# ...
# }

About

eawsy

This project is maintained and funded by Alsanium, SAS.

We ❤️ AWS and open source software. See our other projects, or hire us to help you build modern applications on AWS.

Contact

We want to make it easy for you, users and contributers, to talk with us and connect with each others, to share ideas, solve problems and make help this project awesome. Here are the main channels we're running currently and we'd love to hear from you on them.

Twitter

eawsyhq

Follow and chat with us on Twitter.

Share stories!

Gitter

eawsy/bavardage

This is for all of you. Users, developers and curious. You can find help, links, questions and answers from all the community including the core team.

Ask questions!

GitHub

pull requests & issues

You are invited to contribute new features, fixes, or updates, large or small; we are always thrilled to receive pull requests, and do our best to process them as fast as we can.

Before you start to code, we recommend discussing your plans through the eawsy/bavardage channel, especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, give you feedback on your design, and help you find out if someone else is working on the same thing.

Write code!

License

This product is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this product except in compliance with the License. See LICENSE and NOTICE for more information.

Trademark

Alsanium, eawsy, the "Created by eawsy" logo, and the "eawsy" logo are trademarks of Alsanium, SAS. or its affiliates in France and/or other countries.

Amazon Web Services, the "Powered by Amazon Web Services" logo, and AWS Lambda are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries.