2
votes

I am facing issue while launching cloudformation template passing userdata. Seems like no commands are running inside. Instance is coming up healthy without running these commands. Please help to resolve.

AWSTemplateFormatVersion : 2010-09-09
Description: sonar
Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance 
    Properties:
      KeyName: sivakey_feb
      ImageId: ami-04bfee437f38a691e
      UserData:
        Fn::Base64: 
          !Sub |
            #!/bin/bash -xe
            sudo chown -R ec2-user:ec2-user /opt/sonar/temp/conf
            /opt/sonar/bin/linux-x86-64/sonar.sh start
      InstanceType: t2.large
      Tags:
        -
          Key: Name
          Value: sonar
1

1 Answers

2
votes

So I can't see anything wrong with your script. But I would suggest following actions;

  1. executable flag to your script so

    chmod +x /opt/sonar/bin/linux-x86-64/sonar.sh
    
  2. for debugging purposes add the following line

    exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
    

So your userdata should look like;

    #!/bin/bash -xe
    exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
    sudo chown -R ec2-user:ec2-user /opt/sonar/temp/conf
    chmod +x /opt/sonar/bin/linux-x86-64/sonar.sh
    /opt/sonar/bin/linux-x86-64/sonar.sh start

The additional line taken from aws docs;

To troubleshoot issues on your EC2 instance bootstrap without having to access the instance through SSH, you can add code to your user-data bash script that redirects all the output both to the /var/log/user-data.log and to /dev/console. When the code is executed, you can see your user-data invocation logs in your console.