Search code examples
amazon-web-servicesamazon-ec2amazon-ami

AWS CloudFormation Template for customized windows AMI not running cfn-init and not creating the related files


Below is my template snippet in which Windows Custom AMI is used to create the server. Here cfn-init.exe is not getting executed even if I have given it in the userdata block. As a result no files are gettting created in my server. Please help.

Thanks in Advance.

"CMServer":{ 
"Type":"AWS::EC2::Instance",
"Metadata":{ "AWS::CloudFormation::Init":{ 
               "config":{ "files":{ "c:\\cfn
                                    cfn-hup.conf":{ 
                                     "content":{ "Fn::Join":[ "",[ "main\n","stack=",
                                       { "Ref":"AWS::StackName"},"\n","region=",
                                          { "Ref":"AWS::Region"},"\n",
                                            "interval=1"]]}},
                  "c:\\cfn\\hooks.d
                   cfn-auto-reloader.conf":{ "content":{ 
                               "Fn::Join":[ "",[ "cfn-auto-reloader-hook\n",
                                 "triggers=post.update\n",
                            "path=Resources.CMServer.Metadata.AWS::CloudFormation::Init\n",
                              "action=cfn-init.exe -v -s ",{ "Ref":"AWS::StackName"},"                             
                             -r CMServer",
                             " --region ",
                               { 
                                 "Ref":"AWS::Region"},"\n"]]}},
             "C:\\inetpub\\wwwroot\\AWSCM\\Website\\App_Config
            ConnectionStrings.config":{ 
            "content":{ 
             "Fn::Join":[ "",[ 
             "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
             "<connectionStrings>\n",
             "<add name=\"core\" connectionString=\"user id=user;password=******;Data Source=",
        { "Fn::GetAtt": },
          ";Database=CoreDb\"/>\n",
"<add name=\"master\" connectionString=\"user id=user;password=******;Data Source=",
{ "Fn::GetAtt": },
";Database=MasterDb\"/>\n",
"<add name=\"web\" connectionString=\"user id=user;password=******;Data Source=",
{ "Fn::GetAtt": },
";Database=WebdB\"/>\n",
"</connectionStrings>"
]
]
},
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
},
"services":{ 
"windows":{ 
"cfn-hup":{ 
"enabled":"true",
"ensureRunning":"true",
"files":[ 
"c:\\cfn
cfn-hup.conf", "c:\\cfn\\hooks.d
cfn-auto-reloader.conf"
]
}
}
}
}
}
},
"Properties":{ 
"ImageId":{ 
"Fn::FindInMap":[ 
"AWSRegionArch2AMI",
{ 
"Ref":"AWS::Region"
},
"SitecoreCMAMI"
]
},
"SecurityGroupIds":[ 
{ 
"Ref":"CMServerSecurityGroup"
}
],
"SubnetId":{ 
"Ref":"PrivateSubnet1"
},
"InstanceType":{ 
"Ref":"CMInstanceType"
},
"KeyName":{ 
"Ref":"KeyName"
},
"UserData":{ 
"Fn::Base64":{ 
"Fn::Join":[ 
"",
[ 
"<script>\n",
"cfn-init.exe -v -s ",
{ 
"Ref":"AWS::StackName"
},
" -r CMServer",
" --region ",
{ 
"Ref":"AWS::Region"
},
"\n"
"</script>"
]
]
}
},
"Tags":[ `enter code here`
{ 
"Key":"Name",
"Value":"CMServer"
}
]
}
},

Solution

  • I got solved the problem. the problem was with template JSON format. I tried with single quotes(') instead of double like "\n",

    . hence solved.

    thank you for your response.