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개 답변
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
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠