简体   繁体   中英

Cloudformation template in YAML is failing to parse the user data powershell script

I have written a cloudformation template in YAML, and everything runs smoothly but now instead of manually going into powershell to add a local group member and install some windows features I want to add the powershell commands to the user data portion of the AWS::EC2::Instance properties. Here's the template in short:

Resources:
   Instance:
      Properties:
         UserData:
            Fn::Base64: |
               <powershell>
               add-localgroupmember (my group member)
               install-windowsfeature (my windows feature)
               </powershell>

weirdly enough the local group member gets added automatically but the windows feature doesn't get installed. Is there a certain format for the commands when they are multi line? Here is the log error:

2022-11-25 19:48:58 Info: Try parsing user data in yaml format
2022-11-25 19:48:58 Info: Parsing failed, fall back to XML format
2022-11-25 19:48:58 Info: Converting user data to yaml format

I have tried to format the powershell script differently, nothing changed. I also tried adding the script one command at a time and so far the only command that works is the local group member and not the windows feature installing.

I took this same template and added an outfile only to check where the powershell script stops. Sometimes these files are created and sometimes they are not. Same with the adding local group member line. Only sometimes they are added. I am not sure what is going on here.

the below yml template adds a user as well as adds a feature.

Let me know if it is helpful.

Resources:
  WebServer1:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0be29bafdaad782db
      InstanceType: t2.micro
      KeyName: NVirg-KP
      SecurityGroups:
        - EC2-AllTraffic-SG
      UserData:
        Fn::Base64: |
           <powershell>
            #username and password
            $username = "ec2-user1"
            $description = "ec2-user1"
            $password = ConvertTo-SecureString "Login@1234" -AsPlainText -Force

            #creating the user
            New-LocalUser -Name $username -Password $password -FullName $username -Description $description
            Add-LocalGroupMember -Group Administrators -Member $username
            
            #Installing a feature
            Import-Module ServerManager
            Install-WindowsFeature -Name Web-Server
           </powershell>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM