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.