What is the correct format of writing TemplateBody of CfnStackSetProps struct in go CDK v2?

0

I'm going to use CDK to create a stackset resources and I want to use the TemplateBody parameters.

Here is my code snippet:

import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" )

func NewMyStack(scope constructs.Construct, id string, props *awscdk.StackProps) awscdk.Stack {

stack := awscdk.NewStack(scope, &id, props)

yaml := `
AWSTemplateFormatVersion: 2010-09-09
Description: test

Resources:
  eventbridgerulecentralaccount:
	Type: AWS::Events::Rule
	Properties:
	  Description: Forward event to EventBus
	  EventBusName: default
	  State: ENABLED
	  EventPattern:
		source:
		- custom.dingtalkevent.test
	  Targets:
	  - Arn: "arn:aws:xxx"
		Id: dingtalk-eventbus-account
		RoleArn:
		  Fn::GetAtt:
		  - EventBridgeIAMrole
		  - Arn
  EventBridgeIAMrole:
	Type: AWS::IAM::Role
	Properties:
	  AssumeRolePolicyDocument:
		Version: '2012-10-17'
		Statement:
		- Effect: Allow
		  Principal:
			Service:
			  Fn::Sub: events.amazonaws.com
		  Action: sts:AssumeRole
	  Path: "/"
	  Policies:
	  - PolicyName: PutEventsDestinationBus
		PolicyDocument:
		  Version: '2012-10-17'
		  Statement:
		  - Effect: Allow
			Action:
			- events:PutEvents
			Resource:
			- "arn:aws:events:xxx"`

awscdk.NewCfnStackSet(stack, jsii.String("MyStackSet"), &awscdk.CfnStackSetProps{
	PermissionModel: jsii.String("SERVICE_MANAGED"),
	StackSetName:    jsii.String("MyStackSet"),
	AutoDeployment: &awscdk.CfnStackSet_AutoDeploymentProperty{
		Enabled:                      jsii.Bool(true),
		RetainStacksOnAccountRemoval: jsii.Bool(true),
	},
	TemplateBody: jsii.String(yaml),
	Capabilities: jsii.Strings("CAPABILITY_NAMED_IAM"),
})
return stack

}

I got the error "Resource handler returned message: "Template format error: YAML not well-formed. (line 2, column 1) (Service: CloudFormation, Status Code: 400" when I deploy it.

I've tried to create stackset via AWS console use the yaml in TemplateBody and everything works fine. I would understand how to use the TemplateBody in Go CDK.

1 Answer
0

It looks like you are doing the right thing here.

Maybe just test it out here

https://codebeautify.org/yaml-validator

It could be something weird around tabs and spaces

If that doesn’t get you anywhere maybe try reducing the template string down to something super simple just to ensure it is in-fact the yaml string that is causing issues. Even just do a CDK Synth to see it work so as not the deploy anything. Then maybe build it up to this final string.

profile picture
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions