I have 2 templates as below. What I want to ask is how can I use Sub
and other variables in Transform?
When I add UserData, I get the following error. This is about Sub
. How can I change it according to the transform?
Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON. Rollback requested by user.
main.yaml
Parameters:
VpcId:
Type: 'AWS::EC2::VPC::Id'
Subnets:
Type: 'List<AWS::EC2::Subnet::Id>'
InstanceType:
Type: String
Default: c5.large
AllowedValues:
- c5.large
- c5.xlarge
Resources:
Instance:
Fn::Transform:
Name: AWS::Include
Parameters:
Location:
Fn::Sub: s3://test-bucket/instance.yaml
instance.yaml
Type: 'AWS::EC2::Instance'
Properties:
KeyName: test
ImageId: ami-0015a39e4b7c0966f
InstanceType:
Ref: InstanceType
SubnetId:
Fn::Select: [ 0, Ref: Subnets ]
SecurityGroupIds:
- Fn::GetAtt: [ Sec, GroupId ]
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --configsets wordpress_install --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}
CodePudding user response:
You can't use !Sub
. It should be Fn::Sub:
:
Type: 'AWS::EC2::Instance'
Properties:
KeyName: test
ImageId: ami-0015a39e4b7c0966f
InstanceType:
Ref: InstanceType
SubnetId:
Fn::Select: [ 0, Ref: Subnets ]
SecurityGroupIds:
- Fn::GetAtt: [ Sec, GroupId ]
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --configsets wordpress_install --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}